android gif播放

package com.ttw.gif;


import java.io.IOException;
import java.io.InputStream;


import com.ttw.androidhtppclient.Utility;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;


public class GifMove extends View {


private Movie mMovie;


private InputStream is = null;

private long mMovieStart;


public GifMove(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


public GifMove(Context context, AttributeSet attrs) {
super(context, attrs);
}


public GifMove(Context context) {
super(context);
}


public void setGifSource(int source) {

BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), source, option);
Utility.logI("move widht = " + option.outWidth + " height = " + option.outHeight);
LayoutParams lp = this.getLayoutParams();
lp.width = option.outWidth;
lp.height = option.outHeight;
this.setLayoutParams(lp);
if(null != is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
is = null;
}
}
is = getResources().openRawResource(source);
mMovie = Movie.decodeStream(is);
}


@Override
protected void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
 
         if (mMovieStart == 0) {
             mMovieStart = now;
         }

if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 500;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight()
- mMovie.height());
invalidate();
}
super.onDraw(canvas);
}


}

posted @ 2013-03-29 14:04  xinyuyuanm  阅读(196)  评论(0编辑  收藏  举报