【问题标题】:How to Get Image From Image Resource Id如何从图像资源 ID 获取图像
【发布时间】:2015-05-30 06:16:55
【问题描述】:

我曾尝试过这样的事情我已经看到示例有它的可绘制名称并找到资源 id ..现在我不知道可绘制名称我只需要从 imgresourceId 中找到它

这是我的图片资源 ID

int id = Integer.parseInt(data.getStringExtra("imgId"));

05-30 12:09:33.648: D/imgid(5461): 2130837579

我想把它设置为编辑文本的可绘制右侧

所以我做了这样的事情

ed_operator.setCompoundDrawablesRelativeWithIntrinsicBounds( null, null, id, null );

第三个参数是可绘制的,那么如何将此图像资源ID转换为可绘制

有没有我可以应用的替代解决方案....

【问题讨论】:

  • 你也可以发布你的xml代码吗?你的头像叫什么名字?
  • 你在“myImageList”数组中有什么...?
  • 我有一组图片@shobhan
  • 我的意思是它们是资源 ID 或名称还是可绘制对象..?
  • 检查一下@shobhan pastie.org/10214699

标签: android image android-intent drawable android-drawable


【解决方案1】:

嘿,您将资源 ID(整数)作为额外的。并获得额外的字符串。把它变成

        int resID =  data.getIntExtra("imgId",-1);
        if(resID != -1 ){
          Drawable img = getResources().getDrawable( resID );
          img.setBounds( 0, 0, 60, 60 );

          ed_operator.setCompoundDrawablesRelativeWithIntrinsicBounds( null, null, img, null );//if you dont have images already in drawable 
          //OR
          ed_operator.setCompoundDrawables( null, null,img,null);//if you had set images in your drawable
        }

【讨论】:

  • 不,伙计..我试过一切都很好,但drawable没有设置为drawable right ...虽然它显示get drawable被贬低了
  • 我已经有一个用于edittext的正确drawable ..问题是我如何用我的drawable替换它..它没有替换
  • 我已经检查过了,但是 nthng hpnd 无论如何都可以替换现有图像或从右侧可绘制图像中删除图像..所以第一次我删除了我无法设置的现有图像
  • 我已经编辑了你的代码审查它..所以我可以接受它作为答案
  • ed_operator.setCompoundDrawables(null, null,img,null);如果您的编辑文本中已有可绘制对象,则可以应用它
【解决方案2】:

要从资源中获取图像,并且您知道它的 id ,您可以在 Activity 中执行以下操作:

// Get the id.
int id = Integer.parseInt(data.getStringExtra("imgId"));
// Get the drawable from the id.
Drawable d = getResources().getDrawable(id);

【讨论】:

  • 检查我的问题,我知道它的资源 ID ..如何将其转换为可绘制对象
  • 如果你知道资源id并且你想从资源id中获取drawable,这就是方法。你必须澄清一下。
  • setCompoundDrawablesRelative 的正确用法是 setCompoundDrawablesRelative(Drawable, Drawable, Drawable, Drawable) 而不是 setCompoundDrawablesRelative(int, int, int, int)。所以你必须先从id中获取drawable,然后将其设置为Edit Text
  • 这是我的资源 id 05-30 12:09:33.648: D/imgid(5461): 2130837579
  • 这是我的 img 资源 id 05-30 12:09:33.648: D/imgid(5461): 2130837579 如何将其转换为可绘制
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 2023-03-11
  • 2012-03-28
  • 1970-01-01
  • 2019-03-07
相关资源
最近更新 更多