Android自定义Material进度条效果

时间:2021-05-20

首先看下效果图

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:gravity="center" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <com.example.mytest.view.CircleProgressBar android:id="@+id/progress1" android:layout_width="60dp" android:layout_height="60dp" app:mlpb_progress_color="#566da9" app:mlpb_progress_stoke_width="3dp" /> </LinearLayout>

声明属性

<declare-styleable name="CircleProgressBar"> <attr name="mlpb_inner_radius" format="dimension"/> <attr name="mlpb_background_color" format="color"/> <attr name="mlpb_progress_color" format="color"/> <attr name="mlpb_progress_stoke_width" format="dimension"/> <attr name="mlpb_show_arrow" format="boolean"/> <attr name="mlpb_enable_circle_background" format="boolean"/> <attr name="mlpb_arrow_width" format="dimension"/> <attr name="mlpb_arrow_height" format="dimension"/> <attr name="mlpb_progress" format="integer"/> <attr name="mlpb_max" format="integer"/> <attr name="mlpb_progress_text_size" format="dimension"/> <attr name="mlpb_progress_text_color" format="color"/> <!--<attr name="mlpb_progress_text_offset" format="dimension"/>--> <attr name="mlpb_progress_text_visibility" format="enum"> <enum name="visible" value="0"/> <enum name="invisible" value="1"/> </attr> </declare-styleable>

自定义控件:

/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://pat.LAYER_TYPE_SOFTWARE, mBgCircle.getPaint()); mBgCircle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset, KEY_SHADOW_COLOR); final int padding = (int) mShadowRadius; // set padding so the inner image sits correctly within the shadow. setPadding(padding, padding, padding, padding); } mBgCircle.getPaint().setColor(mBackGroundColor); setBackgroundDrawable(mBgCircle); } mProgressDrawable.setBackgroundColor(mBackGroundColor); mProgressDrawable.setColorSchemeColors(mColors); mProgressDrawable.setSizeParameters(mDiameter, mDiameter, mInnerRadius <= 0 ? (mDiameter - mProgressStokeWidth * 2) / 4 : mInnerRadius, mProgressStokeWidth, mArrowWidth < 0 ? mProgressStokeWidth * 4 : mArrowWidth, mArrowHeight < 0 ? mProgressStokeWidth * 2 : mArrowHeight); if (isShowArrow()) { mProgressDrawable.showArrowOnFirstStart(true); mProgressDrawable.setArrowScale(1f); mProgressDrawable.showArrow(true); } super.setImageDrawable(null); super.setImageDrawable(mProgressDrawable); mProgressDrawable.setAlpha(255); if(getVisibility()==VISIBLE) { mProgressDrawable.start(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mIfDrawText) { String text = String.format("%s%%", mProgress); int x = getWidth() / 2 - text.length() * mTextSize / 4; int y = getHeight() / 2 + mTextSize / 4; canvas.drawText(text, x, y, mTextPaint); } } @Override final public void setImageResource(int resId) { } public boolean isShowArrow() { return mShowArrow; } public void setShowArrow(boolean showArrow) { this.mShowArrow = showArrow; } @Override final public void setImageURI(Uri uri) { super.setImageURI(uri); } @Override final public void setImageDrawable(Drawable drawable) { } public void setAnimationListener(Animation.AnimationListener listener) { mListener = listener; } @Override public void onAnimationStart() { super.onAnimationStart(); if (mListener != null) { mListener.onAnimationStart(getAnimation()); } } @Override public void onAnimationEnd() { super.onAnimationEnd(); if (mListener != null) { mListener.onAnimationEnd(getAnimation()); } } /** * Set the color resources used in the progress animation from color resources. * The first color will also be the color of the bar that grows in response * to a user swipe gesture. * * @param colorResIds */ public void setColorSchemeResources(int... colorResIds) { final Resources res = getResources(); int[] colorRes = new int[colorResIds.length]; for (int i = 0; i < colorResIds.length; i++) { colorRes[i] = res.getColor(colorResIds[i]); } setColorSchemeColors(colorRes); } /** * Set the colors used in the progress animation. The first * color will also be the color of the bar that grows in response to a user * swipe gesture. * * @param colors */ public void setColorSchemeColors(int... colors) { mColors = colors; if (mProgressDrawable != null) { mProgressDrawable.setColorSchemeColors(colors); } } /** * Update the background color of the mBgCircle image view. */ public void setBackgroundColor(int colorRes) { if (getBackground() instanceof ShapeDrawable) { final Resources res = getResources(); ((ShapeDrawable) getBackground()).getPaint().setColor(res.getColor(colorRes)); } } public boolean isShowProgressText() { return mIfDrawText; } public void setShowProgressText(boolean mIfDrawText) { this.mIfDrawText = mIfDrawText; } public int getMax() { return mMax; } public void setMax(int max) { mMax = max; } public int getProgress() { return mProgress; } public void setProgress(int progress) { if (getMax() > 0) { mProgress = progress; } } public boolean circleBackgroundEnabled() { return mCircleBackgroundEnabled; } public void setCircleBackgroundEnabled(boolean enableCircleBackground) { this.mCircleBackgroundEnabled = enableCircleBackground; } @Override public int getVisibility() { return super.getVisibility(); } @Override public void setVisibility(int visibility) { super.setVisibility(visibility); if (mProgressDrawable != null) { mProgressDrawable.setVisible(visibility == VISIBLE, false); if (visibility != VISIBLE) { mProgressDrawable.stop(); } else { if (mProgressDrawable.isRunning()) { mProgressDrawable.stop(); } mProgressDrawable.start(); } } } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mProgressDrawable != null) { mProgressDrawable.stop(); mProgressDrawable.setVisible(getVisibility() == VISIBLE, false); requestLayout(); } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mProgressDrawable != null) { mProgressDrawable.stop(); mProgressDrawable.setVisible(false, false); } } private class OvalShadow extends OvalShape { private RadialGradient mRadialGradient; private int mShadowRadius; private Paint mShadowPaint; private int mCircleDiameter; public OvalShadow(int shadowRadius, int circleDiameter) { super(); mShadowPaint = new Paint(); mShadowRadius = shadowRadius; mCircleDiameter = circleDiameter; mRadialGradient = new RadialGradient(mCircleDiameter / 2, mCircleDiameter / 2, mShadowRadius, new int[]{ FILL_SHADOW_COLOR, Color.TRANSPARENT }, null, Shader.TileMode.CLAMP); mShadowPaint.setShader(mRadialGradient); } @Override public void draw(Canvas canvas, Paint paint) { final int viewWidth = CircleProgressBar.this.getWidth(); final int viewHeight = CircleProgressBar.this.getHeight(); canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2 + mShadowRadius), mShadowPaint); canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2), paint); } } }

在java代码中设置进度条上的颜色值

progress1 = (CircleProgressBar) findViewById(R.id.progress1); progress1.setColorSchemeResources(android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章