GifView
来源:互联网 发布:珍珠读屏软件 编辑:程序博客网 时间:2024/06/01 10:53
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
- GifView
- android GifView
- android中开源的gifview
- GifView显示gif图片
- Android开发之-GifView
- 自定义控件-GifView
- Android开发之-GifView
- android GifView分享
- Gifview的使用
- GifView显示gif图
- Android GifView 的用法
- GifView的应用
- 图片控件 - GifView
- 开源控件GifView
- Android GifView控件简述
- GifView的使用
- GifView 详细介绍
- GifView的使用
- Impala与Hive的比较
- 数据库视图
- 学习中遇到的几种上下文 (后期慢慢积累)
- 自定义控件三实现方法
- 关于页面显示带入其他页面的元素
- GifView
- 如何让Oracle客户端显示中文
- postgresql更新序列的起始值
- sql中sum()和count()的区别
- 我们应该知道的java位运算
- Activiti源码浅析:Activiti的活动授权机制
- [bzoj3879]SvT
- ZZULI 1863: 神の数【模拟】
- VS 2013 中MFC程序编译过程出现 RC2108错误