我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的个GIF动画完美实现 源码 百度搜就知道很多下载的地方 我写的东西有可能比较乱,如果单一的篇章没看明白,请看上一篇文章
上篇文章 地址:http://blog.csdn.net/u014737138/article/details/40709035
上篇文章介绍的是如何处理一个GIF动画,我们只是简单的让它在当前的窗口中显示,
我们还可以让它在小窗口中显示,很简单,只需要加个Dialog类就可以了,
先看下效果把:
代码实现: import com.wust.gif.R; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.graphics.Canvas; import android.graphics.Movie; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { public Movie mMovie; public long mMovieStart; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //setContentView(new CustomGifView(this)); Dialog customDialog = new Dialog(this); customDialog.setTitle("GIF"); customDialog.setContentView(new CustomGifView(this)); customDialog.show(); } class CustomGifView extends View { public CustomGifView(Context context) { super(context); mMovie = Movie.decodeStream(getResources().openRawResource( R.drawable.animation)); } public void onDraw(Canvas canvas) { long now = android.os.SystemClock.uptimeMillis(); if (mMovieStart == 0) { // first time 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); invalidate(); } } } }
对比上篇文章的源码,只需要看这里的红色代码,就这样操作。希望对你有用
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有