Ree*_*ena 32 xml android android-imageview
我想检查ImageView在xml中附加了哪个图像资源,我能够检查哪个图像资源附加到图像视图但我的要求是如何检查它是否ImageView具有我设置为xml的相同资源,基于我需要执行一些actions.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();
}
Run Code Online (Sandbox Code Playgroud)
Jit*_*yay 65
嗨请试试这个如下
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();
}
Run Code Online (Sandbox Code Playgroud)
请.getConstantState()用来比较
访问
http://developer.android.com/reference/android/graphics/drawable/Drawable.html
http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html
编辑:
.getResources().getDrawable(imageResource)
Run Code Online (Sandbox Code Playgroud)
在API21中被弃用,所以我更改了Jitesh Upadhyay的答案.
这是代码:
@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;
}
Run Code Online (Sandbox Code Playgroud)
Spr*_*ker 16
你可以做以下事情,
tag根据您的要求,通过xml或动态设置.
通过xml,
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageview1"
android:background="@drawable/bg"
android:tag="bg"/>
Run Code Online (Sandbox Code Playgroud)
动态,
ImageView imageView=(ImageView)findViewById(R.id.imageview1);
imageView.setTag("bg");
Run Code Online (Sandbox Code Playgroud)
使用 imageView.getTag()检索图像信息.
String backgroundImageName = String.valueOf(imageView.getTag());
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你ImageView经常改变背景,那么,
imageView.setTag(R.drawable.drawablename);
imageView.setImageResource(R.drawable.drawablename);
String backgroundImageName = String.valueOf(imageView.getTag());
Run Code Online (Sandbox Code Playgroud)
现在你可以进行检查了.
if (backgroundImageName.equals("bg")) // here "bg" is the tag that you set previously
{
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();
}
Run Code Online (Sandbox Code Playgroud)
kotlin 中的相同解决方案:
if (regProfile.drawable.constantState == ContextCompat.getDrawable(this, R.drawable.ivpic).constantState)
{
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40115 次 |
| 最近记录: |