Android:如何将图像资源与R.drawable.imagename进行比较?
我正在开发一个示例应用程序,其中我需要在 onClick 侦听器中获取图像视图的资源,并将其与我知道存在的图像源进行比较。如果资源相同,我想启动另一个意图。我现在面临的问题是访问 ImageView (及其资源 Id 整数)以与可绘制资源进行比较。
@Override
// should int be final ??
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is not working and I need to find a way to solve this >>
if (((ImageView)v).getResources().getInteger(0) == R.drawable.imageToCompare)
{
// do nothing
}
else
{
// do something
}
I am working on a sample application in which I need to get the resource of an image view in a onClick listener and compare that with the image source that I know exists. If the resources are the same, I want to launch another intent. The problem I am facing right now is to access that ImageView (and hence its resource Id integer) to compare to the drawable resource.
@Override
// should int be final ??
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is not working and I need to find a way to solve this >>
if (((ImageView)v).getResources().getInteger(0) == R.drawable.imageToCompare)
{
// do nothing
}
else
{
// do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法从
ImageView
获取可绘制 ID。但如果您从代码中设置它,您也可以将其存储在某个地方,例如标记字段中。看一下类似的问题:我将背景图像资源 TextView 与 R.drawable.bg_image 进行比较。You can't get a drawable id from an
ImageView
. But if you set it from code, you can also store it somewhere, for example in the tag field. Take a look at the similar question: Who I compare an background Image resource TextView with a R.drawable.bg_image for switch.正确的方法是使用
setTag()
和getTag
。我猜你正在制作自己的适配器。在方法
public View getView(intposition, View ConvertView, ViewGroupparent)
中,当您向其添加其他属性时,将标记设置为Imageview
。然后在
onClick
中,您只需将View v
与您的drawable
id 进行比较即可。例如,我想在点击时更改图像,我就这样编码了。 请注意当我设置新的可绘制对象时我如何更改标签The proper way is to user
setTag()
andgetTag
. I guess you are making your own adapter.In the method
public View getView(int position, View convertView, ViewGroup parent)
, you set tag toImageview
when you add other properties to it.Then in the
onClick
you simply compareView v
with yourdrawable
id. For example, I wanted to change image on click and I coded it this way. Take notice how I also change tag when I set a new drawable你可以用这个。
You can use this.