我知道,只要使用 myImaageView.setImaageResources(我的新图像可绘制)
,改变图像视图资源就没什么大不了了。
但我想做的是先检查当前图像来源,然后再修改它。
基本上,我想使用图像观察来执行我自己的团体无线电按钮。 所以每次点击任何图像View 时, 点播事件的方法将会改变我集团的图像资源 。
并为我可怜的英语感到抱歉
regards, redsea
我知道,只要使用 myImaageView.setImaageResources(我的新图像可绘制)
,改变图像视图资源就没什么大不了了。
但我想做的是先检查当前图像来源,然后再修改它。
基本上,我想使用图像观察来执行我自己的团体无线电按钮。 所以每次点击任何图像View 时, 点播事件的方法将会改变我集团的图像资源 。
并为我可怜的英语感到抱歉
regards, redsea
没有 get DrawableId
函数, 所以您在更改可提取文件时需要为 < code> ImageView code> 设置一个标签。 例如, 将可绘制的 ID 设为 < code> ImageView code > 的标签, 这样您就可以从标签中获取可绘制的 ID 。
我说90%的时间,你的观点不会 有任何标签在上面, 所以最简单的方式是 假设你的标签是唯一的标签:
myImageView.setTag(R.drawable.currentImage); //When you change the drawable
int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
Android 视图可以同时托管多个标签, 只要它们有独特的标识符。 您需要 < a href=" http:// developmenter. android. com/ guide/ potics/resources/ more-resources. html# Id" rel = “ noreferrer” > 创建一个独特的 id < / a > 资源, 并将其作为第一个参数加入 < code> setTag 方法调用 。 保留这样的代码 :
myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
To get Image source, you can use Drawable.ConstantState
.
For example, in my problem I was implementing a button click to change the ImageView.
I created two Drawable.ConstantState
objects and compared them.
The code is given below:
ImageView myImg=(ImageView) findViewById(R.id.myImg);
Drawable.ConstantState imgID = myImg.getDrawable().getConstantState();
Drawable.ConstantState imgID2 = getDrawable(R.drawable.otherImage).getConstantState();
if(imgID!=imgID2)
{
// Block of Code
}
else // Other Block of Code
您的意思是您打算检查资源代号以确定是否选中了一个无线电按钮吗? 虽然这可能有效, 但它不是一个非常好的设计。 一般而言, 您想要将模型从视图中分离出来( 通常您想要将模型从视图中分离出来( 或者称为 < a href=" http:// en. wikipedia. org/ wiki/ Model_view_ controller" rel= “ nofolp” > Model- View- current < / a > 范例 ) 。 要做到这一点, 您可能会让您的无线电按钮组为所选项目保留一个索引, 并且将每个项目存储在列表中 。 当选中一个项目时, 您可以决定其索引, 更新组模式, 然后更新 UI 以反映该变化 。
这并不是说,你提议的`他们'不会工作,只是设计不善,导致维护能力差(如资源名称改变)。
If you want get the src
drawable, use the following method of ImageView
:
http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()
在我尝试过后, 我发现有办法获得图像来源, 不需要标签属性。
Integer src = attrs.getAttributeResourceValue(
"http://schemas.android.com/apk/res/android", "src", -1);
The first parameter is namespace of android
and src
is the property name.
This method return the image resource id if it found or it will return -1;
你可以用这个
private Drawable colocaImgen(String nombreFile) {
Drawable res1 = null;
String uri1 = null;
try {
//First image
uri1 = "@drawable/" + nombreFile;
int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
res1 = getResources().getDrawable(imageResource1);
} catch (Exception e) {
// TODO Auto-generated catch block
//int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
res1 = getResources().getDrawable(R.drawable.globo2);// Default image
}
return res1;
}
然后
((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
我也有同样的问题,这是我的工作解决方案:
public void engLayoutExpand(View view) {
ImageView iv = (ImageView) view;
// One way:
Bitmap bm1 = ((BitmapDrawable) iv.getDrawable()).getBitmap();
Bitmap bm2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.baseline_expand_less_black_36)).getBitmap();
if (bm1 == bm2) {
iv.setImageResource(R.drawable.baseline_expand_more_black_36);
}
// Other way
Drawable.ConstantState cs1 = iv.getDrawable().getConstantState();
Drawable.ConstantState cs2 = getResources().getDrawable(R.drawable.baseline_expand_less_black_36).getConstantState();
if ( cs1 == cs2) {
iv.setImageResource(R.drawable.baseline_expand_more_black_36);
}
}
I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...
I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...
I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...
I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...
To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?
When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...
I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...
I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...