利用SurfaceView实现帧动画效果,更流畅,更节约内存

利用SurfaceView实现帧动画效果

在开发Android做动画效果的时候,有时候UI给开发一组动画实现的帧图片,如果说图片较少(十几张)、分辨率较低(几K,十几K),用帧动画实现应该没什么问题,但是如果有几十上百张、或者几百K或者上M的图片,这个时候用帧动画来实现其实就很有问题了,内存吃紧,会卡顿,OOM等问题随之而来。

当然如果可以沟通UI改设计是最好的,但是如果非得这样做不可呢?本人就遇到过这样的需求,而且还有好几种不同的图片根据状态连续来回的切换动画,这个时候还要考虑切换的时候不卡顿的问题,之前写过一篇博客利用Handler来一帧帧的去绘制,目前没有发现什么问题,也很流畅,但是不断的发送Handler消息还是有一定弊端的,所以这里带来更优的选择,用SurfaceView来加载图片,因为这个控件是另开子线程利用双缓存加载资源再直接绘制Surface上,不影响主线程,还避过了大部分java层的耗时操作。因此此选择更优。

具体代码实现如下:

注意一下,如果是png带透明背景的图片,需要surfaceView透明背景的实现:

    //设置透明背景
        //setZOrderOnTop(true) 必须在setFormat方法之前,不然png的透明效果不生效
        setZOrderOnTop(true);
        mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);

public class SurfaceViewAnimation extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    private String TAG = "SurfaceViewAnimation";

    private SurfaceHolder mSurfaceHolder;

    private boolean mIsThreadRunning = true; // 线程运行开关
    public static boolean mIsDestroy = false;// 是否已经销毁

    private int[] mBitmapResourceIds;// 用于播放动画的图片资源id数组
    private int totalCount;//资源总数 用来判断动画循环
    private Canvas mCanvas; // 画图片
    private Bitmap mBitmap;// 显示的图片的bitmap

    private int mCurrentIndext;// 当前动画播放的位置
    private int mGapTime = 50;// 每帧动画持续存在的时间
    private boolean mIsRepeat = false;

    private OnFrameFinishedListener mOnFrameFinishedListener;// 动画监听事件
    private Thread thread;

    Rect mSrcRect, mDestRect;

    public SurfaceViewAnimation(Context context) {
        this(context, null);
        initView();
    }

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

    public SurfaceViewAnimation(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        initView();

    }

    private void initView() {

        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_HARDWARE);

        //设置透明背景
        //setZOrderOnTop(true) 必须在setFormat方法之前,不然png的透明效果不生效
        setZOrderOnTop(true);
        mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);

        mBitmapResourceIds = new int[1];
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        
        // 根据实际需要是利用start()函数外部开启调用开启动画线程,还是显示控件就开启动画
        mIsThreadRunning = ture;
        new Thread(this).start();
    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        destroy();
    }

    /**
     * 制图方法
     */
    private void drawView() {
        // 无资源文件退出
        if (mBitmapResourceIds == null && mBitmapResourcePaths == null) {
            Log.e("frameview", "the bitmapsrcIDs is null");
            mIsThreadRunning = false;

            return;
        }

        Log.d(TAG, "drawView: mCurrentIndext=" + mCurrentIndext);
        Log.d(TAG, "drawView: Thread id = " + Thread.currentThread().getId());

        //防止是获取不到Canvas
        SurfaceHolder surfaceHolder = mSurfaceHolder;
        // 锁定画布
        synchronized (surfaceHolder) {
            if (surfaceHolder != null) {
                mCanvas = surfaceHolder.lockCanvas();
                Log.d(TAG, "drawView: mCanvas= " + mCanvas);
                if (mCanvas == null) {
                    return;
                }
            }
            try {

                if (surfaceHolder != null && mCanvas != null) {

                    synchronized (mBitmapResourceIds) {
                        if (mBitmapResourceIds != null && mBitmapResourceIds.length > 0) {
                            mBitmap = BitmapUtil.decodeSampledBitmapFromResource(getResources(), mBitmapResourceIds[mCurrentIndext], getWidth(), getHeight());
                        } else if (mBitmapResourcePaths != null && mBitmapResourcePaths.size() > 0) {
                            mBitmap = BitmapFactory.decodeFile(mBitmapResourcePaths.get(mCurrentIndext));

                        }
                    }
                    mBitmap.setHasAlpha(true);

                    if (mBitmap == null) {
                        return;
                    }

                    Paint paint = new Paint();
                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                    mCanvas.drawPaint(paint);
                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
                    paint.setAntiAlias(true);
                    paint.setStyle(Paint.Style.STROKE);

                    mSrcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
                    mDestRect = new Rect(0, 0, getWidth(), getHeight());
                    mCanvas.drawBitmap(mBitmap, mSrcRect, mDestRect, paint);

                    // 播放到最后一张图片
                    if (mCurrentIndext == totalCount - 1) {
                        //TODO 设置重复播放
                        //播放到最后一张,当前index置零
                        mCurrentIndext = 0;
                    }

                }

            } catch (Exception e) {
                Log.d(TAG, "drawView: e =" + e.toString());
                e.printStackTrace();
            } finally {

                mCurrentIndext++;

                if (mCurrentIndext >= totalCount) {
                    mCurrentIndext = 0;
                }
                if (mCanvas != null) {
                    // 将画布解锁并显示在屏幕上
                    if (getHolder() != null) {
                        surfaceHolder.unlockCanvasAndPost(mCanvas);
                    }
                }

                if (mBitmap != null) {
                    // 收回图片
                    mBitmap.recycle();
                }
            }
        }
    }

    @Override
    public void run() {
        if (mOnFrameFinishedListener != null) {
            mOnFrameFinishedListener.onStart();
        }
        Log.d(TAG, "run: mIsThreadRunning=" + mIsThreadRunning);
        // 每隔mGapTimems刷新屏幕
        while (mIsThreadRunning) {
            drawView();
            try {
                Thread.sleep(mGapTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (mOnFrameFinishedListener != null) {
            mOnFrameFinishedListener.onStop();
        }
    }

    /**
     * 开始动画
     */
    public void start() {
        if (!mIsDestroy) {
            mCurrentIndext = 0;
            mIsThreadRunning = true;
            thread = new Thread(this);
            thread.start();
        } else {
            // 如果SurfaceHolder已经销毁抛出该异常
            try {
                throw new Exception("IllegalArgumentException:Are you sure the SurfaceHolder is not destroyed");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 防止内存泄漏
     */
    private void destroy() {
        //当surfaceView销毁时, 停止线程的运行. 避免surfaceView销毁了线程还在运行而报错.
        mIsThreadRunning = false;
        try {
            Thread.sleep(mGapTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mIsDestroy = true;

        thread.interrupt();
        thread = null;

        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }

        if (mSurfaceHolder != null) {
            mSurfaceHolder.addCallback(null);
        }

        if (mOnFrameFinishedListener != null) {
            mOnFrameFinishedListener = null;
        }
    }

    /**
     * 设置动画播放素材的id
     *
     * @param bitmapResourceIds 图片资源id
     */
    public void setBitmapResoursID(int[] bitmapResourceIds) {
        // 防止主线程和子线程同时操作mBitmapResourceIds资源造成死锁
        synchronized (mBitmapResourceIds) {
            this.mBitmapResourceIds = bitmapResourceIds;
            totalCount = bitmapResourceIds.length;
        }
    }

    /**
     * 设置动画播放素材的路径
     *
     * @param bitmapResourcePaths
     */
    public void setmBitmapResourcePath(ArrayList bitmapResourcePaths) {
        this.mBitmapResourcePaths = bitmapResourcePaths;
        totalCount = bitmapResourcePaths.size();
    }

    /**
     * 设置每帧时间
     */
    public void setGapTime(int gapTime) {
        this.mGapTime = gapTime;
    }

    /**
     * 结束动画
     */
    public void stop() {
        mIsThreadRunning = false;
    }

    /**
     * 继续动画
     */
    public void reStart() {
        mIsThreadRunning = false;
    }

    /**
     * 设置动画监听器
     */
    public void setOnFrameFinisedListener(OnFrameFinishedListener onFrameFinishedListener) {
        this.mOnFrameFinishedListener = onFrameFinishedListener;
    }

    /**
     * 动画监听器
     *
     * @author qike
     */
    public interface OnFrameFinishedListener {

        /**
         * 动画开始
         */
        void onStart();

        /**
         * 动画结束
         */
        void onStop();
    }
}

项目demo代码地址:https://github.com/pffo/SurfaceViewAnimation

同一个世界,同一个程序梦,欢迎交流。

你可能感兴趣的:(利用SurfaceView实现帧动画效果,更流畅,更节约内存)