Android:如何将图像资源与R.drawable.imagename进行比较?

发布于 2024-11-15 20:50:36 字数 1125 浏览 3 评论 0原文

我正在开发一个示例应用程序,其中我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里泪两行 2024-11-22 20:50:36

您无法从 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.

找个人就嫁了吧 2024-11-22 20:50:36
if(((ImageView)v).getDrawable().getConstantState() == MainActivity.this
                        .getResources().getDrawable(R.drawable.unlock)
                        .getConstantState()))
{

// Do Something
}
if(((ImageView)v).getDrawable().getConstantState() == MainActivity.this
                        .getResources().getDrawable(R.drawable.unlock)
                        .getConstantState()))
{

// Do Something
}
离旧人 2024-11-22 20:50:36

正确的方法是使用 setTag()getTag。我猜你正在制作自己的适配器。

在方法 public View getView(intposition, View ConvertView, ViewGroupparent) 中,当您向其添加其他属性时,将标记设置为 Imageview

ImageView temp = (ImageView) v.findViewById(resId);
temp.setImageResource(icon);
temp.setTag(icon); //drawable unique ID will be my identifier here

然后在 onClick 中,您只需将 View v 与您的 drawable id 进行比较即可。例如,我想在点击时更改图像,我就这样编码了。 请注意当我设置新的可绘制对象时我如何更改标签

        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if ((Integer)v.getTag() == R.drawable.current_img) {
                    v.setBackgroundResource(R.drawable.new_img);
                    v.setTag(R.drawable.new_img);
                } 
            }
        });

The proper way is to user setTag() and getTag. I guess you are making your own adapter.

In the method public View getView(int position, View convertView, ViewGroup parent), you set tag to Imageview when you add other properties to it.

ImageView temp = (ImageView) v.findViewById(resId);
temp.setImageResource(icon);
temp.setTag(icon); //drawable unique ID will be my identifier here

Then in the onClick you simply compare View v with your drawable 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

        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if ((Integer)v.getTag() == R.drawable.current_img) {
                    v.setBackgroundResource(R.drawable.new_img);
                    v.setTag(R.drawable.new_img);
                } 
            }
        });
最初的梦 2024-11-22 20:50:36

你可以用这个。

  if((pic1.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.addpics).getConstantState()))
    {
      //do something
    }

You can use this.

  if((pic1.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.addpics).getConstantState()))
    {
      //do something
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文