自定义SeekBar设置拇指和progressDrawable

时间:2015-01-27 13:16:57

标签: android seekbar

我正在实现自定义SeekBar,因为我需要使用不同的progressDrawable和Thumb。我在资产中有一个文件夹,包含拇指和progreeDrawable的所有图像,我正在尝试将此图像加载到搜索栏。我用ImageView做了同样的技巧,问题是当我使用的不是图像而是xml文件。所以我决定使用StateList:

ics_play.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_activated="false"
        android:state_pressed="false"
        android:drawable="@drawable/ico_in_circle_play"/>
    <item
        android:state_activated="true"
        android:state_pressed="false"
        android:drawable="@drawable/ico_in_circle_pause" />
    <item
        android:state_activated="false"
        android:state_pressed="true"
        android:drawable="@drawable/ico_in_circle_play_hover"/>
    <item
        android:state_activated="true"
        android:state_pressed="true"
        android:drawable="@drawable/ico_in_circle_pause_hover" />
</selector>

自定义StateListDrawable:

public class CustomStateDrawable extends StateListDrawable {

    private final static String TAG = "LirisStateDrawable";
    public boolean inflate(AssetManager assetManager,String theme, String tag){

        Drawable d = null;
        Drawable d1 = null;
        Drawable d2 = null;
        Drawable d3 = null;

        try {
            d = Drawable.createFromStream(assetManager.open("liris_themes/"+theme+"/"+tag+"_f_f.png"), null);
            d1 = Drawable.createFromStream(assetManager.open("liris_themes/"+theme+"/"+tag+"_t_f.png"), null);
            d2 = Drawable.createFromStream(assetManager.open("liris_themes/"+theme+"/"+tag+"_f_t.png"), null);
            d3 = Drawable.createFromStream(assetManager.open("liris_themes/"+theme+"/"+tag+"_t_t.png"), null);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        addState(new int[]{-android.R.attr.state_activated,-android.R.attr.state_pressed}, d);
        addState(new int[]{android.R.attr.state_activated,-android.R.attr.state_pressed },  d1);
        addState(new int[]{-android.R.attr.state_activated,android.R.attr.state_pressed },  d2);
        addState(new int[]{android.R.attr.state_activated,android.R.attr.state_pressed },  d3);

        return true;
    }
}

然后我在我的自定义ImageView

中对它进行了充气
stateListDrawable.inflate(getResources().getAssets(),theme,tag)

它工作正常。 但是现在,我如何为seekbar实现它呢?

我正在使用xml文件设置搜索栏进度:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/rounded_corners_seekbar_transparent"/>
    <!--<item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="@drawable/rounded_corners_seekbar_transparent"
            android:scaleWidth="100%" />
    </item>-->
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/rounded_corners_seekbar_player"
            android:scaleWidth="100%" />
    </item>

</layer-list>

如果我有“id”属性,我应该如何使用StateListDrawable?:

addState(new int[]{android.R.attr.id????}, d);

当我使用imageView时,它是xml文件中的选择器,而不是图层列表。

我需要将LayerDrawable用于搜索栏

1 个答案:

答案 0 :(得分:1)

按xml: -

 <SeekBar
                android:id="@+id/wb_seekbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:progressDrawable="@drawable/wb_apptheme_scrubber_progress_horizontal_holo_light"
                android:thumb="@drawable/wb_apptheme_scrubber_control_selector_holo_light" />

动态/编程地更改Drawable:

    //setting wb seekbar dynamically
    RelativeLayout.LayoutParams wbLayoutParams = (RelativeLayout.LayoutParams)wbSeekBar.getLayoutParams();
    wbLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    wbSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.wb_apptheme_scrubber_progress_horizontal_holo_light));
    Drawable wbThumb = getResources().getDrawable(R.drawable.wb_apptheme_scrubber_control_selector_holo_light);
    wbThumb.setBounds(new Rect(0, 0, wbThumb.getIntrinsicWidth(),wbThumb.getIntrinsicHeight()));
    wbSeekBar.setPadding(padding, 0, padding, 0);
    wbSeekBar.setLayoutParams(wbLayoutParams);
    wbSeekBar.setThumb(wbThumb);

这里有一个很好的资源Android Holo颜色生成器,它将有助于更快地创建不同颜色的元素。只需按此 Link

选择颜色和元素即可