简体   繁体   中英

How can i compare ImageView with R.drawable?

I want to check if one matrix with R.drawable items contains the current 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.

After calling the setImageResource for a given view, set that view tag with the same resource id (imageview.setTag(R.drawable.asset_id)).

You can get that id back by casting the call ((int) imageview.getTag()).

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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