歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現垂直型的SeekBar

Android實現垂直型的SeekBar

日期:2017/3/1 9:58:04   编辑:Linux編程

今天給大家推薦一個Android垂直型的SeekBar,可能對於你們在項目中有所幫助。這個已經有人具體實現。本人只是在這裡稍做推薦。有關更多的好的控件本人在網上已建了一個網站專門做Android開源控件的收錄以及示例代碼的各種使用用法,目的是幫助更多的Android開發者,讓更多的人愛上Android開發者。可以給出具體實現的思想及代碼。

相關閱讀:Android 垂直Seekbar【源碼】 http://www.linuxidc.com/Linux/2012-06/62090.htm

按以前寫作方式,首先上效果圖:

具體實現方式是繼續SeekBar,重寫onDraw方法只要旋轉90度就可以實現。

給出以上兩個其中的一個代碼:

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 extendsSeekBar {

public VerticalSeekBar(Context context) {
super(context);
}

public VerticalSeekBar(Context context, AttributeSet attrs, intdefStyle) {
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, intheightMeasureSpec) {
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:
int i=0;
i=getMax() - (int)(getMax() * event.getY() / getHeight());
setProgress(i);
Log.i("Progress",getProgress()+"");
onSizeChanged(getWidth(),getHeight(), 0, 0);
break;

case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}

}

Copyright © Linux教程網 All Rights Reserved