Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android实现可播放GIF动画的ImageView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Android的原生控件并不支持播放GIF格式的图片。我们都知道,在Android中如果想要显示一张图片,可以借助ImageView来完成,但是如果将一张gif图片设置到ImageView里,它只会显示这张图片的第一帧,不会产生任何的动画效果。今天我们来编写一个自定义的增强型ImageView(继承ImageView),可以播放GIF格式的图片,暂且叫做GifImageView吧。

1.自定义属性

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <declare-styleable name="GifImageView"> 
  <attr name="auto_play" format="Boolean"></attr> 
 </declare-styleable> 
</resources>

 2.自定义view获取属性

 private Movie mMovie;//播放动画需要用到的,系统类
 privatE int mImageWidth;//动画的imageview的宽度
 privatE int mImageHeight;//动画imageview的高度
 private long mMovieStart = 0;// 播放开始
 private Boolean isAutoPlay;//是否自动播放
 private Bitmap mStartPlay;//开始按钮
 private Boolean isPlaying=false;//记录是否正在播放
 private float mScale;//图片的缩放比
 privatE int mMeasuredGifWidth;//缩放后图片宽
 privatE int mMeasuredGifHeight;//缩放后图片高
 ...

 private void init(Context context,AttributeSet attrs) {
 TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.GifImageView);
 // 通过反射拿布局中src的资源id,所以gif文件需要放在布局的src中
 int resourcEID = getresourcEID(attributes,context,attrs);
 if (resourcEID != 0) {
  // 说明是gif动画
  // 1.将resourcesId变成流
  // 2.用Move来decode解析流
  // 3.获得bitmap的长宽
  InputStream is = getresources().openRawresource(resourcEID);
  mMovie = Movie.decodeStream(is);
  if (mMovie != null) {
  Bitmap bitmap = BitmapFactory.decodeStream(is);
  mImageWidth = bitmap.getWidth();
  mImageHeight = bitmap.getHeight();
  // 用完释放
  bitmap.recycle();
  // 获得是否允许自动播放,如果不允许自动播放,则初始化播放按钮
  isAutoPlay = attributes.getBoolean(R.styleable.GifImageView_auto_play,falsE);
  if (!isAutoPlay) {
   mStartPlay = BitmapFactory.decoderesource(getresources(),R.drawable.start_play);
   setOnClickListener(this);
  }  
  }
 }
 //回收资源
 attributes.recycle();
 }

 /**
 * 通过反射拿布局中src的资源id
 * 
 * @param attrs
 * @param context
 * @param attributes
 */
 privatE int getresourcEID(TypedArray attributes,Context context,AttributeSet attrs) {
 try {
  Field filed = TypedArray.class.getDeclaredField("mValue");
  filed.setAccessible(true);
  TypedValue typeValue = (Typedvalue) filed.get(attributes);
  return typeValue.resourcEID;
 } catch (Exception E) {
  e.printStackTrace();
 }
 }
 return 0;
}

3.重写onMesure()

 @Override
 protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) {
 super.onMeasure(widthMeasureSpec,heightMeasureSpec);
 if (mMovie != null) {
  /*
   * Calculate horizontal scaling
   */
  float scaleW = 1f;
  int measureModeWidth = MeasureSpec.getMode(widthMeasureSpec);
  if (measureModeWidth != MeasureSpec.UNSPECIFIED) {
   int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
   scaleW = (float) mImageWidth / (float) maximumWidth;
  }
  /*
   * calculate vertical scaling
   */
  float scaleH = 1f;
  int measureModeHeight = MeasureSpec.getMode(heightMeasureSpec);
  if (measureModeHeight != MeasureSpec.UNSPECIFIED) {
   int maximumHeight = MeasureSpec.getSize(heightMeasureSpec);
   scaleH = (float) mImageHeight / (float) maximumHeight;
  }
  /*
   * calculate overall scale
   */
  mScale = 1f / Math.max(scaleH,scaleW);
  mMeasuredGifWidth = (int) (mImageWidth * mScalE);
  mMeasuredGifHeight = (int) (mImageHeight * mScalE);
  setMeasuredDimension(mMeasuredGifWidth,mMeasuredGifHeight);
 }
 }

4.重写onDraw()

 @Override
 protected void onDraw(Canvas canvas) {
 if (mMovie == null) {
  // mMovie等于null,说明是张普通的图片,则直接调用父类的onDraw()方法
  super.onDraw(canvas);
 } else {
  // mMovie不等于null,说明是张gif图片
  if (isAutoPlay) {
  // 如果允许自动播放,就播放
  playmovie(canvas);
  invalidate();
  } else {
  // 不允许自动播放的话
  // 1.判断是否正在播放
  // 2.获得第一帧的图像
  // 3.然后添加播放按钮
  if (isPlaying) {
   // 如果正在播放就playmoive继续播放
   if (playmovie(canvas)) {
   isPlaying = false;
   }
   invalidate();
  } else {
   // 第一帧
   mMovie.setTime(0);
   canvas.save(Canvas.MATRIX_SAVE_FLAG);
   canvas.scale(mScale,mScalE);
   mMovie.draw(canvas,0);// 画
   canvas.restore();
   // 绘制开始按钮
   int offsetW = (mMeasuredGifWidth - mStartPlay.getWidth()) / 2;
   int offsetH = (mMeasuredGifHeight - mStartPlay.getHeight()) / 2;
   canvas.drawBitmap(mStartPlay,offsetW,offsetH,null);
  }
  }
 }
 }

 /**
 * 播放gif动画
 * 
 * @param canvas
 */
 private Boolean playmovie(Canvas canvas) {
 // 1.获取播放的时间
 // 2.如果开始start=0,则认为是开始
 // 3.记录播放的时间
 // 4.设置进度
 // 5.画动画
 // 6.如果时间大于了播放的时间,则证明结束
 long Now = SystemClock.uptimeMillis();
 if (mMovieStart == 0) {
  mMovieStart = Now;
 }
 int duration = mMovie.duration();
 if (duration == 0) {
  duration = 1000;
 }
 //记录gif播放了多少时间
 int relTime = (int) ((Now - mMovieStart) % duration);
 mMovie.setTime(relTimE);// 设置时间
 canvas.save(Canvas.MATRIX_SAVE_FLAG);
 canvas.scale(mScale,mScalE);
 mMovie.draw(canvas,0);// 画
 canvas.restore();
 if ((Now - mMovieStart) >= duration) {
  // 结束
  mMovieStart = 0;
  return true;
 }
 return false;
 }

5.添加点击事件

 @Override
 public void onClick(View v) {
 if(v.getId()==getId()){
  isPlaying=true;
  invalidate();
 }
 }

还有一点@R_696_10916@,有些4.0以上系统的手机启动了硬件加速功能之后会导致GIF动画播放不出来,因此我们需要在AndroidManifest.xml中去禁用硬件加速功能,可以通过指定android:hardwareAccelerated=false来完成。

--------------------------------------------------------------------------------

现在我们来看看运行后效果如何吧,
布局文件

<LinearLayout xmlns:android="http://scheR_627_11845@as.android.com/apk/res/android"
 xmlns:tools="http://scheR_627_11845@as.android.com/tools"
 xmlns:attr="http://scheR_627_11845@as.android.com/apk/res/com.hx.gifimageview"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <com.hx.gifimageview.GifImageView
  android:layout_width="150dip"
  android:layout_height="150dip"
  android:layout_margin="10dp"
  android:src="@drawable/shulan"
  attr:auto_play="false" />

 <com.hx.gifimageview.GifImageView
  android:layout_width="150dip"
  android:layout_height="150dip"
  android:layout_margin="10dp"
  android:src="@drawable/shulan"
  attr:auto_play="true" />

 <com.hx.gifimageview.GifImageView
  android:layout_width="150dip"
  android:layout_height="150dip"
  android:layout_margin="10dp"
  android:src="@drawable/jingtai"
  attr:auto_play="true" />
</LinearLayout>

 

Android实现可播放GIF动画的ImageView


图一的auto_play属性false,会显示第一帧和一个播放按钮,点击后gif图片会自动运行。
图二的auto_play属性为true,会自动循环播放Gif。
图三我们给的资源是静态图片,因为自定义view继承ImageView,所以具备ImageView所有特性,因此也能显示静态图片

源码下载:http://xiazai.code.net/201609/yuanma/GifImageView(code.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android实现可播放GIF动画的ImageView全部内容,希望文章能够帮你解决Android实现可播放GIF动画的ImageView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: