gif圖動畫在android中仍是比較經常使用的,好比像新浪微博中,有不少gif圖片,並且展現很是好,因此我也想弄一個。通過我多方的搜索資料和整理,終於弄出來了,其實github上有不少開源的gif的展現代碼,我下載過幾個,可是都不是很理想,不是我徹底想要的。因此有時候就得本身學會總結,把開源的東西整理成本身的,如今無聊,也正好有朋友須要,因此如今整理了一下,留着之後備用!html
廢話很少說,直接上圖:java
在這裏主要用的是:android中的android.graphics.Movie 這個類,這是android提供給咱們的一個很是方便的工具。android
首先,重寫控件View,自定義一個展現gif圖的GifView,代碼以下:git
- package net.loonggg.gif.view;
-
- import net.loonggg.gif.R;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Movie;
- import android.os.Build;
- import android.util.AttributeSet;
- import android.view.View;
-
- public class GifView extends View {
-
-
- private static final int DEFAULT_MOVIE_DURATION = 1000;
-
- private int mMovieResourceId;
-
- private Movie mMovie;
-
- private long mMovieStart;
-
- private int mCurrentAnimationTime = 0;
-
- private float mLeft;
-
- private float mTop;
-
- private float mScale;
-
- private int mMeasuredMovieWidth;
-
- private int mMeasuredMovieHeight;
-
- private boolean mVisible = true;
-
- private volatile boolean mPaused = false;
-
- public GifView(Context context) {
- this(context, null);
- }
-
- public GifView(Context context, AttributeSet attrs) {
- this(context, attrs, R.styleable.CustomTheme_gifViewStyle);
- }
-
- public GifView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- setViewAttributes(context, attrs, defStyle);
- }
-
- @SuppressLint("NewApi")
- private void setViewAttributes(Context context, AttributeSet attrs,
- int defStyle) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- setLayerType(View.LAYER_TYPE_SOFTWARE, null);
- }
-
- final TypedArray array = context.obtainStyledAttributes(attrs,
- R.styleable.GifView, defStyle, R.style.Widget_GifView);
- mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
- mPaused = array.getBoolean(R.styleable.GifView_paused, false);
- array.recycle();
- if (mMovieResourceId != -1) {
- mMovie = Movie.decodeStream(getResources().openRawResource(
- mMovieResourceId));
- }
- }
-
-
- public void setMovieResource(int movieResId) {
- this.mMovieResourceId = movieResId;
- mMovie = Movie.decodeStream(getResources().openRawResource(
- mMovieResourceId));
- requestLayout();
- }
-
- public void setMovie(Movie movie) {
- this.mMovie = movie;
- requestLayout();
- }
-
- public Movie getMovie() {
- return mMovie;
- }
-
- public void setMovieTime(int time) {
- mCurrentAnimationTime = time;
- invalidate();
- }
-
-
- public void setPaused(boolean paused) {
- this.mPaused = paused;
- if (!paused) {
- mMovieStart = android.os.SystemClock.uptimeMillis()
- - mCurrentAnimationTime;
- }
- invalidate();
- }
-
-
- public boolean isPaused() {
- return this.mPaused;
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- if (mMovie != null) {
- int movieWidth = mMovie.width();
- int movieHeight = mMovie.height();
- int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
- float scaleW = (float) movieWidth / (float) maximumWidth;
- mScale = 1f / scaleW;
- mMeasuredMovieWidth = maximumWidth;
- mMeasuredMovieHeight = (int) (movieHeight * mScale);
- setMeasuredDimension(mMeasuredMovieWidth, mMeasuredMovieHeight);
- } else {
- setMeasuredDimension(getSuggestedMinimumWidth(),
- getSuggestedMinimumHeight());
- }
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- mLeft = (getWidth() - mMeasuredMovieWidth) / 2f;
- mTop = (getHeight() - mMeasuredMovieHeight) / 2f;
- mVisible = getVisibility() == View.VISIBLE;
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- if (mMovie != null) {
- if (!mPaused) {
- updateAnimationTime();
- drawMovieFrame(canvas);
- invalidateView();
- } else {
- drawMovieFrame(canvas);
- }
- }
- }
-
- @SuppressLint("NewApi")
- private void invalidateView() {
- if (mVisible) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- postInvalidateOnAnimation();
- } else {
- invalidate();
- }
- }
- }
-
- private void updateAnimationTime() {
- long now = android.os.SystemClock.uptimeMillis();
-
- if (mMovieStart == 0) {
- mMovieStart = now;
- }
-
- int dur = mMovie.duration();
- if (dur == 0) {
- dur = DEFAULT_MOVIE_DURATION;
- }
-
- mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
- }
-
- private void drawMovieFrame(Canvas canvas) {
-
- mMovie.setTime(mCurrentAnimationTime);
- canvas.save(Canvas.MATRIX_SAVE_FLAG);
- canvas.scale(mScale, mScale);
- mMovie.draw(canvas, mLeft / mScale, mTop / mScale);
- canvas.restore();
- }
-
- @SuppressLint("NewApi")
- @Override
- public void onScreenStateChanged(int screenState) {
- super.onScreenStateChanged(screenState);
- mVisible = screenState == SCREEN_STATE_ON;
- invalidateView();
- }
-
- @SuppressLint("NewApi")
- @Override
- protected void onVisibilityChanged(View changedView, int visibility) {
- super.onVisibilityChanged(changedView, visibility);
- mVisible = visibility == View.VISIBLE;
- invalidateView();
- }
-
- @Override
- protected void onWindowVisibilityChanged(int visibility) {
- super.onWindowVisibilityChanged(visibility);
- mVisible = visibility == View.VISIBLE;
- invalidateView();
- }
-
- }
Movie其實管理着GIF動畫中的多個幀,只須要經過 setTime() 一下就可讓它在draw()的時候繪出相應的那幀圖像。經過當前時間與duration之間的換算關係,是很容易實現GIF動起來的效果。程序員
其次,在xml佈局文件中,把這個view定義進去,代碼以下:github
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <net.loonggg.gif.view.GifView
- android:id="@+id/gif1"
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:layout_gravity="center_horizontal"
- android:enabled="false" />
-
- <net.loonggg.gif.view.GifView
- android:id="@+id/gif2"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:layout_gravity="center_horizontal"
- android:enabled="false" />
-
- </LinearLayout>
最後,在MainActivity中的使用,代碼以下:canvas
- package net.loonggg.gif;
-
- import net.loonggg.gif.view.GifView;
- import android.app.Activity;
- import android.os.Bundle;
-
- public class Gif extends Activity {
- private GifView gif1, gif2;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- gif1 = (GifView) findViewById(R.id.gif1);
-
- gif1.setMovieResource(R.raw.kitty);
- gif2 = (GifView) findViewById(R.id.gif2);
- gif2.setMovieResource(R.raw.b);
-
-
-
- }
-
- }
注意:與ImageView和其餘View惟一的區別在於我加了一個gif屬性。微信
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <declare-styleable name="GifView">
- <attr name="gif" format="reference" />
- <attr name="paused" format="boolean" />
- </declare-styleable>
- <declare-styleable name="CustomTheme">
- <attr name="gifViewStyle" format="reference" />
- </declare-styleable>
-
- </resources>
這個代碼已經很是好了,使用也很是方便,其實不懂代碼是什麼意思也能夠很好的用,只須要懂得我寫註釋的那幾行和Activity裏面的那幾行代碼就能夠了!app
轉載請註明出處:http://blog.csdn.net/loongggdroid/article/details/21166563ide
求源碼,請關注微信訂閱號:smart_android,微信名:非著名程序員,關注成功後,給微信號發送您的郵箱,源碼就會發到您的郵箱裏。發送格式:發送內容+您的郵箱(內容即爲你要的源碼內容)