Android 实现播放本地GIF图片

做一个记录,实现播放本地GIF图片,前提是需要一张GIF图片

二话不说先上效果图
第二个效果图

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GifSurfaceView sv = (GifSurfaceView) findViewById(R.id.sv);
        sv.setNet(false);
        sv.setZoom(1.0f);   
    }
}

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/***
 * 高速描绘
 * @author Sinocall
 *
 */
public class GifSurfaceView extends SurfaceView implements Callback {

    private SurfaceHolder holder;
    private Movie movie;

    private boolean isNet = false;
    private String path;
    private float zoom;
    //不断绘制,不断更新视图
    private Handler handler = new Handler();

    private Runnable run = new Runnable() {

        @Override
        public void run() {
            //不断绘制真画面
            Canvas canvas = holder.lockCanvas();

            //不影响后面,下次绘图操作
            canvas.save();
            canvas.scale(zoom, zoom);
            movie.draw(canvas, 0, 0);
            canvas.restore();

            holder.unlockCanvasAndPost(canvas);

            //5,1 2 3 4 5,
            movie.setTime((int)System.currentTimeMillis() % movie.duration());

            handler.removeCallbacks(run);
            handler.postDelayed(run, 30);

        }
    };


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

    }

    public GifSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        holder = getHolder();
        holder.addCallback(this);
    }

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

    /***
     * 计算,控件,
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //开始Gif图片绘制
        //Gif图片---》影片
        InputStream is;
        try {
            if(isNet){
                is = new URL(path).openConnection().getInputStream();
            }else{
                is = getContext().getAssets().open("1.gif");
            }

            movie = Movie.decodeStream(is);

            //设置SurfaceView宽高
            int width = movie.width();
            int height = movie.height();
            //指定控件的宽高
            setMeasuredDimension((int)(width*zoom), (int)(height*zoom));

            //有很多个珍画面轮训播放
            handler.post(run);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 初始化完成
     * @param holder
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //停止绘图
        handler.removeCallbacks(run);
    }

    public boolean isNet() {
        return isNet;
    }

    public void setNet(boolean isNet) {
        this.isNet = isNet;
    }

    public String getPath() {
        return path;
    }

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

    public float getZoom() {
        return zoom;
    }

    public void setZoom(float zoom) {
        this.zoom = zoom;
    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.hky.view.GifSurfaceView
        android:id="@+id/sv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</RelativeLayout>
  • 就这么简单,就这么任性的完成了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值