日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发中加载Gif动画图片方法

發布時間:2023/12/9 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发中加载Gif动画图片方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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"/>

③.調用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();}

總結

以上是生活随笔為你收集整理的Android开发中加载Gif动画图片方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。