GitHub有1300颗恒星的sample如何自定义seekbar
,但是没有办法将它从水平旋转到垂直...
当我尝试添加
时android:rotation="-90"
这是疯狂的旋转,但它仍然需要一个像水平seekbar
的空间。
下一步我尝试了seekbar
:
public class VerticalDiscreteSeekBar extends DiscreteSeekBar {
public VerticalDiscreteSeekBar(Context context) {
super(context);
}
public VerticalDiscreteSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalDiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
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;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".tools.TestDeleteIt">
<com.example.android.camera2basic.tools.VerticalDiscreteSeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
app:dsb_thumbColor="@color/color_white"
app:dsb_indicatorColor="@color/color_white"
app:dsb_indicatorFormatter="%03d"
app:dsb_indicatorTextAppearance="@style/CustomFloaterTextAppearance"
app:dsb_max="14"
app:dsb_min="-14"
app:dsb_progressColor="@color/ntz_color_yellow"
app:dsb_rippleColor="@color/ntz_color_yellow"
app:dsb_scrubberHeight="10dp"
app:dsb_thumbSize="25dp"
app:dsb_trackColor="@color/ntz_color_yellow"
app:dsb_trackHeight="10dip"/>
</LinearLayout>
最终看起来如此(它只显示形状,但没有真实视图)
我做错了什么?我怎么能让它运作起来?