GifView

public class GifView extends View {

   private Movie movie;
   private long mMovieStart;
   private float ratioWidth;
   private float ratioHeight;

   public GifView(Context context) {
      this(context, null);
      // TODO Auto-generated constructor stub
   }

   public GifView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
      // TODO Auto-generated constructor stub
   }

   public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      // TODO Auto-generated constructor stub
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
         // 新api代码块
         setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      }
      // 获取资源属性
      TypedArray ta = context.obtainStyledAttributes(attrs,
            R.styleable.GifViews);
      // 获取资源id
      int resourceId = ta.getResourceId(R.styleable.GifViews_src, -1);
      // 设置gif资源
      setGifResource(context, resourceId);
      // 资源回收
      ta.recycle();

   }

   /**
    * 设置默认gif资源调用,在xml布局中src属性调用
    */
   public void setGifResource(Context context, int resourceId) {
      // Log.i("Simon", "==========:" + resourceId);
      if (resourceId == -1) {
         return;
      }
      // 获取gif动画资源
      InputStream is = context.getResources().openRawResource(resourceId);
      // 解码流转换为movie格式
      movie = Movie.decodeStream(is);
      // 重新布局
      requestLayout();

   }

   /**
    * 设置Gif资源--流Stream
    */
   public void setGifStream(InputStream is) {
      // 解码流转换为movie格式
      movie = Movie.decodeStream(is);
      // 重新布局
      requestLayout();
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

      if (movie != null) {
         int width = movie.width();
         int height = movie.height();
         if (width <= 0) {
            width = 1;
         }
         if (height <= 0) {
            height = 1;
         }
         int paddingLeft = getPaddingLeft();
         int paddingRight = getPaddingRight();
         int paddingTop = getPaddingTop();
         int paddingBottom = getPaddingBottom();

         width += paddingLeft + paddingRight;
         height += paddingTop + paddingBottom;

         width = Math.max(width, getSuggestedMinimumWidth());
         height = Math.max(height, getSuggestedMinimumHeight());

         int widthSize = resolveSizeAndState(width, widthMeasureSpec, 0);
         int heightSize = resolveSizeAndState(height, heightMeasureSpec, 0);

         ratioWidth = (float) widthSize / width;
         ratioHeight = (float) heightSize / height;

         setMeasuredDimension(widthSize, heightSize);

      } else {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }

   }

   @Override
   protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub
      super.onDraw(canvas);
      // 查看上次时间
      long now = android.os.SystemClock.uptimeMillis();
      // 如果当前是第一次播放
      if (mMovieStart == 0) {
         mMovieStart = now;
      }
      if (movie != null) {
         // 获取movie时间长
         int duration = movie.duration();
         if (duration == 0) {
            duration = 1000;
         }
         int relTime = (int) ((now - mMovieStart) % duration);
         movie.setTime(relTime);

         // 最小缩放比率
         float scale = Math.min(ratioWidth, ratioHeight);
         canvas.scale(scale, scale);
         // 从 0 0 开始绘制
         movie.draw(canvas, 0, 0);
         // movie.draw(canvas, getWidth() - movie.width(),
         // getHeight() - movie.height());
         // 重新绘制
         invalidate();

      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值