如何通过位图交换图层列表中的图像?
这是我的位图
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
//Need some code to access "dynamicItem" and exchange it with my Bitmap
这是我的图层列表(没什么了不起的)。我想用我的位图交换动态项目。
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/dynamicItem"
android:drawable="@drawable/defaultItem" />
<item>
<bitmap
android:src="@drawable/some_stuff_on_top"
android:gravity="top|left" />
</item>
</layer-list>
Here is my Bitmap
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
//Need some code to access "dynamicItem" and exchange it with my Bitmap
And here is my layer list (nothing spectacular). I want to exchange the dynamicItem with my Bitmap.
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/dynamicItem"
android:drawable="@drawable/defaultItem" />
<item>
<bitmap
android:src="@drawable/some_stuff_on_top"
android:gravity="top|left" />
</item>
</layer-list>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LayerDrawable 中的 setDrawableByLayerId 方法采用 (int, Drawable) 而不是 (int, Bitmap),因此首先从位图创建一个可绘制对象
The method setDrawableByLayerId in LayerDrawable takes (int, Drawable) not (int, Bitmap), so first create a drawable from your bitmap
为了避免在移动图像时拉伸 您必须指定重力。就我而言,它有助于上述解决方案:
或以编程方式:
在屏幕上您可能会看到独立图像在左侧被拉伸和模糊
To avoid stretching while shifting the image You must specify gravity. In my case it helps with mentioned solution:
or programmatically:
On the screen You may see that standalone image is stretched and blur at the left side