package com.terry.gif;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;publicclassTypegifView extends View implements Runnable {
gifOpenHelper gHelper;privateboolean isStop=true;intdelta;
String title;
Bitmap bmp;//construct - refer for javapublicTypegifView(Context context) {this(context,null);
}//construct - refer for xmlpublicTypegifView(Context context, AttributeSet attrs) {
super(context, attrs);//添加属性TypedArray ta=context.obtainStyledAttributes(attrs,
R.styleable.gifView);intn=ta.getIndexCount();for(inti=0; i
setSrc(id);break;caseR.styleable.gifView_delay:intidelta=ta.getInteger(R.styleable.gifView_delay,1);
setDelta(idelta);break;caseR.styleable.gifView_stop:
boolean sp=ta.getBoolean(R.styleable.gifView_stop,false);if(!sp) {
setStop();
}break;
}
}
ta.recycle();
}/**
* 设置停止
*
* @param stop*/publicvoidsetStop() {
isStop=false;
}/**
* 设置启动*/publicvoidsetStart() {
isStop=true;
Thread updateTimer=newThread(this);
updateTimer.start();
}/**
* 通过下票设置第几张图片显示
* @param id*/publicvoidsetSrc(intid) {
gHelper=newgifOpenHelper();
gHelper.read(TypegifView.this.getResources().openRawResource(id));
bmp=gHelper.getImage();//得到第一张图片}publicvoidsetDelta(intis) {
delta=is;
}//to meaure its Width & Height@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}privateintmeasureWidth(intmeasureSpec) {returngHelper.getWidth();
}privateintmeasureHeight(intmeasureSpec) {returngHelper.getHeigh();
}protectedvoidonDraw(Canvas canvas) {//TODO Auto-generated method stubcanvas.drawBitmap(bmp,0,0,newPaint());
bmp=gHelper.nextBitmap();
}publicvoidrun() {//TODO Auto-generated method stubwhile(isStop) {try{this.postInvalidate();
Thread.sleep(gHelper.nextDelay()/delta);
}catch(Exception ex) {
}
}
}
}