在ImageView android中获取关联图像(Drawable)

发布于 2024-12-02 10:16:59 字数 227 浏览 3 评论 0原文

我已将图像设置为 android 中的 ImageView 控件:

iv.setImageResource(R.drawable.image1);

我想获取此关联的 Drawable,如下所示:

Drawable myDrawable = iv.getImageResource(); //wrong

I have set an image to an ImageView control in android:

iv.setImageResource(R.drawable.image1);

I want to get this associated Drawable, looks like:

Drawable myDrawable = iv.getImageResource(); //wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心在旅行 2024-12-09 10:16:59

您可以获得可绘制的资源,例如

Drawable myDrawable = iv.getDrawable();

您可以将其与可绘制的资源进行比较,例如

if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
    //do work here
}

You can get the drawable like

Drawable myDrawable = iv.getDrawable();

You can compare it with a drawable resource like

if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
    //do work here
}
你的笑 2024-12-09 10:16:59

首先定义Drawable。

Drawable myDrawable = getResources().getDrawable(R.drawable.image1);
iv.setImageDrawable(myDrawable);

Define the Drawable first.

Drawable myDrawable = getResources().getDrawable(R.drawable.image1);
iv.setImageDrawable(myDrawable);
遗忘曾经 2024-12-09 10:16:59

不幸的是,没有 getImageResource()getDrawableId()。但是,我使用 ImageView 标签创建了一个简单的解决方法。

在 onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

然后,如果你愿意,你可以创建一个简单的函数来获取可绘制的 id:

private int getImageResource(ImageView iv) {
    return (Integer) iv.getTag();
}

我希望这对你有帮助,它确实让我的工作更轻松。

Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.

In onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

Then, if you like, you can create a simple function to get the drawable id:

private int getImageResource(ImageView iv) {
    return (Integer) iv.getTag();
}

I hope this helps you, it sure made my work easier.

二智少女 2024-12-09 10:16:59

使用Drawabledrawable=imageview.getDrawable();

use Drawable drawable=imageview.getDrawable();

不羁少年 2024-12-09 10:16:59

您可以比较可绘制资源,例如

     if (imageView.getDrawable().getConstantState() == 
        ContextCompat.getDrawable(getContext(), R.drawable.image1).getConstantState()) {
}

从同一资源创建的 BitmapDrawables 将共享存储在其 ConstantState 中的唯一位图
https://developer.android.com/reference/android/graphics/可绘制/可绘制.ConstantState

getResources.getDrawable is deprecated so you can use ContextCompat.getDrawable inplace of it.

You can compare drawable resources like

     if (imageView.getDrawable().getConstantState() == 
        ContextCompat.getDrawable(getContext(), R.drawable.image1).getConstantState()) {
}

BitmapDrawables created from the same resource will for instance share a unique -bitmap stored in their ConstantState
https://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState

getResources.getDrawable is deprecated so you can use ContextCompat.getDrawable inplace of it.
南笙 2024-12-09 10:16:59

如果您可以使用视图的 id 成员变量,这是一个简单的方法:只需使用 v.setId() 存储 R.drawable id 即可。然后用 v.getId() 取回它。

This is an easy approach if you can use the view's id member variable: just store the R.drawable id using v.setId(). Then get it back with v.getId().

随波逐流 2024-12-09 10:16:59
if ((holder.fav.getDrawable().getConstantState()) == context.getResources().getDrawable(R.drawable.star_blank).getConstantState() ) {

  holder.fav.setImageDrawable(context.getResources().getDrawable(R.drawable.star_fill));

// comparison is true
} 

//getConstantState()是关键

if ((holder.fav.getDrawable().getConstantState()) == context.getResources().getDrawable(R.drawable.star_blank).getConstantState() ) {

  holder.fav.setImageDrawable(context.getResources().getDrawable(R.drawable.star_fill));

// comparison is true
} 

//getConstantState() is the key

幻想少年梦 2024-12-09 10:16:59

getResources().getDrawable() 现已弃用,只需使用 getDrawable(R.id.myImage);

getResources().getDrawable() is now deprecated, just use getDrawable(R.id.myImage);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文