在android中的canvas上旋转图像

我想根据android中的特定角度旋转图像,有些像指南针…

我有这个代码…它适用于drawPath(),但我想用图像替换路径和绘图的东西..我试图创建一个位图图像,DrawBitmapImage,但图像不像路径一样旋转..任何请帮助?

public void draw(Canvas canvas) { double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); //Correction; angle-=90; //Correction for azimuth angle-=azimuth; if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90; while(anglewidth){ top+=(height-width)/2; height=width; } if(width>height){ left+=(width-height)/2; width=height; } float centerwidth = width/2f; float centerheight = height/2f; Paint p = new Paint(); p.setColor(color); p.setStyle(Paint.Style.FILL); p.setAntiAlias(true); float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0); float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0); Path path = new Path(); path.moveTo( startX, startY); path.lineTo( left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0), top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0)); path.lineTo( left+(float)centerwidth, top+(float)centerheight ); path.lineTo( left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0) ); path.lineTo( startX, startY ); canvas.drawPath(path, p); } 

Solutions Collecting From Web of "在android中的canvas上旋转图像"

您可以使用矩阵绘制位图时旋转位图:

 Matrix matrix = new Matrix(); matrix.setRotate(angle, imageCenterX, imageCenterY); yourCanvas.drawBitmap(yourBitmap, matrix, null); 

您也可以在绘制之前旋转canvas来执行此操作:

 yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated. yourCanvas.rotate(-angle); yourCanvas.drawBitmap(yourBitmap, left, top, null); yourCanvas.restore(); 

选择最适合你的。

您必须先旋转canvas,然后绘制您想要的任何内容。 然后绘制的对象将在屏幕上旋转显示。

 canvas.rotate(45); // degrees to rotate 

试试这个好方法。

查看本教程,您将获得有关如何绘制位图以及如何旋转canvas的信息

查看完整教程

@Reham:看看下面的示例代码,

 public class bitmaptest extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout linLayout = new LinearLayout(this); // load the origial BitMap (500 x 500 px) Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.android); int width = bitmapOrg.width(); int height = bitmapOrg.height(); int newWidth = 200; int newHeight = 200; // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap matrix.postRotate(45); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); ImageView imageView = new ImageView(this); // set the Drawable on the ImageView imageView.setImageDrawable(bmd); // center the Image imageView.setScaleType(ScaleType.CENTER); // add ImageView to the Layout linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // set LinearLayout as ContentView setContentView(linLayout); } } 

你必须使用矩阵旋转图像看线

 matrix.postRotate(45); - 

这会将图像旋转45度

希望这能帮助你… thx

使用以下代码。 它对我有用

浮动旋转= 30.0f;

  Bitmap bitmap = your bitmap Rect rect = new Rect(100,100,bitmap.width, bitmap.height); Matrix matrix = new Matrix(); float px = rect.exactCenterX(); float py = rect.exactCenterY(); matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); matrix.postRotate(rotation); matrix.postTranslate(px, py); canvas.drawBitmap(bitmap, matrix, null); matrix.reset(); invalidate(); 

这是唯一一个对我没有问题的人。

  private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){ int w = bitmap.getWidth(); int h = bitmap.getHeight(); int newW=w, newH=h; if (rotationAngleDegree==90 || rotationAngleDegree==270){ newW = h; newH = w; } Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig()); Canvas canvas = new Canvas(rotatedBitmap); Rect rect = new Rect(0,0,newW, newH); Matrix matrix = new Matrix(); float px = rect.exactCenterX(); float py = rect.exactCenterY(); matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); matrix.postRotate(rotationAngleDegree); matrix.postTranslate(px, py); canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG )); matrix.reset(); return rotatedBitmap; }