0

我在画布上旋转位图时遇到问题,我正在使用 canvas.drawline 在画布上绘制位图并使用以下代码旋转图像

它旋转但现在显示两个图像?如何从画布中删除以前的图像?该怎么办?

bitmap = Bitmap.createBitmap((int) /*getWindowManager()
                        .getDefaultDisplay().getWidth()*/600, (int) /*getWindowManager()
                        .getDefaultDisplay().getHeight()*/600, Bitmap.Config.ARGB_8888 );
             Canvas canvas = new Canvas(bitmap);
             drawingImageView.setVisibility(View.VISIBLE);

               drawingImageView.setImageBitmap(bitmap);

              // canvas.rotate(90);
                   for(int i=0;i<SettingsStaticData.arrX1_crossPoint.size();i++)
                   {

                       canvas.drawLine(new Float(SettingsStaticData.arrX1_crossPoint.get(i)),new Float(SettingsStaticData.arrY1_crossPoint.get(i)),new Float(SettingsStaticData.arrX2_crossPoint.get(i)) ,new Float(SettingsStaticData.arrY2_crossPoint.get(i)), paint);


                   }


                    paint.setStrokeWidth((float) 0.9);
                   paint.setStyle(Paint.Style.FILL);
                 //  paint.setColor(Color.RED);

                   paint.setColor(Color.parseColor("#800080"));
               for(int i=0;i<arrXX.size()-1;i++)
                {

                  if(i==arrXX.size()-2)
                  {
                      canvas.drawLine(new Float(arrXX.get(0)),new Float(arrYY.get(0)),new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);
                      canvas.drawLine(new Float(arrXX.get(i)),new Float(arrYY.get(i)),new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);

                  }
                  else
                  {
                      canvas.drawLine(new Float(arrXX.get(i)),new Float(arrYY.get(i)),new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);

                  }
                }


        //     canvas.drawColor(0, Mode.CLEAR);
               Matrix matrix = new Matrix();

                  matrix.reset();
               matrix.postTranslate(-bitmap.getWidth() / 2-500, -bitmap.getHeight() / 2-500); // Centers image
               matrix.postRotate(180);
              //matrix.postTranslate(px, py);


               canvas.drawBitmap(bitmap, matrix, null);
4

1 回答 1

2

问题是最后一行:canvas.drawBitmap(bitmap, matrix, null);

canvas用 初始化,bitmap因此您可以在自身上绘制位图(进行一些更改)。

只需创建另一个Canvas canvas2并将最后一行更改为canvas2.drawBitmap(bitmap, matrix, null); 并显示最后一行drawingImageView.setImageBitmap(bitmap2);,其中 bitmap2 是用于创建 canvas2 的位图。

所以,明确地:

bitmap = Bitmap.createBitmap((int) /*getWindowManager()
            .getDefaultDisplay().getWidth()*/600, (int) /*getWindowManager()
            .getDefaultDisplay().getHeight()*/600, Bitmap.Config.ARGB_8888 );
bitmapFinal = Bitmap.createBitmap((int) /*getWindowManager()
            .getDefaultDisplay().getWidth()*/600, (int) /*getWindowManager()
            .getDefaultDisplay().getHeight()*/600, Bitmap.Config.ARGB_8888 );

    Canvas canvas = new Canvas(bitmap);
    // canvas.rotate(90);
    for(int i=0;i<SettingsStaticData.arrX1_crossPoint.size();i++){
        canvas.drawLine(new Float(SettingsStaticData.arrX1_crossPoint.get(i)),
                new Float(SettingsStaticData.arrY1_crossPoint.get(i)),
                new Float(SettingsStaticData.arrX2_crossPoint.get(i)) ,
                new Float(SettingsStaticData.arrY2_crossPoint.get(i)), paint);
    }

    paint.setStrokeWidth((float) 0.9);
    paint.setStyle(Paint.Style.FILL);
    //  paint.setColor(Color.RED);

    paint.setColor(Color.parseColor("#800080"));
    for(int i=0;i<arrXX.size()-1;i++){

        if(i==arrXX.size()-2){
            canvas.drawLine(new Float(arrXX.get(0)),new Float(arrYY.get(0)),
                    new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);
            canvas.drawLine(new Float(arrXX.get(i)),new Float(arrYY.get(i)),
                    new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);

        }else{
            canvas.drawLine(new Float(arrXX.get(i)),new Float(arrYY.get(i)),new Float(arrXX.get(i+1)) ,new Float(arrYY.get(i+1)), paint);
        }
    }
    //     canvas.drawColor(0, Mode.CLEAR);
    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.postTranslate(-bitmap.getWidth() / 2-500, -bitmap.getHeight() / 2-500); // Centers image
    matrix.postRotate(180);
    //matrix.postTranslate(px, py);

    Canvas canvas2 = new Canvas(bitmapFinal);
    canvas2.drawBitmap(bitmap, matrix, null);
    drawingImageView.setVisibility(View.VISIBLE);
    drawingImageView.setImageBitmap(bitmapFinal);

另外,我的建议是寻找一个更好的位置来创建位图——你真的不需要每次都创建一个新的位图——你应该创建一次并重复使用(不断创建位图的成本很高)。我会onCreate()为此目的建议功能。

于 2012-10-02T13:17:13.897 回答