23

How can a Drawable loaded from a resource be rotated when it is drawn? For example, I would like to draw an arrow and be able to rotate it to face in different directions when it is drawn?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Computerish
  • 9,590
  • 7
  • 38
  • 49
  • possible duplicate of [How can I draw an Arrow showing the driving direction in MapView?](http://stackoverflow.com/questions/4331794/how-can-i-draw-an-arrow-showing-the-driving-direction-in-mapview) – Jonas Jan 21 '11 at 20:25
  • The question is quite different, but I think the answer may be related. – Computerish Jan 21 '11 at 21:09
  • Does this answer your question? [Rotating Image on A canvas in android](https://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in-android) – Mahozad Jun 28 '21 at 13:37

4 Answers4

21

You need to use Bitmap and Canvas Class functions to prepare drawable:

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult); 
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);

mImageView.setImageBitmap(bmResult);

In this code sample rotation for 90 degrees over image center occurs.

Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • OK, If you need to make image semi-transparent or need zoom feature, you may check also this http://stackoverflow.com/questions/4688306/how-can-i-make-image-opaque-to-some-level/4689624#4689624 – Zelimir Jan 22 '11 at 07:17
  • Works fine for me as well. What is the issue you encounter? – Zelimir May 30 '11 at 08:56
  • As Jay said a couple of years back this works nicely if the original bitmap is square, what about non square bitmaps? – Apqu Dec 11 '13 at 14:31
  • 1
    As I also asked couple of years ago :), what is the issue you encounter? – Zelimir Dec 11 '13 at 16:39
  • 1
    Another possible way to rotate a bitmap, yet without creating a temporary bitmap on the heap (which may take a lot of space together with the original one), can be found here: https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations – android developer May 18 '14 at 12:26
10

essentially it can be boiled down to: do a(n inverse) canvas transformation instead of transforming drawable

private BitmapDrawable drawable; // or Drawable

protected void onDraw(Canvas canvas) { // inherited from View 
  //...
  canvas.save();
  canvas.rotate(degrees, pivotX, pivotY);
  drawable.draw(canvas);
  canvas.restore();
  //...
}

if you have BitmapDrawable it may be desirable to increase quality of the output by setting antialiasing

drawable.setAntialias(true);
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
jJ'
  • 3,038
  • 32
  • 25
3

Accepted answer doesn't work for me. I have non square image, so I changed his code a bit.

private Bitmap rotateDrawable(@DrawableRes int resId) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), resId);
    Bitmap bmpResult = Bitmap.createBitmap(bmpOriginal.getHeight(), bmpOriginal.getWidth(), Bitmap.Config.ARGB_8888);
    Canvas tempCanvas = new Canvas(bmpResult);
    int pivot = bmpOriginal.getHeight() / 2;
    tempCanvas.rotate(90, pivot, pivot);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmpResult;
}

mImageView.setImageBitmap(rotateDrawable(R.drawable.some_image));
Muzaffer
  • 1,457
  • 1
  • 13
  • 22
0

essentially this:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

source link:

http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
  • 1
    Not what the asker wanted, this rotates between a series of images, what was wanted was a planar rotation of a single image! – fredley Jan 21 '11 at 20:47
  • Actually, you can perform rotations (on a single Drawable) with a tween animation. http://developer.android.com/guide/topics/resources/animation-resource.html#Tween – edthethird Mar 14 '12 at 00:16