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
原创粉丝点击