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

最后都变了- 提交于 2019-12-29 04:40:35

问题


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

but what I want to do is to check the current ImageSource before changing it.

basically, I want to implement my own group radio buttons using imageViews. so every time any imageView is clicked, the oncliked event method will change the Image Resources of my group.

and sorry for my poor English.

regards, redsea


回答1:


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);



回答2:


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).




回答3:


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




回答4:


Actualy after my try ,I find there is a way to get source of imageView without need of tag property.

Integer src = attrs.getAttributeResourceValue(
                    "http://schemas.android.com/apk/res/android", "src", -1);

The first parameter is namespace of android and src is the property name. This method return the image resource id if it found or it will return -1;




回答5:


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"));



回答6:


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);
    }

}


来源:https://stackoverflow.com/questions/10726519/how-to-get-the-source-of-imageview-in-order-to-change-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!