【问题标题】:Check what image is displayed in an ImageView?检查 ImageView 中显示的图像是什么?
【发布时间】:2019-08-14 00:54:49
【问题描述】:

所以我知道你可以像下面这样设置 ImageView 的图像(假设 bun_picture 是一个 ImageView):

bun_picture.setImageResource(R.drawable.rawbun);

但是有什么方法可以检查 ImageView 当前显示的是什么图像? 类似的东西(注意:.getImageResource 不存在,我只是用它来完成我想做的事情):

if(bun_picture.getImageResource() == R.drawable.rawbun) == true){
do something}

显然这不起作用,但是我可以使用一些等效的方法吗?

谢谢

【问题讨论】:

标签: java android image android-imageview


【解决方案1】:

除非您在其中一个可绘制对象上调用了mutate(),否则您可以通过比较它们的ConstantState 字段来检查两个可绘制对象是否代表相同的图像。

ConstantState mine = bun_picture.getConstantState();
ConstantState other = ContextCompat.getDrawable(context, R.drawable.rawbun).getConstantState();

if (mine.equals(other)) {
    // they are the same
}

【讨论】:

    猜你喜欢
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多