how to get the source of ImageView in order to change it?

后端 未结 7 1865
醉话见心
醉话见心 2020-12-15 06:23

I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)

but what I want to do is to chec

7条回答
  •  抹茶落季
    2020-12-15 06:56

    I had same problem and here is my working solutions:

    public void engLayoutExpand(View view) {
        ImageView iv = (ImageView) view;
    
        // One way:
        Bitmap bm1 = ((BitmapDrawable) iv.getDrawable()).getBitmap();
        Bitmap bm2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.baseline_expand_less_black_36)).getBitmap();
    
        if (bm1 == bm2) {
            iv.setImageResource(R.drawable.baseline_expand_more_black_36);
        }
    
        // Other way
        Drawable.ConstantState cs1 = iv.getDrawable().getConstantState();
        Drawable.ConstantState cs2 = getResources().getDrawable(R.drawable.baseline_expand_less_black_36).getConstantState();
    
        if ( cs1 == cs2) {
            iv.setImageResource(R.drawable.baseline_expand_more_black_36);
        }
    
    }
    

提交回复
热议问题