Seb
Seb

Reputation: 420

Android bitmap rotate canvas crops/cuts image

bmpAndroidMarker = BitmapFactory.decodeResource(context.getResources(), R.drawable.t_move2);
                        bmpAndroidMarkerResult = Bitmap.createBitmap(bmpAndroidMarker.getWidth(), bmpAndroidMarker.getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas tempCanvas = new Canvas(bmpAndroidMarkerResult);
                        tempCanvas.rotate(direction+45, bmpAndroidMarker.getWidth()/2, bmpAndroidMarker.getHeight()/2);
                        tempCanvas.drawBitmap(bmpAndroidMarker, 0, 0, null);

This it the code I have written (borrowed). The icon is generated within an imageview, inside a listview.

My problem is that on rotating this 'arrow', it seems that it 'clips' part of the far edges off, as if it were keeping the original bitmap's dimensions. I can't figure out how to allow it to 'overflow' and render the correct size image.

Is there some way of doing this?

Upvotes: 1

Views: 1979

Answers (2)

egg-hunter
egg-hunter

Reputation: 21

Try wrapping your ImageView in a FrameLayout and setting the frame layout's width and height as the size of the image you are trying to render. You can do this in the view's layout file like ...

<FrameLayout
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_width="YOUR IMAGE WIDTH"
        android:layout_height="YOUR IMAGE HEIGHT">
        <ImageView
            android:scaleType="matrix"
            android:layout_width="YOUR IMAGE WIDTH"
            android:layout_height="YOUR IMAGE HEIGHT"
            android:src="@drawable/YOUR IMAGE FILE" />
    </FrameLayout>

... or in a programmatic way by building your FrameLayout with your specified dimensions then adding your ImageView as a subview.

If your image is bigger than the screen you can use android:scaleType="matrix" to keep it from scaling and ensure your aspect ratio is retained.

This was originally answered by Pavlo Viazovskyy here. Credit where credit is due!

Upvotes: 1

Graeme
Graeme

Reputation: 25864

You're rotating the Image by 45 degrees so the resultant Bitmap should be the width of the original plus the originals diagonal width (Pythagoras' Theorem should be able to help).

AFAIK you'll need to do that Maths yourself when creating the result Bitmap as that's the canvas which is being drawn upon, rotating it's contents will not rotate the container.

Upvotes: 2

Related Questions