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

后端 未结 7 1840
醉话见心
醉话见心 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:33

    Are you saying that you intend to check the resource id to determine if a radio button is selected or not? While this could probably work, it isn't a very good design. Generally speaking you want to separate the model from the view (otherwise known as the Model-View-Controller paradigm). To do this, you might have your radio button group maintain an index for the selected item, and have each of your items stored in a list. When an item is selected, you can determine its index, update the group's model, and then update the UI to reflect that change.

    Its not that what you're proposing wont work, it just isn't good design and leads to poor maintainability (such as if the resource names change).

    0 讨论(0)
  • 2020-12-15 06:42

    There is no getDrawableId function so you'll need to do something like set a tag for the ImageView when you change its drawable. For instance, set the drawable id as a tag for the ImageView so you could just get the drawable id from the tag.

    How to do that?

    I'd say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:

    myImageView.setTag(R.drawable.currentImage);    //When you change the drawable
    int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
    

    What if I already have a tag on my view

    Android views can host multiple tags at the same time, as long as they have a unique identifier. You'd need to create a unique id resource and add it as the first argument to the setTag method call. Leaving the code like this:

    myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
    int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
    
    0 讨论(0)
  • 2020-12-15 06:45

    To get Image source, you can use Drawable.ConstantState. For example, in my problem I was implementing a button click to change the ImageView. I created two Drawable.ConstantState objects and compared them. The code is given below:

    ImageView myImg=(ImageView) findViewById(R.id.myImg);
            Drawable.ConstantState imgID = myImg.getDrawable().getConstantState();
            Drawable.ConstantState imgID2 = getDrawable(R.drawable.otherImage).getConstantState();
            if(imgID!=imgID2)
            {
                // Block of Code
            }
            else // Other Block of Code 
    
    0 讨论(0)
  • 2020-12-15 06:46

    If you want get the src drawable, use the following method of ImageView : http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()

    0 讨论(0)
  • 2020-12-15 06:49

    You can use this.

     private Drawable colocaImgen(String nombreFile) {
        Drawable res1 = null;
        String uri1 = null;
        try {
            //First image
            uri1 = "@drawable/" + nombreFile;
            int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
            res1 = getResources().getDrawable(imageResource1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
            res1 = getResources().getDrawable(R.drawable.globo2);// Default image
        }
        return res1;
    }
    

    Then

    ((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
    
    0 讨论(0)
  • 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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题