您好,登錄后才能下訂單哦!
本篇內容主要講解“Android中如何使用progressbar實現帶底部指示器和文字進度條”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Android中如何使用progressbar實現帶底部指示器和文字進度條”吧!
列出源碼:
public class TextProgressBar extends LinearLayout { String text; Paint mPaint; private Rect textRect; private Bitmap bitmap; private ProgressBar progressBar; int progress; int proWidth, proHeight; public TextProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initText(context); } public TextProgressBar(Context context) { super(context); initText(context); } public TextProgressBar(Context context, AttributeSet attrs) { super(context, attrs); initText(context); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); progress = progressBar.getProgress(); if (progress == 0 || progress == progressBar.getMax()) return; this.mPaint.getTextBounds(this.text, 0, this.text.length(), textRect); proWidth = progressBar.getWidth(); proHeight = progressBar.getHeight(); //畫指示器 int bitmapx = (int) (progressBar.getLeft() + proWidth * ((progress * 1f) / progressBar.getMax())) - bitmap.getWidth() / 2; int bitmapy = proHeight + bitmap.getHeight() / 2; if (bitmapx < 0) bitmapx = progressBar.getLeft(); if(bitmapx > progressBar.getRight()-bitmap.getWidth()) bitmapx = progressBar.getRight()-bitmap.getWidth(); canvas.drawBitmap(bitmap, bitmapx, bitmapy, mPaint); //寫字 int tvx = (int) (progressBar.getLeft() + proWidth * ((progress * 1f) / progressBar.getMax())) - textRect.centerX(); int tvy = proHeight + bitmap.getHeight() * 2 + 4; if (tvx < 0) tvx = progressBar.getLeft(); if(tvx >= progressBar.getRight()-textRect.width()) tvx = progressBar.getRight()-textRect.width(); canvas.drawText(this.text, tvx, tvy + bitmap.getHeight(), this.mPaint); } //初始化,畫筆 private void initText(Context context) { View inflate = View.inflate(context, R.layout.textprogressbar, this); progressBar = (ProgressBar) inflate.findViewById(R.id.progressbar1); progressBar.setProgress(50); setWillNotDraw(false); this.mPaint = new Paint(); this.mPaint.setColor(getResources().getColor(R.color.red)); this.mPaint.setTextSize(20); textRect = new Rect(); text = "0"; bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.progress_top); } public void setProgress(int progress) { progressBar.setProgress(progress); } public void setText(String str) { text = str; } }
到此,相信大家對“Android中如何使用progressbar實現帶底部指示器和文字進度條”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。