0

I am trying to rotate a bitmap in a RemoteViews. However when I use either of the Matrix.setRotate methods or either of the Matrix.postRotate methods the Bitmap gets scaled wierd. Here is the code I am using to accomplish the task.

Bitmap bMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow);
Matrix m = new Matrix();
m.setRotate((float) 0, bMap.getWidth()/2, bMap.getHeight()/2);
bMap = Bitmap.createBitmap(bMap,0,0, bMap.getWidth(),bMap.getHeight(),m, true);

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.speedometer);
remoteView.setImageViewBitmap(R.id.speedoNeedle, bMap);

Here is the original layout file xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="136px"
android:layout_height="136px"
 >

<ImageView
        android:layout_width="136px"
        android:layout_height="136px"
        android:src="@drawable/dial"
        />
<ImageView
        android:id="@+id/speedoNeedle"
        android:layout_width="9px"
        android:layout_height="78px"
        android:layout_marginLeft="63px"
        android:layout_marginTop="27px"
        android:rotation="-138"
        android:src="@drawable/arrow" />
</RelativeLayout>

If I set the rotate value to zero or comment out the m.setRotate((float) 0, bMap.getWidth()/2, bMap.getHeight()/2) the bitmap displays correctly.

enter image description here

If i set the rotation value to 138 I get this: you can barely see the needle.

enter image description here

Here is a screenshot of the value at 184:

enter image description here

The needle isn't even visible at the max value of 276.

What am I doing wrong? How can I fix it?

Thanks in advance.

dherrin79
  • 367
  • 1
  • 4
  • 13

1 Answers1

1

Well, from the first point of view you've got the width of the speedoNeedle ImageView set to 9px, which does not seem to be enough for the entire range of angles of the arrow bitmap... isn't this the problem? As far as I understand, you're rotating the whole arrow bitmap around its center, therefore the dimensions of the rotated bitmap will vary... For the start, I'd try to set the width and height of the speedoNeedle ImageView to the same number.

  • That makes sense I will try that out and let you know if that works. Thanks. – dherrin79 Jul 18 '13 at 16:13
  • Yes this is the right idea. I tried it and it was better but still had some scaling issues I will have to make the needle the right size so that when the bit map is rotated 45 degrees the diagonal length is less than the overall width of the dial. And I think that will work. Thanks for your input. – dherrin79 Jul 18 '13 at 17:08
  • My comment above didn't quite work but I still believe the accepted answer is the right idea. If anyone has any thoughts it would be appreciated. – dherrin79 Jul 18 '13 at 17:17
  • You're welcome. I think that it should be enough to set the width and height to the actual width of the bitmap, because the worst cases are on 0 and 90 degreees (and of course, 180 and 270). Oh and i forgot to mention - try to set the parameter ScaleType of the ImageView to CENTER, it should perform no scaling then and put your bitmap to the center. – user2579825 Jul 18 '13 at 18:08