Android seekbar widget: no vertical orientation?
I can't believe there is no way to change the orientation of the SeekBar widget. I've been perusing this widget's properties and I can't find anything that开发者_JS百科 allows me to change its orientation to vertical.
So, am I missing something obvious? Do I have to write my own implementation of a seekbar to have its thumb slide up/down instead of left/right?
there are two way to achive the goal
1- Uing xml For api greater then api 11
<com.yourPackge.VerticalSeekBar
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:id="@+id/verticalseekbar"
android:max="10"
android:rotation="270"
/>
2- creating custom vertical seek bar
public class VerticalSeekBar extends SeekBar {
public VerticalSeekBar(Context context) {
super(context); }
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() /getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}}
if you want to create custom progress bar and thumb
In your activity where you want to add seekbar
<SeekBar
android:progressDrawable="@drawable/progress"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:max="10"
android:id="@+id/seekBar4"
android:thumb="@drawable/tumb_shape"/>
tump_shap.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="270"
android:endColor="#00ACE7"
android:startColor="#0086B3" />
<size
android:height="20dp"
android:width="20dp" />
</shape>
progress.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/background_fill"/>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/progress_fill" />
</item>
</layer-list>
精彩评论