简体   繁体   中英

android load tab icon from internal memory

I need to change tab icon if selected or not. I am using following selector that works if I use images from drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/search_selected"
        android:state_selected="true" />
    <item android:drawable="@drawable/search_normal" />
</selector>

However, I need to use images from data/data... and use following code to load image from there. How to handle selector in order to load proper image (when selected or not) if I need to load it from internal memory? Thank you

ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);

File imgFile = new  File(pathImage);

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    icon.setImageBitmap(myBitmap); }

    //icon.setImageResource(R.drawable.tab_search;

Use StateListDrawables .

Get your both images as BitmapDrawable and do something like this:

StateListDrawable selector = new StateListDrawable();
selector.addState(new int[]{ android.R.attr.state_enabled }, yourDefaultBitmapDrawable);
selector.addState(new int[]{ android.R.attr.state_selected }, yourSelectedBitmapDrawable);
icon.setImageDrawable(selector);

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