public class GifSurface extends SurfaceView implementsCallback{privateMovie movie;private static int ZOOM = 2;publicString path;private Handler handler = newHandler();private Runnable runnable = newRunnable() {
@Overridepublic voidrun() {
Canvas canvas=holder.lockCanvas();
canvas.save();
canvas.scale(ZOOM, ZOOM);
movie.draw(canvas,0, 0);
canvas.restore();
holder.unlockCanvasAndPost(canvas);
movie.setTime((int) (System.currentTimeMillis()%movie.duration()));
handler.removeCallbacks(runnable);
handler.postDelayed(runnable,30);
}
};privateSurfaceHolder holder;publicGifSurface(Context context, AttributeSet attrs) {super(context, attrs);
holder=getHolder();
holder.addCallback(this);
}
@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {try{
InputStream is= getContext().getAssets().open("122856549320150205220255.gif");
movie=Movie.decodeStream(is);int width =movie.width();int height =movie.height();
setMeasuredDimension(width*ZOOM, height*ZOOM);
handler.post(runnable);
}catch(IOException e) {
e.printStackTrace();
}
}
@Overridepublic voidsurfaceCreated(SurfaceHolder holder) {
}
@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, intwidth,intheight) {
}
@Overridepublic voidsurfaceDestroyed(SurfaceHolder holder) {
handler.removeCallbacks(runnable);
}private void setZoom(intzoom) {this.ZOOM =zoom;
}public voidsetPath(String path) {this.path =path;
}
}