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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android中播放gif动画之二

發布時間:2025/3/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中播放gif动画之二 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

在上一篇,是使用代碼通過構造方法傳入要播放的gif動畫的id進行獲取的。本文進一步改造,讓GifView和ImageView一樣可以在布局文件中設置src屬性,并在GifView中我們使用反射動態獲取src的圖片資源ID,然后對它進行播放。這樣使用起來就靈活多了。代碼如下:

GifView:

package com.home.gifview;import java.io.InputStream; import java.lang.reflect.Field; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Movie; import android.os.SystemClock; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.ImageView;public class GifView extends ImageView {private Movie movie;private long movieStart;private int imageWidth;private int imageHeight;public GifView(Context context) {super(context);}public GifView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public GifView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.GifView);int resourceId = getResourceId(a, context, attrs);if (resourceId != 0) {InputStream is = getResources().openRawResource(resourceId);movie = Movie.decodeStream(is);if (movie != null) {Bitmap bitmap = BitmapFactory.decodeStream(is);imageWidth = bitmap.getWidth();imageHeight = bitmap.getHeight();bitmap.recycle();}}}@Overrideprotected void onDraw(Canvas canvas) {if (movie == null) {// 普通圖片則直接調用父類的onDraw()方法super.onDraw(canvas);} else {playGif(canvas);invalidate();}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (movie != null) {// 如果是gif圖片則定制GifView的大小setMeasuredDimension(imageWidth, imageHeight);}}/*** 播放gif動畫* * @param canvas* @return*/private boolean playGif(Canvas canvas) {long now = SystemClock.uptimeMillis();if (movieStart == 0) {movieStart = now;}int duration = movie.duration();if (duration == 0) {duration = 1000;}int relTime = (int) ((now - movieStart) % duration);movie.setTime(relTime);movie.draw(canvas, 0, 0);if ((now - movieStart) >= duration) {movieStart = 0;return true;}return false;}/*** 使用反射,獲取到src指定圖片資源所對應的id。* * @param a* @param context* @param attrs* @return*/private int getResourceId(TypedArray a, Context context, AttributeSet attrs) {try {Field field = TypedArray.class.getDeclaredField("mValue");field.setAccessible(true);TypedValue typedValueObject = (TypedValue) field.get(a);return typedValueObject.resourceId;} catch (Exception e) {e.printStackTrace();} finally {if (a != null) {a.recycle();}}return 0;} }

attrs.xml:

<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="GifView"></declare-styleable></resources>

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><com.home.gifview.GifViewandroid:id="@+id/main_gifView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/loading" /></RelativeLayout>

禁用硬件加速:

android:hardwareAccelerated="false"





?

轉載于:https://my.oschina.net/secyaher/blog/274414

總結

以上是生活随笔為你收集整理的android中播放gif动画之二的全部內容,希望文章能夠幫你解決所遇到的問題。

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