I think you complete too many tasks in just one second. Instead, you can prepare all the heavy staff in onPreExecute() , read the XML and make a picture in doInBackground() , resfresh ImageView in onProgressUpdate() and finally, when the task is completed, save the image in sdcard .
I modified your Asynctask to execute the above scenario, I have not tested it, but it gives you this idea.
In the onCreate() method of your activity, you run AsyncTask only once. It remains executing or sleeping until you set the Quit_Task variable to true. When the button is pressed, you switch the Do_Drawing: Do_Drawing=!Do_Drawing; variable Do_Drawing: Do_Drawing=!Do_Drawing; and her.
private boolean Do_Drawing = false; private boolean Quit_Task = false; // READ XML FILE class PositionAsync extends AsyncTask<Void, Void, Void> { Paint paintBlack; BitmapFactory.Options myOptions; Bitmap mutableBitmap2; Canvas canvas2; XMLHelper helper; void Sleep(int ms) { try { Thread.sleep(ms); } catch (Exception e) { } } @Override protected void onPreExecute() { // Prepare everything for doInBackground thread paintBlack = new Paint(); paintBlack.setAntiAlias(true); paintBlack.setColor(Color.BLACK); myOptions = new BitmapFactory.Options(); myOptions.inDither = true; myOptions.inScaled = false; myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important myOptions.inPurgeable = true; File ImageSource = new File("/sdcard/app_background3.jpg"); Bitmap bitmap2 = BitmapFactory.decodeFile(ImageSource.getAbsolutePath(), myOptions); Bitmap workingBitmap = Bitmap.createBitmap(bitmap2); mutableBitmap2 = workingBitmap.copy(Bitmap.Config.ARGB_8888, true); canvas2 = new Canvas(mutableBitmap2); helper = new XMLHelper(); } @Override protected Void doInBackground(Void... arg0) { while (!Quit_Task) { // Sleep until button is pressed or quit while (!Do_Drawing) { Sleep(1000); if (Quit_Task) return null; } float RoomWidthPx = canvas2.getWidth(); float RoomHeightPx = canvas2.getHeight(); float RoomXmeter = (float) 9.3; float RoomZmeter = (float) 14.7; // keep drawing until button is pressed again or quit while (Do_Drawing) { if (Quit_Task) return null; helper.get(); for (PositionValue position : helper.positions) { String PosX = position.getPositionX(); String PosY = position.getPositionY(); String PosZ = position.getPositionZ(); float x = Float.valueOf(PosX); float y = Float.valueOf(PosY); float z = Float.valueOf(PosZ); float xm = x * RoomWidthPx / RoomXmeter; float zm = z * RoomHeightPx / RoomZmeter; canvas2.drawCircle(xm, zm, 25, paintBlack); } this.publishProgress((Void) null); Sleep(1000); } } return null; } @Override protected void onProgressUpdate(Void... progress) { // once all points are read & drawn refresh the imageview try { ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setAdjustViewBounds(true); imageView.setImageBitmap(mutableBitmap2); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onPostExecute(Void result) { // SAVE DRAWINGS INTO FILE once the task is done. FileOutputStream fos = null; try { fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "app_background3.jpg"); mutableBitmap2.compress(Bitmap.CompressFormat.JPEG, 95, fos); } catch (Throwable ex) { ex.printStackTrace(); } } } // END READ XML FILE
source share