怎样在开发Android端带货直播APP源码时,实现播放动画的操作

在开发Android端带货直播APP源码时,经常会有客户想要在某些地方实现播放一个小动画的需求。一般按照简单的原则会使用连续的图片做帧动画,但是如果有几十上百张、或者几百K甚至几M的图片,这个时候用帧动画来实现其实就很有问题了,内存吃紧,会卡顿,OOM等问题随之而来。
经过考虑,可以在开发带货直播APP源码时,选择用SurfaceView来加载图片,因为这个控件是另开子线程利用双缓存加载资源再直接绘制Surface上,不影响主线程,还避过了大部分java层的耗时操作。因此此选择更优。
首先,先要自定义一个继承自SurfaceView的类,次要步骤略过,主要绘图的方法为:

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();
                }
            }
        }
    }

以上就是在开发Android端带货直播APP源码时,实现播放小动画的一系列操作,这些小功能的添加和使用,也可以在一定程度上为带货直播APP源码增色不少。要想了解更多,可继续关注。
声明:本文由作者原创,转载请注明出处及原文链接。

发布了150 篇原创文章 · 获赞 65 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/yb1314111/article/details/102930191