android - 在android中,在画布上旋转位图

  显示原文与译文双语对照的内容
0 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);
时间:原作者:1个回答

0 0

问题是最后一行: 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);

我的建议是寻找一个更好的位置来创建位图- 你不需要每次创建一个新位图。 我将 sugest onCreate() 函数用于这里目的。

原作者:
...