Android 实现播放本地GIF图片

来源:互联网 发布:费马的房间 知乎 编辑:程序博客网 时间:2024/05/15 20:57

做一个记录,实现播放本地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>
  • 就这么简单,就这么任性的完成了!
0 0
原创粉丝点击