开发者

Rotate a bitmap inside a layer-list

I'm trying to rotate a bitmap which is contained in an item inside a layer-list. The layer-list is defined in an Android XML file. I would like to adjust the rotation of the bitmap from inside an Android activity. I understand that bitmaps can be rotated using a Matrix however I'm not sure how to update the bitmap contained inside the XML file.

My code so far is:

// I开发者_JAVA技巧nside Activity
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                R.drawable.face_line_green);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();

        Matrix matrix = new Matrix();
        matrix.postRotate(10);

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                width, height, matrix, true);

// Inside XML
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:top="6px">
            <bitmap android:src="@drawable/face_line_red" android:gravity="center" android:id="@+id/line_red" />
        </item>
        <item android:left="4px">
            <bitmap android:src="@drawable/face_line_green"
                android:gravity="center" android:id="@+id/line_green" />
        </item>
    </layer-list>


You can easily rotate Bitmap using a RotateDrawable. As a result, the easiest way to perform a Bitmap rotation is to wrap it in a RotateDrawable which will also be wrapped in your LayerListDrawable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜