自定义竖向进度条且动态改变进度条的颜色

最近有小伙伴私信我咋这么久没出新文章?害,属实是最近忙的没时间出文章
在这里插入图片描述
这不一有时间就赶紧来出文章了,接下来废话不多说直接进入主题
首先向我们走来的是自定义的进度条
先看代码

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;
    }
}

这便就是自定义的竖着的进度条的全部代码了
接下来向我们走来的是在部剧里面如何使用的代码

  <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"
            />

里面有本人使用时加的一些约束属性什么的,大家可以忽略掉
再然后就是进度条的背景问题,我这里所说的背景就是进度条滑动过得部分以及未滑动的部分

<?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>

最后,就是动态改变进度条滑动过的颜色

//设置进度条的滑动过的部分的颜色
verticalSeekBar.setProgressTintList(ColorStateList.valueOf(Color.HSVToColor(hsb)));

以上便是全部代码了,简单易懂,有需要的就拿去用,有问题的话还烦请指出,抱拳了

猜你喜欢

转载自blog.csdn.net/m0_46366678/article/details/126642705