Recientemente, un amigo me envió un mensaje privado, ¿por qué no he publicado un nuevo artículo en tanto tiempo? Harm, la verdad es que he estado ocupado últimamente y
no tengo tiempo para publicar un artículo. Publicaré un artículo en cuanto tenga tiempo. A continuación, iré directo al tema sin decir tonterías. Primero que nada,
el la barra de progreso personalizada está llegando a nosotros.
Veamos el código
public class VerticalSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
private boolean mIsTouching;
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) {
if (w < h) {
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);
}
public void fresh() {
onSizeChanged(getWidth(),getHeight(), 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:{
mIsTouching = true;
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_UP:
mIsTouching = false;
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:
mIsTouching = false;
break;
}
return true;
}
public boolean isTouching() {
return mIsTouching;
}
}
Este es todo el código de la barra de progreso vertical personalizada.
Lo siguiente que nos viene es cómo usar el código en el drama.
<com.zsj.comminlibrary.view.VerticalSeekBar
android:id="@+id/sb_dim"
android:thumb="@null"
android:max="100"
android:background="@null"
android:progressDrawable="@drawable/dim_seek_bg1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_room_name"
android:layout_marginTop="@dimen/dp55"
android:layout_width="54dp"
android:layout_height="308dp"
/>
Hay algunos atributos de restricción agregados cuando lo uso, puede ignorarlo,
y luego está el fondo de la barra de progreso. El fondo del que estoy hablando aquí es la parte de la barra de progreso que se ha deslizado y la parte que no. se deslizó
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 进度条背景色 -->
<item android:id="@android:id/background">
<shape>
<solid
android:color="#19FFFFFF"
/>
<corners android:radius="7dp" />
<size
android:height="53dp"
/>
</shape>
</item>
<!-- 第一进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="#FFD284"
/>
<corners android:radius="7dp" />
<size
android:height="53dp"
/>
</shape>
</clip>
</item>
</layer-list>
Por último, es cambiar dinámicamente el color de la barra de progreso deslizante.
//设置进度条的滑动过的部分的颜色
verticalSeekBar.setProgressTintList(ColorStateList.valueOf(Color.HSVToColor(hsb)));
Lo anterior es todo el código, simple y fácil de entender, utilícelo si lo necesita, señale si tiene alguna pregunta, sostenga el puño