【发布时间】:2015-01-27 13:16:56
【问题描述】:
我正在实现自定义 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 作为搜索栏
【问题讨论】:
-
你需要 StateListDrawable 做什么?
-
好的。现在我想我需要 LayerListDrawable
-
不,你需要LayerDrawable
-
你走错路了..把你的图片放在 res 文件夹下的 drawable 文件夹里.. 并相应地将这些图片检索到 seekbar 进度选择器文件中。
-
我需要这种方法来实现某些特定目标。我知道你在说什么,但这不利于我接下来要做的事情