GifView的应用
来源:互联网 发布:淘宝3c认证编号 编辑:程序博客网 时间:2024/06/07 23:35
自定义GIFView的用法:
首先先来看看别人提供的自定义GIFView的用法:
1、导入GIFView的jar包:地址:http://download.csdn.net/detail/weiwozhiyi/9396922
2、在布局文件写上:
<com.ant.liao.GifView android:id="@+id/gifview1" android:layout_width="300dp" android:layout_height="200dp" android:layout_marginTop="10dp" android:background="#888"/>
然后在Activity生成GIFView对象
gifview1 = (GifView) findViewById(R.id.gifview1);gifview1.setShowDimension(600, 200);//设置GIFView中的图片显示大小gifview1.setGifImage(R.drawable.boy);//设置GifView显示的资源
另外还能控制GIFView的播放和暂停,相应的方法为:
gifview1.showAnimation();//播放gifview1.showCover();//暂停
其它的属性可以自己来尝试。
接下来来书写自己的自定义GIFView:
1、在style.xml来声明属性,用来在布局文件中引用:
<declare-styleable name="gifview"> <attr name="src" format="reference"/><--! 声明属性为src 格式为引用类型 !--></declare-styleable>
2、定义GIFView继承View,构造方法:
public GifView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); resources = context.getResources(); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.gifview); int id = ta.getResourceId(R.styleable.gifview_src, -1);//得到在xml中资源,如果不存在则为-1 setGifViewResouce(id);//为GIFView设置资源 ta.recycle();//释放}
相应的方法:
private void setGifViewResouce(int id) { if (id == -1) { return; } InputStream input = resources.openRawResource(id); movie = Movie.decodeStream(input); requestLayout();//重新进行布局}
3、接下来只需要Movie类结合onDraw方法来将动态的动画绘画出来
@Overrideprotected void onDraw(Canvas canvas) { super.onDraw(canvas); long now = android.os.SystemClock.uptimeMillis();//定义一个递增的数值 if (mMovieStart == 0) { mMovieStart = now; } if (movie != null) { int duration = movie.duration();//得到gif图片的时长 if (duration == 0) { duration = 1000; } int relTime = (int) ((now - mMovieStart) % duration); movie.setTime(relTime);//设置Movie的时间,通过传入瞬时时间来画出该时间下的图像 movie.draw(canvas, getWidth() - movie.width(), getHeight() - movie.height());//将gif图片设置到控件的右下角 invalidate();//对控件进行重绘,在UI线程用invalidate()如果在子线程用postInvalidate()方法 }}
4、布局文件(要引入在1、中定义的属性需要在根布局写上AndroidStudio:xmlns:gif="http://schemas.android.com/apk/res-auto" Eclipse:xmlns:gif="http://schemas.android.com/apk/res/应用的包名")
<com.example.administrator.gifviewapp.GifView android:id="@+id/gifview" android:layout_width="300dp" android:layout_height="200dp" android:background="#888" gif:src="@drawable/boy"/>
在此就能显示一个GIFView了
然后并不能很好的进行显示,为此需要对Gif图片进行缩放,为了得到缩放比只需要重写onMessure方法
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(movie != null){ int w = movie.width(); if (w <= 0) { w = 1; } int h = movie.height(); if (h <= 0) { h = 1; } int pleft = getPaddingLeft(); int pright = getPaddingRight(); int ptop = getPaddingTop(); int pbottom = getPaddingBottom(); int widthSize; int heightSize; w += pleft + pright; h += ptop + pbottom; w = Math.max(w, getSuggestedMinimumWidth());//子类应确保测量的height和width至少是view的最小高度和宽度 h = Math.max(h, getSuggestedMinimumHeight()); widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);//用来创建最终的宽和高 heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); scaleW = (float)widthSize/w; scaleH = (float)heightSize/h; setMeasuredDimension(widthSize, heightSize); }else{ setMeasuredDimension(widthMeasureSpec,heightMeasureSpec); }}
然后更改onDraw方法中的
movie.draw(canvas, getWidth() - movie.width(), getHeight() - movie.height());
改为:
float scale = Math.min(scaleW,scaleH);
canvas.scale(scale,scale);
movie.draw(canvas, 0, 0);
0 0
- GifView的应用
- android中开源的gifview
- Gifview的使用
- Android GifView 的用法
- GifView的使用
- GifView的使用
- GifView
- Android中gifview的使用
- PhotoView与GifView的使用
- android开源GifView的使用
- Gif动态图片库--GifView的基本使用
- 安卓的GifView实现动画效果,GifView背景为黑色问题
- iOS 第三方控件GIFView的学习记录
- GifView——Android显示GIF动画的扩展控件
- Android中GIF图的使用(GifView)
- Android中第三方控件gifView的基本使用
- 图片缩放库 Photoview 和 Gif 控件 GifView 的使用
- 简述GifView的配置显示gif动态图片
- 遇到问题时不要乱了方寸
- Xen中DMA传输解析
- Apache设置禁止访问网站目录
- 使用编辑器sublime text搭建SV IDE
- Android5.0新特性 -- Material Design材料设计(1)
- GifView的应用
- Spark入门实战系列 (做个标签,转载基于 ELK Stack 和 Spark Streaming 的日志处理平台设计与实现)
- 启动 netconsole 将内核消息发送到远程 syslog 服务器
- 3D打印模型打磨、上色和拼接
- 802.11 a/b/g/n/ac 是什么以及它们有什么区别
- 程序员的成长和代码行数的关系
- bzoj3998 弦论
- R 语言绘图006-绘图参数设置函数par()详解
- 3714: [PA2014]Kuglarz 思路题 最小生成树