内部存储器中的android加载选项卡图标

时间:2012-03-30 10:04:18

标签: java android eclipse android-layout

如果选中或不选中,我需要更改标签图标。我正在使用跟随选择器,如果我使用drawable文件夹中的图像。

<?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>

但是,我需要使用来自数据/数据的图像...并使用以下代码从那里加载图像。如果我需要从内部存储器加载选择器以便加载正确的图像(如果选择与否)?谢谢

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;

1 个答案:

答案 0 :(得分:1)

使用StateListDrawables

将您的两张图片都设为BitmapDrawable并执行以下操作:

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