使用AsyncTask和progressbar

时间:2015-08-17 11:45:21

标签: android bitmap android-asynctask effects

有时,当我编辑位图时,我的手机需要比平时更多的时间来应用更改。有人可以帮助我了解如何正确使用AsyncTaskprogressbar

例如,在我的应用程序中,我执行以下操作:

i.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BitmapFactory.Options Options = new BitmapFactory.Options();
                Bitmap bitmap= BitmapFactory.decodeFile(photoPath,Options);
                bitmap = editing(bitmap,60); 
            }
        });

不幸的是editing比平常花费更多时间:

 public Bitmap editing(Bitmap src, int degree) {
      // get source image size
         int width = src.getWidth();
         int height = src.getHeight();

         int[] pix = new int[width * height];
      // get pixel array from source
         src.getPixels(pix, 0, width, 0, 0, width, height);

         int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
         double angle = (PI * (double)degree) / HALF_CIRCLE_DEGREE;

         int S = (int)(RANGE * Math.sin(angle));
         int C = (int)(RANGE * Math.cos(angle));

         for (int y = 0; y < height; y++)
             for (int x = 0; x < width; x++) {
                 int index = y * width + x;
                 int r = ( pix[index] >> 16 ) & 0xff;
                 int g = ( pix[index] >> 8 ) & 0xff;
                 int b = pix[index] & 0xff;
                 RY = ( 70 * r - 59 * g - 11 * b ) / 100;
                 GY = (-30 * r + 41 * g - 11 * b ) / 100;
                 BY = (-30 * r - 59 * g + 89 * b ) / 100;
                 Y  = ( 30 * r + 59 * g + 11 * b ) / 100;
                 RYY = ( S * BY + C * RY ) / 256;
                 BYY = ( C * BY - S * RY ) / 256;
                 GYY = (-51 * RYY - 19 * BYY ) / 100;
                 R = Y + RYY;
                 R = ( R < 0 ) ? 0 : (( R > 255 ) ? 255 : R );
                 G = Y + GYY;
                 G = ( G < 0 ) ? 0 : (( G > 255 ) ? 255 : G );
                 B = Y + BYY;
                 B = ( B < 0 ) ? 0 : (( B > 255 ) ? 255 : B );
                 pix[index] = 0xff000000 | (R << 16) | (G << 8 ) | B;
             }
      // output bitmap
         Bitmap outBitmap = Bitmap.createBitmap(width, height, src.getConfig());    
         outBitmap.setPixels(pix, 0, width, 0, 0, width, height);

         pix = null;

         return outBitmap;
     }
}

1 个答案:

答案 0 :(得分:0)

private class myTask extends AsyncTask<Void,Void,Bitmap>{

private ProgressDialog progressDialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();
}

  protected Bitmap doInBackground(Void... params) {

         BitmapFactory.Options Options = new BitmapFactory.Options();

            Bitmap bitmap= BitmapFactory.decodeFile(photoPath,Options);

            bitmap = editing(bitmap,60); 

         return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //you can use result here to show it on UI
         progressDialog.dismiss();
        }
    }