From the code you posted, majority of the operation in the function does not involve a view. The only part that interacts with a view is the canvas drawing operation. So as has been pointed out in the comments, you should run all the calculations in a worker thread, then call a runOnUIThread with a runnable to do the final drawing on the canvas. The runOnUIThread call will prevent android from throwing an error that occurs when you modify a view from a worker thread not tied to the UI.
The options you have here are obviously AsyncTask and the threading classes. In my example below, I am using an 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;
}
}