当前位置: 代码迷 >> 综合 >> 记一次 带 指示器的 progressbar
  详细解决方案

记一次 带 指示器的 progressbar

热度:21   发布时间:2023-10-20 14:57:19.0

老子最烦 没事 在开头逼逼的 ,直接上代码

1.控件

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);}@Overrideprotected 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);setWillNotDraw(false);this.mPaint = new Paint();this.mPaint.setColor(getResources().getColor(R.color.tishi));this.mPaint.setTextSize(25);textRect = new Rect();text = "0";bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ig_jindu_sanjiao);}public void setProgress(int progress) {progressBar.setProgress(progress);}public void setText(String str) {text = str;}}

 

2.布局 textprogressbar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"><ProgressBarandroid:id="@+id/progressbar1"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="3dp"android:max="100"android:progressDrawable="@drawable/progress_bar_layer_list"/>
</LinearLayout>

progress_bar_layer_list 的drawable文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:id="@android:id/background"><shape><corners android:radius="5dip" /><solid android:color="@color/common_gray"></solid></shape></item><item android:id="@android:id/progress"><clip><shape><corners android:radius="5dip" /><solid android:color="@color/lan"></solid></shape></clip></item></layer-list>

 

3.色值

<color name="lan">#0287FF</color>
<color name="text_gray">#999999</color>

4.指示箭头箭头(别找我要,我又不是美工)

 

 

5.使用方法

<com.qikeya.qkyxt.tools.TextProgressBarandroid:layout_marginLeft="@dimen/activity_left_right_padding"android:layout_marginRight="@dimen/activity_left_right_padding"android:id="@+id/prgress"android:layout_width="match_parent"android:layout_height="26dp"/>
prgress.setProgress(10);
prgress.setText("学习进度10%");

 

6.效果

记一次 带 指示器的 progressbar

 

记录完毕

 

 

 

 

 

 

 

  相关解决方案