给progressbar设置drawable和自定义progressbar

在项目中遇见给progressbar直接设置drawable背景不起效果,解决方法如下:

public class SetProgressDrawable {
    private Context context;
    private ProgressBar progressBar;
    private int resId;

    public SetProgressDrawable(Context context, ProgressBar progressBar, int resId) {
        this.context = context;
        this.progressBar = progressBar;
        this.resId = resId;
    }

    @SuppressLint("NewApi")
    public void setProgressDrawable(){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
            progressBar.setProgressDrawableTiled(ContextCompat.getDrawable(context,resId));
        }else {
            Drawable layerDrawable = progressBar.getResources().getDrawable(resId);
            Drawable drawable = getMethod(progressBar, new Object[]{layerDrawable, false});
            progressBar.setProgressDrawable(drawable);
        }
    }

    private Drawable getMethod(Object object, Object[] paras) {
        Drawable newDrawable = null;
        try {
            Class<?>[] c = new Class[2];
            c[0] = Drawable.class;
            c[1] = boolean.class;
            String name = "tileify";
            @SuppressLint("PrivateApi")
            Method method = ProgressBar.class.getDeclaredMethod(name, c);
            method.setAccessible(true);
            newDrawable = (Drawable) method.invoke(object, paras);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return newDrawable;
    }
}

你可以这样用😄:

ProgressBar progressBar = findViewById(R.id.progress_bar_green);
        SetProgressDrawable setProgressDrawable = new SetProgressDrawable(context, progressBar, R.drawable.custom_progress_blue);
        setProgressDrawable.setProgressDrawable();
        progressBar.setProgress(50);

自定义背景的progressbar

<?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 android:shape="rectangle">
            <corners android:radius="4dp"/>
            <gradient android:startColor="#EFF3F7"
                android:endColor="#EFF3F7"/>
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <shape android:shape="rectangle">
                <corners android:radius="4dp"/>
                <gradient android:angle="45"
                    android:startColor="#42D673"
                    android:endColor="#42D673"/>
            </shape>
        </scale>
    </item>

</layer-list>

你可以这样用😄:

<ProgressBar
        android:id="@+id/progress_bar_green"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:layout_centerInParent="true"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/custom_progress_green" />

 

关注下,MVP的项目学这个就够了!!!
https://github.com/HaperSun/HarperAndroidMvp

posted on 2019-07-01 21:56  HarperSun  阅读(4046)  评论(0编辑  收藏  举报

导航