美文网首页
GradientProgressBar

GradientProgressBar

作者: 晴天忆雨 | 来源:发表于2021-12-23 22:31 被阅读0次
package com.novelot.lib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;


/**
 * @author V
 **/
public class GradientProgressBar extends LinearLayout {
    private View mPlayerBarProgress;
    private Drawable mProgressDrawable;

    public GradientProgressBar(Context context) {
        this(context, null);
    }

    public GradientProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.GradientProgressBar, defStyleAttr, 0);

        final Drawable progressDrawable = a.getDrawable(R.styleable.GradientProgressBar_progressDrawable);
        final int progress = a.getInt(R.styleable.GradientProgressBar_progress, 0);
        if (progressDrawable != null) {
            setProgressDrawable(progressDrawable);
        }

        updateProgress(progress);
    }


    private void init() {
        mPlayerBarProgress = new View(getContext());
        addView(mPlayerBarProgress);
    }


    public void updateProgress(int progress) {
        int parentWidth = getWidth();
        updateProgress(progress, parentWidth);
    }

    public void updateProgress(int progress, int parentWidth) {
        int width = (int) (parentWidth * ((float) progress / 100));
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
        mPlayerBarProgress.setLayoutParams(lp);
    }

    public void resetProgress() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        mPlayerBarProgress.setLayoutParams(lp);
    }


    public Drawable getProgressDrawable() {
        return mProgressDrawable;
    }

    public void setProgressDrawable(Drawable d) {
        this.mProgressDrawable = d;
        mPlayerBarProgress.setBackground(d);
    }

}

attrs.xml

    <!--自定义渐变色进度条-->
    <declare-styleable name="GradientProgressBar">
        <attr name="progressDrawable" format="color|reference"></attr>
        <attr name="progress" format="integer"></attr>
    </declare-styleable>

app:progressDrawable="@drawable/player_bar_progress_bg"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <solid android:color="#4565F8" />
        </shape>
    </item>

    <item android:right="1px">
        <shape>
            <solid android:color="#232C4F" />
        </shape>
    </item>

    <item android:right="1px">
        <shape android:shape="rectangle">
            <gradient
                android:endColor="#992085C8"
                android:startColor="#000058FF" />
        </shape>
    </item>

</layer-list>


相关文章

网友评论

      本文标题:GradientProgressBar

      本文链接:https://www.haomeiwen.com/subject/ybmkqrtx.html