最近公司需要,做了个小东西,根据活动梯度,分割显示进度条,本来想着,用shape画一个背景 但是后来感觉宽度不能控制,就用了黑魔法用ImageView来拼接,然后又是多梯度,这样的话 写的代码就有点冗余,干脆就简单封装了一下
效果图:
贴代码吧,感觉还是用shape比较靠谱,等有时间在研究一下
/** * 项目名称:super tools * 类描述:多梯度进度条 * 主要用于多梯度进度,进度可分为多梯度 * 背景采用多个imageView拼接而成, * 创建人:7号 * 创建时间:17/2/27 16:01 * 修改人:7号 * 修改时间:17/2/27 16:01 * 修改备注: */public class MultiGradientPRogressBar extends FrameLayout { //进度条 private ProgressBar mProgressBar; //进度条背景(多梯度背景) private LinearLayout mBackgroundGroup; //梯度背景分割 头背景(左边圆角) private ImageView mHeaderView; //梯度背景分割 头背景(右边圆角) private ImageView mFooterView; //单一梯度mGradientNum==0时 背景 private GradientDrawable mSingleShape; //单一梯度mGradientNum>=1时 头背景色 private GradientDrawable mHeaderShape; //单一梯度mGradientNum>=1时 尾背景色 private GradientDrawable mFooterShape; //四个圆角的顺序为左上,右上,右下,左下。如果X_Radius,Y_Radius为0表示还是直角。 private float[] leftRadius = new float[]{ 5, 5, 0, 0, 0, 0, 5, 5}; private float[] rightRadius = new float[]{ 0, 0, 5, 5, 5, 5, 0, 0}; //梯度 >=0 由布局传入 private int mGradientNum; //梯度背景色 由布局传入 private int mBgColor; //各梯度的间距 由布局传入 private int mMargins; //默认进度 由布局传入 private int mDefaultProgress; //梯度默认背景色 private int defaultColor = 0xFFFFFFFF; public MultiGradientProgressBar(Context context) { super(context); } public MultiGradientProgressBar(Context context, AttributeSet attrs) { super(context, attrs); setCustomAttributes(context, attrs); setupView(context, attrs); } public MultiGradientProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setCustomAttributes(context, attrs); setupView(context, attrs); } private void setCustomAttributes(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiGradientProgressBar); mGradientNum = a.getInteger( R.styleable.MultiGradientProgressBar_gradientNumber, 0); mBgColor = a .getColor(R.styleable.MultiGradientProgressBar_bgColor, defaultColor); mMargins = a .getInteger(R.styleable.MultiGradientProgressBar_space, (int) MyUtils.getRawSize(context, TypedValue.COMPLEX_UNIT_Dip, 3)); mDefaultProgress = a .getInteger(R.styleable.MultiGradientProgressBar_progress, 0); } /** * @param * @return * @throws * @Title: 初始化布局 * @Description: ${todo}() * @author chengfy * @date 17/2/27 19:36 */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void setupView(Context context, AttributeSet attrs) { mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progressbar, null); mBackgroundGroup = new LinearLayout(context); //初始化头梯度背景 左圆角 mHeaderView = new ImageView(context); LinearLayout.LayoutParams lpHeader = new LinearLayout.LayoutParams(context, attrs); lpHeader.weight = 1; lpHeader.width = 0; //这个不写 LayoutParams会失效 lpHeader.setMargins(0, 0, 0, 0); lpHeader.height = ViewGroup.LayoutParams.MATCH_PARENT; mHeaderView.setLayoutParams(lpHeader); //初始化尾梯度背景 右圆角 设置MarginsLeft mFooterView = new ImageView(context); LinearLayout.LayoutParams lpFooter = new LinearLayout.LayoutParams(context, attrs); lpFooter.weight = 1; lpFooter.width = 0; lpFooter.setMargins(mMargins, 0, 0, 0); lpFooter.height = ViewGroup.LayoutParams.MATCH_PARENT; mFooterView.setLayoutParams(lpFooter); //初始化纯一背景色 双头圆角 mSingleShape = new GradientDrawable(); mSingleShape.setCornerRadius(5); mSingleShape.setColor(mBgColor); //初始化头梯度背景 左圆角 mHeaderShape = new GradientDrawable(); mHeaderShape.setCornerRadii(leftRadius); mHeaderShape.setColor(mBgColor); mHeaderView.setBackgroundDrawable(mHeaderShape); //初始化尾梯度背景 右圆角 mFooterShape = new GradientDrawable(); mFooterShape.setCornerRadii(rightRadius); mFooterShape.setColor(mBgColor); mFooterView.setBackgroundDrawable(mFooterShape); //设置线性布局横向 mBackgroundGroup.setOrientation(LinearLayout.HORIZONTAL); initGradientView(context, attrs); setProgress(mDefaultProgress); this.addView(mBackgroundGroup); this.addView(mProgressBar); } /** * @param * @return * @throws * @Title: 设置多梯度背景 * @Description: ${todo}() * @author chengfy * @date 17/2/27 18:51 */ private void initGradientView(Context context, AttributeSet attrs) { mBackgroundGroup.removeAllViews(); switch (mGradientNum) { case 0://梯度0 break; case 1://梯度1 mBackgroundGroup.setBackgroundDrawable(mSingleShape); break; case 2://梯度2 //添加头 mBackgroundGroup.addView(mHeaderView); //添加尾 mBackgroundGroup.addView(mFooterView); break; default://梯度>=3 //身体背景数量 int bodyNum = mGradientNum - 2; //添加头 mBackgroundGroup.addView(mHeaderView); //添加身体 身体背景色为纯色无圆角 LinearLayout.LayoutParams lpBody = new LinearLayout.LayoutParams(context, attrs); lpBody.weight = 1; lpBody.width = 0; lpBody.setMargins(mMargins, 0, 0, 0); lpBody.height = ViewGroup.LayoutParams.MATCH_PARENT; for (int i = 0; i < bodyNum; i++) { ImageView bodyView = new ImageView(context); bodyView.setLayoutParams(lpBody); bodyView.setBackgroundColor(mBgColor); mBackgroundGroup.addView(bodyView); } //添加尾 mBackgroundGroup.addView(mFooterView); break; } } /** * @param * @return * @throws * @Title: 设置进度 * @Description: ${todo}() * @author chengfy * @date 17/2/27 19:09 */ public void setProgress(int progress) { if (mProgressBar != null) { mProgressBar.setProgress(progress); } }}布局文件:<songxiaocai.mylibrary.MultiGradientProgressBar android:layout_width="match_parent" android:layout_height="10dp" android:layout_margin="20dp" android:layout_marginTop="10dp" android:background="@color/white" multiGradientProgressBar:bgColor="#def8cf" multiGradientProgressBar:gradientNumber="1" multiGradientProgressBar:progress="10" multiGradientProgressBar:progressDrawable="@drawable/pbar_green_cir_transparent" multiGradientProgressBar:space="4"></songxiaocai.mylibrary.MultiGradientProgressBar> <songxiaocai.mylibrary.MultiGradientProgressBar android:layout_width="match_parent" android:layout_height="10dp" android:layout_margin="20dp" android:layout_marginTop="10dp" android:background="@color/white" multiGradientProgressBar:bgColor="#def8cf" multiGradientProgressBar:gradientNumber="2" multiGradientProgressBar:progress="10" multiGradientProgressBar:progressDrawable="@drawable/pbar_green_cir_transparent" multiGradientProgressBar:space="3"></songxiaocai.mylibrary.MultiGradientProgressBar> <songxiaocai.mylibrary.MultiGradientProgressBar android:layout_width="match_parent" android:layout_height="10dp" android:layout_margin="20dp" android:layout_marginTop="10dp" android:background="@color/white" multiGradientProgressBar:bgColor="#def8cf" multiGradientProgressBar:gradientNumber="3" multiGradientProgressBar:progress="10" multiGradientProgressBar:progressDrawable="@drawable/pbar_green_cir_transparent" multiGradientProgressBar:space="3"></songxiaocai.mylibrary.MultiGradientProgressBar> <songxiaocai.mylibrary.MultiGradientProgressBar android:layout_width="match_parent" android:layout_height="10dp" android:layout_margin="20dp" android:layout_marginTop="10dp" android:background="@color/white" multiGradientProgressBar:bgColor="#def8cf" multiGradientProgressBar:gradientNumber="4" multiGradientProgressBar:progress="10" multiGradientProgressBar:progressDrawable="@drawable/pbar_green_cir_transparent" multiGradientProgressBar:space="3"></songxiaocai.mylibrary.MultiGradientProgressBar> <songxiaocai.mylibrary.MultiGradientProgressBar android:layout_width="match_parent" android:layout_height="10dp" android:layout_margin="20dp" android:layout_marginTop="10dp" android:background="@color/white" multiGradientProgressBar:bgColor="#def8cf" multiGradientProgressBar:gradientNumber="5" multiGradientProgressBar:progress="10" multiGradientProgressBar:progressDrawable="@drawable/pbar_green_cir_transparent" multiGradientProgressBar:space="3"></songxiaocai.mylibrary.MultiGradientProgressBar>
新闻热点
疑难解答