tags:

views:

100

answers:

2

I want to show an arrow that indicates the direction towards a goal, using the orientation sensor and current GPS position. Everything works well, except that I want to rotate the arrow image in my ImageView.

The current code, which shows the arrow pointing upwards, is this:

ImageViewArrow.setImageResource(R.drawable.arrow);

What is the best solution for showing the arrow, rotated by N degrees?

I tried this, but it gave messed up graphics:

Matrix matrix = new Matrix(); 
matrix.postRotate(Rotation);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
  R.drawable.arrow); 
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
  bitmapOrg.getWidth(),bitmapOrg.getHeight(), matrix, true); 
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
InfoArrow.setScaleType(ScaleType.CENTER);
InfoArrow.setImageDrawable(bmd);
+1  A: 

here you can find a tutorial

http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

Roflcoptr
I tried that before posting the question, but I got wrong results. However, this is actually the solution - I just had to remove the line InfoArrow.setScaleType(ScaleType.CENTER);, then it looks right.
Lars D
That is terribly inefficient for something that is continually updating. For this kind of thing you really should just write a custom View that draws what you want. There is no need to abuse a standard class like ImageView like this; writing a custom View that draws a bitmap (and applies a rotation before doing so) is pretty simple.
hackbod
A: 

There is a matrix parameter in the createBitmap method that can be used for resizing and rotating the image. You could try something like the following:

    Matrix matrix = new Matrix(); 
    // to rotate the image bitmap use post Rotate
    matrix.postRotate(45); 


    Bitmap rotatedImage = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                      width, height, matrix, true); ` 
Andreas M.