package com.example.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class MySeeBar extends androidx.appcompat.widget.AppCompatSeekBar {
public MySeeBar(Context context) {
super(context);
//让seebar处于最大值滑块的一半 setProgress(getMax()/2);
}
public MySeeBar(Context context, AttributeSet attrs) {
super(context, attrs);
setProgress(getMax()/2);
}
public MySeeBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setProgress(getMax()/2);
}
protected void onSizeChanged(int w,int h,int oldw,int oldh){
super.onSizeChanged(h,w,oldh,oldw);
}
//TODO 重新获取长度宽度
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),getMeasuredHeight());
}
//TODO 翻转seebar
protected void onDraw(Canvas c){
c.rotate(-90);
c.translate(-getHeight(),0);
super.onDraw(c);
}
//TODO 移动seebar滑块
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=50;
i=getMax()-(int)(getMax()*event.getY()/getHeight());
setProgress(i);
setMax(100);
onSizeChanged(getWidth(),getHeight(),0,0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
Android开发垂直seebar制作
于 2024-05-17 11:13:38 首次发布