结构:

Activity:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.top5.MainActivity"> <com.example.top5_3.MyProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max=""
style="@android:style/Widget.ProgressBar.Horizontal"/>
</RelativeLayout>

JAVA:

MainActivity:

 package com.example.top5_3;

 import android.app.Activity;
import android.os.Handler;
import android.os.Message; import android.os.Bundle;
import android.view.Menu;
import android.widget.ProgressBar; public class MainActivity extends Activity {
private MyProgressBar pb; Handler hd = new Handler() {
public void handleMessage(Message msg) {
pb.setProgress(msg.what);
}
}; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (MyProgressBar) findViewById(R.id.pb);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = ; i <= ; i+=) {
Message mg = new Message();
mg.what = i;
hd.sendMessage(mg);
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
} public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

MyProgressBar:

 package com.example.top5_3;

 import java.text.MessageFormat;

 import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.widget.ProgressBar; public class MyProgressBar extends ProgressBar {
private Paint paint=new Paint();
public MyProgressBar(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyProgressBar(Context cont,AttributeSet attrs) {
// TODO Auto-generated constructor stub
super(cont,attrs);
}
protected synchronized void onDraw(Canvas cv){
super.onDraw(cv);
//进度天百分比
int reta=(int)((double)getProgress()/this.getMax()*);
//要绘制的文字
String tip=MessageFormat.format("{0}%",reta);
//设置文字对齐方式
paint.setTextAlign(Align.CENTER);
//设置文字颜色
paint.setColor(Color.BLUE);
//设置字体大小
paint.setTextSize();
//Canvas绘制文本时,使用 fontMetrics对象,计算坐标位置
FontMetrics fmt=paint.getFontMetrics();
int h=(int)((fmt.bottom-fmt.top)/-fmt.bottom);
//进行绘制
cv.drawText(tip, this.getWidth()/, this.getHeight()/+h, paint); }
}

运行效果:

最新文章

  1. 仿造slither.io第一步:先画条蛇
  2. July 13th, Week 29th Wednesday, 2016
  3. 对于java中的变量问题
  4. 其他主流开源硬件简介BeagleBone Black快速入门
  5. Makefile学习笔记
  6. 离散-ACM一道强有力的工具
  7. 《JavaScript基础教程(第8版)》PDF
  8. 【转】使用autolayout常见错误
  9. 面向UI编程:ui.js 1.0 粗糙版本发布,分布式开发+容器化+组件化+配置化框架,从无到有的艰难创造
  10. jQuery选择器(基本过滤选择器)第三节
  11. Python系列之 - 上下文管理协议
  12. UE4命令行使用,解释
  13. js判断访问浏览器是安卓还是ios还是微信浏览器还是微博
  14. CY7C68013 USB接口相机开发记录 - 第二天:驱动修改
  15. spark快速开发之scala基础之5高阶函数,偏函数,闭包
  16. BZOJ1968 [Ahoi2005]COMMON 约数研究 数论
  17. linux下如何按行将文件切割成多个小文件
  18. C++进阶--析构函数中的异常
  19. es6基础(7)--函数扩展
  20. 运行Xcode时,提示:An error was encountered while running (Domain = FBSOpenApplicationErrorDomain, Code = 4)

热门文章

  1. 笔记本电脑 联想 Thinkpad E420 无法打开摄像头怎么办
  2. redis 事务 及发布于订阅功能
  3. vbs io file
  4. 中文分词实践(基于R语言)
  5. Linux服务器 /var/spool/clientmqueue 目录下产生大量文件的删除办法
  6. [Unity-21] Prefab具体解释
  7. go4--break,continue + 标签
  8. Java文件实时监控Commons-io
  9. git服务器的建立
  10. 7. ExtJS.form中msgTarget