Android 2D在异步任务中绘制到画布上,我做得对吗?

时间:2014-07-12 09:28:43

标签: android bitmap android-canvas

我编写了一个程序,用2D渲染投球和旋转球(它是真实3D指南针的2D版本,其类型有一个跟随北方的浮动球体,当框架倾斜时倾斜)

它的工作正常,但需要30mS绘制到没有旋转矩阵的画布/位图和带旋转矩阵的60mS。

绘制代码发生在asyncTask doInBackground中,因为它从套接字获取数据。在完成创建位图之后,它会执行发布进度。所以代码看起来像这样(剥离很多)

Bitmap mRotatedBmp = null;
@Override
    protected String doInBackground(String... params) {

        // read pitch and roll data from socket and loop forever

        int wh=448, c=wh/2, r=wh*100/220;
        Bitmap bmp = Bitmap.createBitmap(wh, wh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);

            // do some drawing like this
            canvas.drawCircle(c, c, r, paint);
            paint.setColor(skycolor);
            canvas.drawArc(oval, 180, 180, true, paint)
    etc, total 1 circle, 2 arcs, 1 rectangle and several paths and a bit of text

//takes 30mS to here
        // ROTATION PART 
        // mRotatedBmp is the thing that gets displayed in publishProgress
        mRotatedBmp  = Bitmap.createBitmap(wh, wh, Bitmap.Config.ARGB_8888);
        Canvas rotatedCanvas = new Canvas(mRotatedBmp );
        Matrix matrix = new Matrix();
        matrix.setRotate(-rollD,r,r);
        rotatedCanvas.drawBitmap(bmp, matrix, new Paint());
 //takes 60mS to here  

    @Override
    protected void onProgressUpdate(Void... values) {


        imageView.setImageBitmap(mRotatedBmp  );        // draw new img to screen
        imageView.invalidate();

所以我想知道我是否正在接受错误的事情。基本上它是一个450x450的图像,包括总共1个圆,2个圆弧,1个矩形和几个路径以及一些文本

感谢任何想法如何加快

1 个答案:

答案 0 :(得分:-1)

我发现这个问题与绘图有很大关系,而创建位图则有90%。

如果我移动位图bmp = Bitmap.createBitmap(wh,wh,Bitmap.Config.ARGB_8888);离开循环,只做一次,然后我得到大约6mS的抽奖时间

现在的问题是我在屏幕内存上进行了大量的闪存,但可以通过双缓冲修复它