如何用位图的图层列表交换的图像? [英] How to exchange an image in a layer-list by a bitmap?

查看:117
本文介绍了如何用位图的图层列表交换的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的位图

 位图位= BitmapFactory.de codeStream((InputStream的)新的网址(URL).getContent());
//需要一些code访问dynamicItem,并与我的位图交流一下
 

这里是我的图层列表(没有壮观)。我想用我的位图交换dynamicItem。

 < XML版本=1.0编码=UTF-8&GT?;
    <层列表
      的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
      <项目
        机器人:ID =@ + ID / dynamicItem
        机器人:可绘制=@可绘制/ defaultItem/>
      <项目>
        <位图

          机器人:SRC =@可绘制/ some_stuff_on_top
          机器人:重力=顶|左/>
      < /项目>
    < /层列表>
 

解决方案

在LayerDrawable的方法setDrawableByLayerId需要(INT,绘制对象)没有(INT,位图),因此首先从位图上创建一个可绘制

 可绘制绘制=新BitmapDrawable(位);
layerDrawable.setDrawableByLayerId(R.id.dynamicItem,绘制);
 

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>

解决方案

The method setDrawableByLayerId in LayerDrawable takes (int, Drawable) not (int, Bitmap), so first create a drawable from your bitmap

Drawable drawable = new BitmapDrawable(bitmap);
layerDrawable.setDrawableByLayerId(R.id.dynamicItem, drawable);

这篇关于如何用位图的图层列表交换的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆