- 本文即收藏,转载比较好的:
- 在转载上作下说明: <item android:id="@android:id/progress">
即是小球走过后的颜色,可以理解为左边的颜色 - <item android:id="@android:id/background">
- 即是小球即将走的颜色,可以理解为右边的颜色
- <corners android:radius="0dip"/> 即是进度条的圆角,0为长方形(90度,直角)
- <SeekBar
- android:id="@+id/player_seekbar"
- android:layout_width="245px"
- android:layout_height="25px"
- android:progressDrawable="@drawable/seekbar_style"
- android:thumb="@drawable/thumb"
- android:paddingLeft="16px"
- android:paddingRight="15px"
- android:paddingTop="5px"
- android:paddingBottom="5px"
- android:progress="0"
- android:max="0"
- android:secondaryProgress="0"
- />
<SeekBar android:id="@+id/player_seekbar" android:layout_width="245px" android:layout_height="25px" android:progressDrawable="@drawable/seekbar_style" android:thumb="@drawable/thumb" android:paddingLeft="16px" android:paddingRight="15px" android:paddingTop="5px" android:paddingBottom="5px" android:progress="0" android:max="0" android:secondaryProgress="0" />
android:progressDrawable="@drawable/seekbar_style"背景条
seekbar_style配置如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
- <itemandroid:id="@android:id/background">
- <shape>
- <cornersandroid:radius="5dip"/>
- <gradient
- android:startColor="#ff9d9e9d"
- android:centerColor="#ff5a5d5a"
- android:centerY="0.75"
- android:endColor="#ff747674"
- android:angle="270"
- />
- </shape>
- </item>
- <itemandroid:id="@android:id/secondaryProgress">
- <clip>
- <shape>
- <cornersandroid:radius="5dip"/>
- <gradient
- android:startColor="#80ffd300"
- android:centerColor="#80ffb600"
- android:centerY="0.75"
- android:endColor="#a0ffcb00"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
- <itemandroid:id="@android:id/progress">
- <clip>
- <shape>
- <cornersandroid:radius="5dip"/>
- <gradient
- android:startColor="#ff0099CC"
- android:centerColor="#ff3399CC"
- android:centerY="0.75"
- android:endColor="#ff6699CC"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
- </layer-list>
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ff9d9e9d" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:angle="270" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ff0099CC" android:centerColor="#ff3399CC" android:centerY="0.75" android:endColor="#ff6699CC" android:angle="270" /> </shape> </clip> </item> </layer-list>
或者:用图片如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
- <itemandroid:id="@android:id/background"
- android:drawable="@drawable/progress_bg"/>
- <itemandroid:id="@android:id/secondaryProgress"
- android:drawable="@drawable/second_progress">
- </item>
- <itemandroid:id="@android:id/progress"
- android:drawable="@drawable/first_progress">
- </item>
- </layer-list>
<?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/progress_bg" /> <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/second_progress"> </item> <item android:id="@android:id/progress" android:drawable="@drawable/first_progress"> </item> </layer-list>
方形
- <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
- <itemandroid:id="@android:id/background"
- android:drawable="@drawable/progress_bg"/>
- <itemandroid:id="@android:id/secondaryProgress">
- <clipandroid:drawable="@drawable/second_progress"/>
- </item>
- <itemandroid:id="@android:id/progress">
- <clipandroid:drawable="@drawable/first_progress"/>
- </item>
- </layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/progress_bg" /> <item android:id="@android:id/secondaryProgress"> <clip android:drawable="@drawable/second_progress" /> </item> <item android:id="@android:id/progress"> <clip android:drawable="@drawable/first_progress" /> </item> </layer-list>
android:thumb="@drawable/thumb"就是那个会动的球
配置如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <selectorxmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 按下状态-->
- <item
- android:state_focused="true"
- android:state_pressed="true"
- android:drawable="@drawable/thumb_pressed"/>
- <!-- 普通无焦点状态 -->
- <item
- android:state_focused="false"
- android:state_pressed="false"
- android:drawable="@drawable/thumb_normal"/>
- <!-- 有焦点状态-->
- <item
- android:state_focused="true"
- android:state_pressed="false"
- android:drawable="@drawable/thumb_focused"/>
- <!-- 有焦点 -->
- <item
- android:state_focused="true"
- android:drawable="@drawable/thumb_focused"/>
- </selector>
3.去掉THUMB 这个小球方式:
去掉布局中的:
android:thumb="@drawable/thumb" <SeekBar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="4dip" android:paddingLeft="16dip" android:paddingRight="16dip" android:layout_marginTop="4dip" android:focusable="false" android:focusableInTouchMode="false" android:clickable="false" android:progressDrawable="@drawable/seekbar" >然后在JAVA代码里面设置:SEEKBAR.setThumb(getThumb());
private ShapeDrawable getThumb() {
ShapeDrawable thumb = new ShapeDrawable(new RectShape());
thumb.getPaint().setColor(0x006cff);
thumb.setIntrinsicHeight(5);
thumb.setIntrinsicWidth(5);
return thumb;
}
demo java code:
package com.android.sharefres.five;
import android.app.Activity;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.widget.SeekBar;
public class SharefresFiveActivity extends Activity {
/** Called when the activity is first created. */
private SeekBar mSeekbar;
public void init(){
mSeekbar=(SeekBar) findViewById(R.id.seekbar);
mSeekbar.setThumb(getThumb());
}
private ShapeDrawable getThumb() {
ShapeDrawable thumb = new ShapeDrawable(new RectShape());
thumb.getPaint().setColor(0x006cff);
thumb.setIntrinsicHeight(5);
thumb.setIntrinsicWidth(5);
return thumb;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<SeekBar android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="4dip"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:layout_marginTop="4dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:progressDrawable="@drawable/seekbar"
/>
</LinearLayout>
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">
<shape>
<corners android:radius="5dip"/>
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip"/>
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip"/>
<gradient
android:startColor="#ff0099CC"
android:centerColor="#ff3399CC"
android:centerY="0.75"
android:endColor="#ff6699CC"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>