关闭

Android显示Gif动图

标签: androidgif动图
273人阅读 评论(0) 收藏 举报
分类:
package org.media.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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


public class DrawingGif extends SurfaceView implements SurfaceHolder.Callback{

    private final static String TAG = "DrawingGif";

    private int scale = 1;
    private String path;
    private SurfaceHolder mHolder;
    private Canvas canvas;
    private Movie movie;
    private Handler mHandler;

    public DrawingGif(Context context) {
        this(context,null);
    }

    public DrawingGif(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public DrawingGif(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        Log.d(TAG,"init");
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHandler = new Handler();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated");
        new Thread(mRunnable).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG,"surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG,"surfaceDestroyed");
        mHandler.removeCallbacks(mRunnable);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d(TAG,"onMeasure,path:"+path);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!TextUtils.isEmpty(path)){
            try {
                InputStream ins = getContext().getAssets().open(path);
                movie = Movie.decodeStream(ins);
                int width = movie.width();
                int height = movie.height();
                setMeasuredDimension(width*scale,height*scale);
                //mHandler.post(mRunnable);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (!TextUtils.isEmpty(path)){
                canvas = mHolder.lockCanvas();
                canvas.save();
                canvas.drawColor(Color.WHITE);
                canvas.scale(scale, scale);
                movie.draw(canvas, 0, 0);
                movie.setTime((int) (System.currentTimeMillis() % movie.duration()));
                canvas.restore();
                mHolder.unlockCanvasAndPost(canvas);
                mHandler.postDelayed(mRunnable, 50);
            }
        }
    };

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
        invalidate();
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
        invalidate();
    }
}

使用:
gif = (DrawingGif) findViewById(R.id.gif);
        gif.setPath("2.gif");//asset路径
        gif.setScale(5);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28857次
    • 积分:813
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:1篇
    • 译文:1篇
    • 评论:3条
    文章分类
    最新评论