Android:如何将图像资源与R.drawable.imagename进行比较? [英] Android: How to compare resource of an image with R.drawable.imagename?

查看:467
本文介绍了Android:如何将图像资源与R.drawable.imagename进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个示例应用程序,我需要在onClick侦听器中获取图像视图的资源,并将其与我知道存在的图像源进行比较。如果资源相同,我想启动另一个意图。我现在面临的问题是访问ImageView(以及它的资源Id整数)以与可绘制资源进行比较。

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         
                }


推荐答案

您无法从 ImageView 。但是如果你从代码中设置它,你也可以将它存储在某个地方,例如在标记字段中。看看类似的问题:我将背景图像资源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.

这篇关于Android:如何将图像资源与R.drawable.imagename进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆