Android 顯示gif動畫
阿新 • • 發佈:2019-02-16
在這裡主要用的是:android中的android.graphics.Movie 這個類,這是android提供給我們的一個非常方便的工具。
首先,重寫控制元件View,自定義一個展示gif圖的GifView,程式碼如下:
- 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;
- publicclass GifView extends View {
- /**
- * 預設為1秒
- */
- privatestaticfinalint DEFAULT_MOVIE_DURATION = 1000;
-
private
- private Movie mMovie;
- privatelong mMovieStart;
- privateint mCurrentAnimationTime = 0;
- privatefloat mLeft;
- privatefloat mTop;
- privatefloat mScale;
- privateint mMeasuredMovieWidth;
- privateint mMeasuredMovieHeight;
-
private
- privatevolatileboolean 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")
- privatevoid setViewAttributes(Context context, AttributeSet attrs,
- int defStyle) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- setLayerType(View.LAYER_TYPE_SOFTWARE, null);
- }
- // 從描述檔案中讀出gif的值,創建出Movie例項
- 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));
- }
- }
- /**
- * 設定gif圖資源
- *
- * @param movieResId
- */
- publicvoid setMovieResource(int movieResId) {
- this.mMovieResourceId = movieResId;
- mMovie = Movie.decodeStream(getResources().openRawResource(
- mMovieResourceId));
- requestLayout();
- }
- publicvoid setMovie(Movie movie) {
- this.mMovie = movie;
- requestLayout();
- }
- public Movie getMovie() {
- return mMovie;
- }
- publicvoid setMovieTime(int time) {
- mCurrentAnimationTime = time;
- invalidate();
- }
- /**
- * 設定暫停
- *
- * @param paused
- */
- publicvoid setPaused(boolean paused) {
- this.mPaused = paused;
- if (!paused) {
- mMovieStart = android.os.SystemClock.uptimeMillis()
- - mCurrentAnimationTime;
- }
- invalidate();
- }
- /**
- * 判斷gif圖是否停止了
- *
- * @return
- */
- publicboolean isPaused() {
- returnthis.mPaused;
- }
- @Override
- protectedvoid 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;