【问题标题】:How to draw many bitmap on cavas如何在画布上绘制许多位图
【发布时间】:2019-09-25 09:41:06
【问题描述】:

我想在画布上绘制许多位图,但 android 显示:“应用程序可能在其主线程上做了太多工作”并且应用程序无法运行。 [ cloud_number 超过 50 ]

这是我的代码:

 fun draw_beautiful(canvas: Canvas){

    val mPaint: Paint = Paint()
    mPaint.style = Paint.Style.FILL
    mPaint.isAntiAlias = true
    mPaint.color= Color.BLACK

    var vt = 0; var x1 =0f; var y1 =0f;var style =0;

    for (i in 1..cloud_number){
        vt = i*4-3
        style = mt_cloud[vt]
        x1  = mt_cloud[vt+2].toFloat()
        y1 = mt_cloud[vt+3].toFloat()

        
        var ob= canvas_cloud1
        if (style==0) ob= canvas_cloud1
        else if (style==1) ob= canvas_cloud2
        else if (style==2) ob= canvas_cloud3

        canvas.drawBitmap(ob, x1,y1 , mPaint)

    }

}

【问题讨论】:

  • 尝试使用 AsyncTask
  • 你能给我一些代码示例吗?
  • canvas 可以在上面工作吗?我曾经尝试过,但画布在异步任务中中断。
  • @DangHoang,在下面查看我的答案。我只能用 Java 编写代码,但如果我在 Kotlin 中尝试它会出现问题。

标签: android canvas kotlin android-asynctask surfaceview


【解决方案1】:

从您发布的代码来看,函数中的大部分操作不涉及视图。唯一与视图交互的部分是画布绘制操作。因此,正如 cmets 中所指出的,您应该在工作线程中运行所有计算,然后调用带有可运行对象的 runOnUIThread 以在画布上进行最终绘图。当您从未绑定到 UI 的工作线程修改视图时,runOnUIThread 调用将防止 android 引发错误。

这里的选项显然是 AsyncTask 和线程类。在下面的示例中,我使用的是 AyncTask。

void draw_beautiful(Canvas canvas) {
        MyDrawingWorkerTask myDrawingWorkerTask = new MyDrawingWorkerTask(this);
        myDrawingWorkerTask.execute(canvas);
    }


/**
 * A class for doing a lot of the work in a background
 */
private static class MyDrawingWorkerTask extends AsyncTask<Canvas, Void, Void> {

    private final Activity activity;

    public MyDrawingWorkerTask(Activity activity) {// add more parameters to use to set other variables like cloud_number, etc
        this.activity = activity;
    }

    // This is where the work is being done
    @Override
    protected Void doInBackground(Canvas... canvases) {
        final Canvas canvas = canvases[0];
        final Paint mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);

        int vt = 0;
        float x1 = 0f;
        float y1 = 0f;
        int style = 0;

        for (int i = 0; i < cloud_number; i++) {
            vt = i * 4 - 3;
            style = mt_cloud[vt];
            x1 = mt_cloud[vt + 2].toFloat();
            y1 = mt_cloud[vt + 3].toFloat();


            Bitmap ob = canvas_cloud1;
            if (style == 0) ob = canvas_cloud1;
            else if (style == 1) ob = canvas_cloud2;
            else if (style == 2) ob = canvas_cloud3;

            Bitmap finalOb = ob;
            float finalX = x1;
            float finalY = y1;

            // note that this is what interacts with the view
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    canvas.drawBitmap(finalOb, finalX, finalY, mPaint);
                }
            });
        }
        return null;
    }
}

【讨论】:

  • 我也是 Kotlin 的新手,我几乎没有在工作环境中写 Kotlin,因为我有截止日期。
  • 我转移到 kotlin 并显示此错误:无法完成刷新条目的计划请求。客户端错误代码:3
  • 我对 Kotlin 知之甚少,请查看here 寻找可能的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 2018-01-06
  • 2013-07-07
相关资源
最近更新 更多