如何在android xml中检查哪个当前图像资源附加到ImageView? [英] How to check which current image resource is attached to ImageView in android xml?
问题描述
我想检查哪个图像资源附加到 xml 中的 ImageView
,我能够检查哪个图像资源附加到图像视图,但我的要求是如何检查 >ImageView
具有与我已设置为 xml 或不设置相同的资源,基于我需要执行一些操作.代码始终执行其他部分.以下是我的代码,
I want to check which image resource is attached to ImageView
in xml, I am able to check that which image resource is attached to image view but my requirement is to how to check that the ImageView
has the same resource which I have set into xml or not, based on that I need to perform some actions.Code always executes else part. Following is my code,
if (regProfile.getDrawable() == getResources().getDrawable( R.drawable.ivpic))
{
Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show();
// new RegisterAsyntask().execute();
}
推荐答案
您好,请按以下方式试一试
Hi please have a try with this as follows
if (regProfile.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ivpic).getConstantState())
{
Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show();
// new RegisterAsyntask().execute();
}
请使用.getConstantState()
进行比较
访问
http://developer.android.com/reference/android/图形/drawable/Drawable.html
http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html
.getResources().getDrawable(imageResource)
在 API21 中已弃用,因此我更改了 Jitesh Upadhyay 的答案.
Is deprecated in API21, so I changed Jitesh Upadhyay's answer.
代码如下:
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static boolean checkImageResource(Context ctx, ImageView imageView,
int imageResource) {
boolean result = false;
if (ctx != null && imageView != null && imageView.getDrawable() != null) {
ConstantState constantState;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
constantState = ctx.getResources()
.getDrawable(imageResource, ctx.getTheme())
.getConstantState();
} else {
constantState = ctx.getResources().getDrawable(imageResource)
.getConstantState();
}
if (imageView.getDrawable().getConstantState() == constantState) {
result = true;
}
}
return result;
}
这篇关于如何在android xml中检查哪个当前图像资源附加到ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!