網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)簡單垂直進度條的具體代碼,供大家參考,具體內(nèi)容如下
代碼注釋特別清晰,就不多解釋了
支持屬性:
- progress_radius ?? ?進度條圓角大小
- progress_border_enable ?進度條是否有邊框
- progress_gradient_enable 進度條顏色是否漸變
- progress_start_color ?? ?從上到下進度條開始的漸變色
- progress_end_color?? ?從上到下進度條結束的漸變色
- progress_solid_color ? ?帶邊框進度條的背景填充色
- progress_border_color?? ?進度條邊框的顏色
- progress_border_width ?? ?進度條邊框的寬度
有需要定義其他屬性的,可以進行擴充下面是效果圖
上代碼
VerticalProgress
public class VerticalProgress extends View {
? ? //進度條圓角
? ? private int mRadius;
? ? //進度條是否有邊框
? ? private boolean mBorderEnable;
? ? //是否有漸變色
? ? private boolean mGradientEnable;
? ? //漸變色
? ? private int mStartResId;
? ? private int mEndResId;
? ? //邊框的顏色
? ? private int mBorderColorResId;
? ? //進度條背景填充色
? ? private int mProgressBgColorId;
? ? //邊框寬度
? ? private int mBorderWidth;
? ? private int mProgress = 10;
? ? private int max = 100;
? ? private int mWidth;
? ? private int mHeight;
? ? private RectF mRectF;
? ? private Paint mPaint;
? ? public VerticalProgress(Context context) {
? ? ? ? super(context);
? ? ? ? init(context, null);
? ? }
? ? public VerticalProgress(Context context, @Nullable AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? init(context, attrs);
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? mWidth = getMeasuredWidth() - 1;// 寬度值
? ? ? ? mHeight = getMeasuredHeight() - 1;// 高度值
? ? }
? ? private void init(Context context, AttributeSet attrs) {
? ? ? ? TypedArray typedArray = null;
? ? ? ? if (attrs != null) {
? ? ? ? ? ? typedArray = context.obtainStyledAttributes(attrs, R.styleable.verticalProgress);
? ? ? ? ? ? mRadius = typedArray.getInt(R.styleable.verticalProgress_progress_radius, 0);
? ? ? ? ? ? mBorderEnable = typedArray.getBoolean(R.styleable.verticalProgress_progress_border_enable, false);
? ? ? ? ? ? mGradientEnable = typedArray.getBoolean(R.styleable.verticalProgress_progress_gradient_enable, true);
? ? ? ? ? ? mStartResId = typedArray.getResourceId(R.styleable.verticalProgress_progress_start_color, R.color.colorPrimary);
? ? ? ? ? ? mProgressBgColorId = typedArray.getResourceId(R.styleable.verticalProgress_progress_solid_color, R.color.white);
? ? ? ? ? ? mEndResId = typedArray.getResourceId(R.styleable.verticalProgress_progress_end_color, R.color.color_4EA6FD);
? ? ? ? ? ? mBorderColorResId = typedArray.getResourceId(R.styleable.verticalProgress_progress_border_color, R.color.color_4EA6FD);
? ? ? ? ? ? mBorderWidth = typedArray.getResourceId(R.styleable.verticalProgress_progress_border_width, 10);
? ? ? ? }
? ? ? ? if (typedArray != null) {
? ? ? ? ? ? typedArray.recycle();
? ? ? ? }
? ? ? ? mRectF = new RectF();
? ? ? ? mPaint = new Paint();
? ? ? ? mPaint.setAntiAlias(true);
? ? }
? ? @SuppressLint("DrawAllocation")
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? if (mRadius == 0) {
? ? ? ? ? ? //弧度為高度的一半
? ? ? ? ? ? mRadius = mWidth / 2;
? ? ? ? }
? ? ? ? if (mBorderEnable) {
? ? ? ? ? ? //第一層矩形(描邊層)
? ? ? ? ? ? mRectF.set(0, 0, mWidth, mHeight);
? ? ? ? ? ? //第一層矩形顏色(進度條描邊的顏色)
? ? ? ? ? ? mPaint.setColor(getResources().getColor(mBorderColorResId));
? ? ? ? ? ? //畫第一層圓角矩形
? ? ? ? ? ? canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
? ? ? ? ? ? //第二層矩形顏色(背景層顏色)
? ? ? ? ? ? mPaint.setColor(getResources().getColor(mProgressBgColorId));
? ? ? ? ? ? //第二層矩形(背景層)
? ? ? ? ? ? mRectF.set(mBorderWidth, mBorderWidth, mWidth - mBorderWidth, mHeight - mBorderWidth);
? ? ? ? ? ? //畫背景層圓角矩形(蓋在描邊層之上)
? ? ? ? ? ? canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
? ? ? ? }
? ? ? ? if (mProgress == 0)//進度為 0不畫進度
? ? ? ? ? ? return;
? ? ? ? float section = mProgress / max;
? ? ? ? //進度層底層
? ? ? ? mRectF.set(+8, mHeight - mProgress / 100f * mHeight + 10, mWidth - 8, mHeight - 8);
? ? ? ? if (mGradientEnable) {
? ? ? ? ? ? //漸變器
? ? ? ? ? ? LinearGradient shader = new LinearGradient(0, 0, mWidth * section, mHeight,
? ? ? ? ? ? ? ? ? ? getResources().getColor(mStartResId), getResources().getColor(mEndResId), Shader.TileMode.CLAMP);
? ? ? ? ? ? //第三層矩形顏色(進度漸變色)
? ? ? ? ? ? mPaint.setShader(shader);
? ? ? ? }
? ? ? ? //畫第三層(進度層)圓角矩形(蓋在背景層之上)
? ? ? ? canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
? ? ? ? //清除之前傳遞的shader
? ? ? ? mPaint.setShader(null);
? ? }
? ? public void setProgress(int currentCount) {
? ? ? ? this.mProgress = currentCount > max ? max : currentCount;
? ? ? ? postInvalidate();
? ? }
}
attr.xml樣式
<declare-styleable name="verticalProgress"> ? ? ? ? <attr name="progress_radius" format="dimension" /> ? ? ? ? <attr name="progress_border_width" format="dimension" /> ? ? ? ? <attr name="progress_gradient_enable" format="boolean" /> ? ? ? ? <attr name="progress_border_enable" format="boolean" /> ? ? ? ? <attr name="progress_start_color" format="color" /> ? ? ? ? <attr name="progress_solid_color" format="color" /> ? ? ? ? <attr name="progress_end_color" format="color" /> ? ? ? ? <attr name="progress_border_color" format="boolean" /> </declare-styleable>
最后調(diào)用示例
<com.widget.VerticalProgress ? ? ? ? ? ? android:id="@+id/vp_progress" ? ? ? ? ? ? android:layout_width="20dp" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? app:progress_border_enable="true" ? ? ? ? ? ? app:progress_solid_color="@color/white" ? ? ? ? ? ? android:layout_centerInParent="true" />
原文鏈接:https://blog.csdn.net/u013346208/article/details/83622599
相關推薦
- 2022-09-15 Nginx如何獲取自定義請求header頭和URL參數(shù)詳解_nginx
- 2023-07-28 nrm的安裝與配置及問題修復
- 2022-09-22 繼承關系下構造方法的訪問特點
- 2022-10-30 C語言算法練習之數(shù)組元素排序_C 語言
- 2022-05-01 Python中的bytes類型用法及實例分享_python
- 2023-01-23 使用Docker部署Dashdot服務器儀表盤的步驟_docker
- 2022-05-27 redis分布式Jedis類型轉換的異常深入研究_Redis
- 2022-11-08 Go中init()執(zhí)行順序詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支