本文实例为大家分享AndroID通过MovIE展示Gif格式图片的相关代码,供大家参考,具体内容如下
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,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设置gif格式图片背景 * @description: * @author ldm * @date 2016-2-18 上午9:21:16 */ private voID setGifVIEwBg(int src_ID) { if (src_ID == -1) { return; } // 获取对应资源文件的输入流 inputStream is = mResources.openRawResource(src_ID); mMovIE = MovIE.decodeStream(is);// 解码输入流为MovIE对象 requestLayout(); } /* * 这个方法供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) { // 如果第一帧,记录起始时间 startTime = Now; } if (mMovIE != null) {// 如果返回值不等于null,就说明这是一个gif图片 int duration = mMovIE.duration();// 取出动画的时长 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); invalIDate(); } } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { if (mMovIE != null) {// 如果返回值不等于null,就说明这是一个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);//根据你提供的大小和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(); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android通过Movie展示Gif格式图片全部内容,希望文章能够帮你解决Android通过Movie展示Gif格式图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)