編輯:關於Android編程
Android加載Gif動畫如何實現?相信大家都很好奇,本文就為大家揭曉,內容如下
<?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" > <com.example.gifdemo.GifView android:id="@+id/gif1" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:enabled="false" /> </LinearLayout>
<declare-styleable name="GifView"> <attr name="gif" format="reference" /> <attr name="paused" format="boolean" /> </declare-styleable>
主界面
package com.example.gifdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { private GifView gif1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gif1 = (GifView) findViewById(R.id.gif1); // 設置背景gif圖片資源 gif1.setMovieResource(R.raw.red); } }
自定義view
package com.example.gifdemo; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Movie; import android.os.Build; import android.util.AttributeSet; import android.view.View; public class GifView extends View { /** * 默認為1秒 */ 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,0); } public GifView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setViewAttributes(context, attrs, defStyle); setBackgroundColor(Color.parseColor("#FFB6C1")); } @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); } // 從描述文件中讀出gif的值,創建出Movie實例 final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.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圖資源 */ public void setMovieResource(int movieResId) { this.mMovieResourceId = movieResId; mMovie = Movie.decodeStream(getResources().openRawResource( mMovieResourceId)); requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mMovie != null) { //gif動畫的寬度、高度 int movieWidth = mMovie.width(); int movieHeight = mMovie.height(); //控件的寬度 int maximumWidth = MeasureSpec.getSize(widthMeasureSpec); //gif圖片寬/控件寬 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); } } } 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") private void invalidateView() { if (mVisible) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { postInvalidateOnAnimation(); } else { invalidate(); } } } // --------------------以下方法未調用------------------------------------/ 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; } @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(); } // --------------------------------------------------------/ }
源碼下載:http://xiazai.jb51.net/201610/yuanma/AndroidGifDemo(jb51.net).rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
1.import android.app.Activity;import android.content.Context;import android.content.r
GCC編譯器介紹GCC編譯器(GNG C Compiler)是GNU項目中符合ANSI C標准的編譯系統,能夠編譯C C++ Object C等語言編寫的程序,同時G
StepView(步驟流程的view)效果圖如下:該控件可以支持這種步驟的操作view展示,像快遞app的流程。1.首先有在xml布局: 2 .一個自定義的Hori
下面代碼設計了異步任務、JSon解析、自定義組件、IO流、文件下載、適配器原理等知識點。 代碼實現從服務器上讀取Json字符串。 Json字符串如下。 {object:{