English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation code of Android loading Gif animation

How to implement Gif animation loading in Android? Everyone is curious, and this article will reveal it, as follows

<?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> 

Main Interface

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.gif)1); 
 // Set background gif image resource 
 gif1.setMovieResource(R.raw.red); 
 } 
} 

Custom 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 { 
 /** 
 * default is1seconds 
 */ 
 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; 
 /** 
 * Constructor 
 */ 
 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); 
 } 
 // Read the gif value from the description file and create a Movie instance 
 final TypedArray array = context.obtainStyledAttributes(attrs, 
  
 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)); 
 } 
 } 
 /** 
 * Set gif image resource 
 */ 
 public void setMovieResource(int movieResId) { 
  
 mMovie = Movie.decodeStream(getResources().openRawResource( 
 mMovieResourceId)); 
 requestLayout(); 
 } 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 if (mMovie != null) { 
 //GIF animation width and height 
 int movieWidth = mMovie.width(); 
 int movieHeight = mMovie.height(); 
 //Control width 
 int maximumWidth = MeasureSpec.getSize(widthMeasureSpec); 
 //GIF image width/Control width 
 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 the first frame, record the start time 
 if (mMovieStart == 0) { 
 mMovieStart = now; 
 } 
 // Get the duration of the animation 
 int dur = mMovie.duration(); 
 if (dur == 0) { 
 dur = DEFAULT_MOVIE_DURATION; 
 } 
 // Calculate which frame to display 
 mCurrentAnimationTime = (int) ((now - mMovieStart) % dur); 
 } 
 private void drawMovieFrame(Canvas canvas) { 
 // Set the frame to be displayed and draw it 
 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(); 
 } 
 } 
 } 
 // --------------------The following method is not called------------------------------------/ 
 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(); 
 } 
 // --------------------------------------------------------/ 
} 

Source Code Download:http://xiazai.jb51.net/201610/yuanma/AndroidGifDemo(jb51.net).rar

That's all for this article. Hope it will be helpful to everyone's study, and also hope everyone will support and cheer for the tutorial.

Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like