Android APIDemo gif图片显示

Android APIDemo gif图片显示_第1张图片


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

public class GifView extends View
{
    private Context mContext;
    private Movie mMovie;
    private long mMovieStart;
    private int dur;

    public GifView(Context context)
    {
        super(context);
        mContext = context;
        initMovie();
    }

    public GifView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        initMovie();
    }

    private void initMovie()
    {
        mMovie = Movie.decodeStream(mContext.getResources().openRawResource(
                R.drawable.gif1));
        dur = mMovie.duration();
        if (dur <= 0)
        {
            dur = 1000;
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (mMovie != null)
        {
            long now = android.os.SystemClock.uptimeMillis();
            if (mMovieStart == 0)
            {
                mMovieStart = now;
            }
            mMovie.setTime((int) ((now - mMovieStart) % dur));
            mMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }
}





你可能感兴趣的:(android,null,Class,import)