本文实例为大家分享了Android实现带指示器和文字的进度条,供大家参考,具体内容如下

根据项目要求需要实现以下效果:

列出源码:

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; }}

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

更多相关文章

  1. 灵活使用Android中ActionBar和ViewPager切换页面
  2. Android实现从activity中停止Service的方法
  3. Android下拉列表spinner的实例代码
  4. android 仿qq分组
  5. 第六天android:手写笔和水波纹
  6. Android(安卓)HorizontalScrollView和ScrollView 水平滚动 垂直
  7. Android(安卓)短信 彩信 wap push的接收
  8. Android轮播图 banner,图片视频混播banner
  9. Android实现EventBus登录界面与传值(粘性事件)

随机推荐

  1. Android ImageSwitcher
  2. android判断手机有没有安装微博
  3. java android 获取手机操作系统相关信息
  4. android图层划分(PhoneWindowManager.win
  5. Android读取XML文件(SAX)
  6. android 跳转并传递参数
  7. openGL ES学习教程 ---基本图像绘制
  8. android通知栏响应事件
  9. WiFi的使用Demo
  10. Android 定时启动应用