Customize the vertical progress bar and dynamically change the color of the progress bar

Recently, a friend privately messaged me, why haven't I published a new article for so long? Harm, the truth is that I have been busy recently and have no time to publish an article
insert image description here
. I will publish an article as soon as I have time. Next, let’s not talk nonsense and go directly to the topic.
First, the custom progress bar is coming to us.
Let’s look at the code first

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

This is all the code of the custom vertical progress bar.
The next thing that comes to us is how to use the code in the 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"
            />

There are some constraint attributes added when I use it, you can ignore it,
and then there is the background problem of the progress bar. The background I am talking about here is the part of the progress bar that has slid and the part that has not slid

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

Finally, it is to dynamically change the color of the progress bar sliding

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

The above is all the code, simple and easy to understand, use it if you need it, please point out if you have any questions, hold your fist

Guess you like

Origin blog.csdn.net/m0_46366678/article/details/126642705