Android 自定義UI-垂直方向的SeekBar
係統自帶的SeekBar樣式是水平的,如果需求一個垂直方向的效果就需要自定義了。原理很簡單,即定義一個類繼承於SeekBar,並在OnDraw方法裏麵旋轉一下視圖。
代碼如下:
- package android.widget;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- 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) {
- //將SeekBar轉轉90度
- c.rotate(-90);
- //將旋轉後的視圖移動回來
- c.translate(-getHeight(),0);
- Log.i("getHeight()",getHeight()+"");
- 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:
- int i=0;
- //獲取滑動的距離
- i=getMax() - (int) (getMax() * event.getY() / getHeight());
- //設置進度
- setProgress(i);
- Log.i("Progress",getProgress()+"");
- //每次拖動SeekBar都會調用
- onSizeChanged(getWidth(), getHeight(), 0, 0);
- Log.i("getWidth()",getWidth()+"");
- Log.i("getHeight()",getHeight()+"");
- break;
- case MotionEvent.ACTION_CANCEL:
- break;
- }
- return true;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:background="@android:color/white"
- android:orientation="horizontal" >
- <android.widget.VerticalSeekBar_Reverse
- android:id="@+id/seekbar_reverse"
- android:layout_width="wrap_content"
- android:layout_height="450dip"
- android:layout_marginRight="30dip" />
- <TextView
- android:id="@+id/reverse_sb_progresstext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/seekbar_reverse"
- android:gravity="center" />
- <android.widget.VerticalSeekBar
- android:id="@+id/vertical_Seekbar"
- android:layout_width="wrap_content"
- android:layout_height="450dip"
- android:layout_toRightOf="@+id/seekbar_reverse" />
- <TextView
- android:id="@+id/vertical_sb_progresstext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/vertical_Seekbar"
- android:layout_toRightOf="@+id/seekbar_reverse"
- android:gravity="center" />
- </RelativeLayout>

代碼下載
推薦博文:Android
Canvas編程:對rotate()和translate()兩個方法的研究
最後更新:2017-04-03 12:55:25