简体   繁体   English

如何比较ImageView与R.drawable?

[英]How can i compare ImageView with R.drawable?

I want to check if one matrix with R.drawable items contains the current ImageView. 我想检查一个具有R.drawable项的矩阵是否包含当前ImageView。

A small part of my code is: 我的代码的一小部分是:

        Integer[] redColor = { R.drawable.red_circle_, R.drawable.red_cross_};

        Random random = new Random();
        int randomItem = random.nextInt( redColor.length );

        item1ImageView.setImageResource( redColor[ randomItem1 ] );

        if ( ( Arrays.asList( redColor ).contains( item1ImageView.getId() )
        {
            //do something
        }

If you want to identify the imageviews by their drawable resource ids, you could make use of the view's tag. 如果要通过图像的可绘制资源ID标识图像视图,则可以使用视图的标签。

After calling the setImageResource for a given view, set that view tag with the same resource id (imageview.setTag(R.drawable.asset_id)). 在为给定视图调用setImageResource之后,请使用相同的资源ID(imageview.setTag(R.drawable.asset_id))设置该视图标记。

You can get that id back by casting the call ((int) imageview.getTag()). 您可以通过强制调用((int)imageview.getTag())来获取该ID。

The easy way is: 简单的方法是:

    Integer[] redColor = { R.drawable.red_circle_, R.drawable.red_cross_};

    Drawable.ConstantState[] redColorConstantState = new Drawable.ConstantState[ 3 ];

    for ( int counter = 0; counter < redColor.length; counter++ )
    {
        redColorConstantState[ counter ] = getResources().getDrawable( redColor[ counter ] ).getConstantState();
    }

    Random random = new Random();
    int randomItem = random.nextInt( redColor.length );

    item1ImageView.setImageResource( redColor[ randomItem1 ] );

    if ( ( Arrays.asList( redColorConstantState ).contains( item1ImageView.getDrawable().getConstantState() )
    {
        //do something
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM