Android通過Movie展示Gif格式圖片
本文實(shí)例為大家分享Android通過Movie展示Gif格式圖片的相關(guān)代碼,供大家參考,具體內(nèi)容如下
public class CommonGifView extends View {
private Resources mResources;
private Movie mMovie;
private long startTime = 0;
private float widthRatio;
private float heightRatio;
public CommonGifView(Context context) {
this(context, null);
}
public CommonGifView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CommonGifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mResources = context.getResources();
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_gif_view);
int src_id = ta.getResourceId(R.styleable.custom_gif_view_gif_src, -1);
setGifViewBg(src_id);
ta.recycle();
}
/**
* 為View設(shè)置gif格式圖片背景
* @description:
* @author ldm
* @date 2016-2-18 上午9:21:16
*/
private void setGifViewBg(int src_id) {
if (src_id == -1) { return; }
// 獲取對(duì)應(yīng)資源文件的輸入流
InputStream is = mResources.openRawResource(src_id);
mMovie = Movie.decodeStream(is);// 解碼輸入流為Movie對(duì)象
requestLayout();
}
/*
* 這個(gè)方法供Activity中使用
*/
public void setGifStream(InputStream is) {
mMovie = Movie.decodeStream(is);
requestLayout();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now = SystemClock.uptimeMillis();
if (startTime == 0) { // 如果第一幀,記錄起始時(shí)間
startTime = now;
}
if (mMovie != null) {// 如果返回值不等于null,就說明這是一個(gè)GIF圖片
int duration = mMovie.duration();// 取出動(dòng)畫的時(shí)長(zhǎng)
if (duration == 0) {
duration = 1000;
}
int currentTime = (int) ((now - startTime) % duration);// 算出需要顯示第幾幀
mMovie.setTime(currentTime);
// mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
float scale = Math.min(widthRatio, heightRatio);
canvas.scale(scale, scale);
mMovie.draw(canvas, 0, 0);
invalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMovie != null) {// 如果返回值不等于null,就說明這是一個(gè)GIF圖片
int w = mMovie.width();//寬度
int h = mMovie.height();//高度
if (w <= 0) {
w = 1;
}
if (h <= 0) {
h = 1;
}
int left = getPaddingLeft();
int right = getPaddingRight();
int top = getPaddingTop();
int bottom = getPaddingBottom();
int widthSize, heightSize;
w += left + right;
h += top + bottom;
w = Math.max(w, getSuggestedMinimumWidth());
h = Math.max(h, getSuggestedMinimumHeight());
widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);//根據(jù)你提供的大小和MeasureSpec,返回你想要的大小值
heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
widthRatio = (float) widthSize / w;
heightRatio = (float) heightSize / h;
setMeasuredDimension(widthSize, heightSize);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
自定義屬性res/values/attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_gif_view">
<attr name="gif_src" format="reference"></attr>
</declare-styleable>
</resources>
在Activity中使用:
public class MainActivity extends Activity {
private CommonGifView view;
private InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (CommonGifView) findViewById(R.id.gif_test);
try {
is = getAssets().open("test01.gif");
view.setGifStream(is);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Flutter Reusable Lottie Animations技巧
這篇文章主要為大家介紹了Flutter Reusable Lottie Animations技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android實(shí)現(xiàn)退出時(shí)關(guān)閉所有Activity的方法
這篇文章主要介紹了Android實(shí)現(xiàn)退出時(shí)關(guān)閉所有Activity的方法,主要通過自定義類CloseActivityClass實(shí)現(xiàn)這一功能,需要的朋友可以參考下2014-09-09
使用RecyclerView實(shí)現(xiàn)水平列表
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實(shí)現(xiàn)水平列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android實(shí)現(xiàn)阿里云oss上傳流程解析
這篇文章主要介紹了Android實(shí)現(xiàn)阿里云oss上傳流程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Android與H5交互產(chǎn)生Script Error踩坑解決
這篇文章主要為大家介紹了Android與H5交互產(chǎn)生Script Error問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼
這篇文章主要介紹了Android之仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧2018-05-05

