Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top