旋转图层列表内的位图
我正在尝试旋转包含在 layer-list
内的 item
中的位图
。 layer-list
在 Android XML 文件中定义。我想从 Android 活动内部调整位图的旋转。我知道可以使用矩阵旋转位图,但我不确定如何更新 XML 文件中包含的位图。
到目前为止我的代码是:
// Inside 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>
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:
// Inside 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
RotateDrawable
轻松旋转Bitmap
。因此,执行Bitmap
旋转的最简单方法是将其包装在RotateDrawable
中,该 RotateDrawable 也将包装在LayerListDrawable
中。You can easily rotate
Bitmap
using aRotateDrawable
. As a result, the easiest way to perform aBitmap
rotation is to wrap it in aRotateDrawable
which will also be wrapped in yourLayerListDrawable
.