Custom Drawable for ProgressBar/ProgressDialog

后端 未结 8 992
野性不改
野性不改 2020-11-22 10:33

Reading the limited documentation that Google has provided, I get the feeling that it is possible to change the look (drawable) of a ProgressBar/ProgressDialog by simply cre

相关标签:
8条回答
  • 2020-11-22 11:05

    i do your code .i can run but i need modify two places:

    1. name="android:indeterminateDrawable" instead of android:progressDrawable

    2. modify name attrs.xml ---> styles.xml

    0 讨论(0)
  • 2020-11-22 11:05
    public class CustomProgressBar {
        private RelativeLayout rl;
        private ProgressBar mProgressBar;
        private Context mContext;
        private String color__ = "#FF4081";
        private ViewGroup layout;
        public CustomProgressBar (Context context, boolean isMiddle, ViewGroup layout) {
            initProgressBar(context, isMiddle, layout);
        }
    
        public CustomProgressBar (Context context, boolean isMiddle) {
            try {
                layout = (ViewGroup) ((Activity) context).findViewById(android.R.id.content).getRootView();
            } catch (Exception e) {
                e.printStackTrace();
            }
            initProgressBar(context, isMiddle, layout);
        }
    
        void initProgressBar(Context context, boolean isMiddle, ViewGroup layout) {
            mContext = context;
            if (layout != null) {
                int padding;
                if (isMiddle) {
                    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
                    // mProgressBar.setBackgroundResource(R.drawable.pb_custom_progress);//Color.parseColor("#55000000")
                    padding = context.getResources().getDimensionPixelOffset(R.dimen.padding);
                } else {
                    padding = context.getResources().getDimensionPixelOffset(R.dimen.padding);
                    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
                }
                mProgressBar.setPadding(padding, padding, padding, padding);
                mProgressBar.setBackgroundResource(R.drawable.pg_back);
                mProgressBar.setIndeterminate(true);
                    try {
                        color__ = AppData.getTopColor(context);//UservaluesModel.getAppSettings().getSelectedColor();
                    } catch (Exception e) {
                        color__ = "#FF4081";
                    }
                    int color = Color.parseColor(color__);
    //                color=getContrastColor(color);
    //                color__ = color__.replaceAll("#", "");//R.color.colorAccent
                    mProgressBar.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP);
                } 
                }
    
                RelativeLayout.LayoutParams params = new
                        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                rl = new RelativeLayout(context);
                if (!isMiddle) {
                    int valueInPixels = (int) context.getResources().getDimension(R.dimen.padding);
                    lp.setMargins(0, 0, 0, (int) (valueInPixels / 1.5));//(int) Utils.convertDpToPixel(valueInPixels, context));
                    rl.setClickable(false);
                    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                } else {
                    rl.setGravity(Gravity.CENTER);
                    rl.setClickable(true);
                }
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                mProgressBar.setScaleY(1.55f);
                mProgressBar.setScaleX(1.55f);
                mProgressBar.setLayoutParams(lp);
    
                rl.addView(mProgressBar);
                layout.addView(rl, params);
            }
        }
    
        public void show() {
            if (mProgressBar != null)
                mProgressBar.setVisibility(View.VISIBLE);
        }
    
        public void hide() {
            if (mProgressBar != null) {
                rl.setClickable(false);
                mProgressBar.setVisibility(View.INVISIBLE);
            }
        }
    }
    

    And then call

    customProgressBar = new CustomProgressBar (Activity, true);
    customProgressBar .show();
    
    0 讨论(0)
提交回复
热议问题