Android开发中加载Gif动画图片方法
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android开发中加载Gif动画图片方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                Gif動畫,我使用了三種方式
1.自定義控件加載gif動畫
①.首先自定義一個GifView,用于顯示Gif圖片,具體代碼如下:
public class GifView extends View {private Resources resources;private Movie mMovie;private long mMovieStart;private float ratioWidth;private float ratioHeight;public GifView(Context context) {this(context,null);}public GifView(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public GifView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {setLayerType(View.LAYER_TYPE_SOFTWARE, null);}resources = context.getResources();TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.GifView);int resourceId = ta.getResourceId(R.styleable.GifView_src, -1);setGifResource(resourceId);ta.recycle();}public void setGifResource(int resourceId) {if (resourceId==-1){return;}InputStream is = resources.openRawResource(resourceId);mMovie = Movie.decodeStream(is);requestLayout();}public void setGifStream(InputStream is){mMovie = Movie.decodeStream(is);requestLayout();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (mMovie!=null){int w = mMovie.width();int h = mMovie.height();if (w<=0){w=1;}if (h<=0){h=1;}int pLeft = getPaddingLeft();int pRight = getPaddingRight();int pTop = getPaddingTop();int pBottom = getPaddingBottom();int widthSize;int heightSize;w+=pLeft+pRight;h+=pTop+pBottom;w=Math.max(w,getSuggestedMinimumWidth());h=Math.max(h,getSuggestedMinimumHeight());widthSize= resolveSizeAndState(w,widthMeasureSpec,0);heightSize= resolveSizeAndState(h,heightMeasureSpec,0);ratioWidth = (float) widthSize/w;ratioHeight = (float) heightSize/h;setMeasuredDimension(widthSize,heightSize);}else{super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);long now = SystemClock.uptimeMillis();if (mMovieStart ==0){ //第一次進入mMovieStart =now;}if (mMovie!=null){int dur = mMovie.duration();if (dur==0){dur=1000;}int relTime= (int) ((now-mMovieStart)%dur);mMovie.setTime(relTime);// mMovie.draw(canvas,0,0);float scale=Math.min(ratioWidth,ratioHeight);canvas.scale(scale,scale);mMovie.draw(canvas,0,0);invalidate();}} }②.在布局文件中添加自定義的控件,我這加了兩個,第一個展示assets文件中的gif圖片,一個展示drawable中的gif圖片
<com.example.wen.adroid.view.GifViewandroid:id="@+id/gv_photo"android:layout_width="match_parent"android:layout_weight="1"android:layout_gravity="center"android:layout_height="0dp"/><com.example.wen.adroid.view.GifViewandroid:id="@+id/gv_local_photo"android:layout_width="match_parent"android:layout_weight="1"android:layout_gravity="center"android:layout_height="0dp"app:src="@drawable/gifphoto"/>③.展示gif圖片,具體代碼如下:
//方法一 使用自定義gifview播放gif圖片mGvLocalPhoto = (GifView) findViewById(R.id.gv_local_photo);mGvPhoto = (GifView) findViewById(R.id.gv_photo);try {InputStream is = getAssets().open("assetphoto.gif");mGvPhoto.setGifStream(is);} catch (IOException e) {e.printStackTrace();}2.使用Glide加載gif動畫
Glide 地址: https://github.com/bumptech/glide
①.首先配置build.gradle
compile 'com.github.bumptech.glide:glide:3.7.0'②.配置好Glide之后,然后通過基本的用法去加載,加載代碼如下:
// 使用Glide播放gif圖片mIvPhoto = (ImageView) findViewById(R.id.iv_photo);Glide.with(this).load(R.drawable.gifphoto).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mIvPhoto);注意:diskCacheStrategy是為其添加緩存策略,其中緩存策略可以為:Source及None,None及為不緩存,Source緩存原型。
3.使用android-gif-drawable加載gif動畫
android-gif-drawable 地址:https://github.com/koral--/android-gif-drawable
①.首先配置build.gradle
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'②.在xml中添加GifImageView控件
<pl.droidsonroids.gif.GifImageViewandroid:id="@+id/giv_photo"android:layout_width="match_parent"android:layout_weight="1"android:layout_height="0dp"android:src="@drawable/gifphoto"/>③.調(diào)用android-gif-drawable
//使用android-gif-drawable 庫GifImageView mGifIvPhoto = (GifImageView) findViewById(R.id.giv_photo);try {//加載asset文件中的gif圖片GifDrawable gif = new GifDrawable(getAssets(), "assetphoto.gif");mGifIvPhoto.setImageDrawable(gif);} catch (IOException e) {e.printStackTrace();}總結(jié)
以上是生活随笔為你收集整理的Android开发中加载Gif动画图片方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 树莓派Raspbian Buster/D
- 下一篇: 英文版opensuse 12.2安装中文
