package com.example.progress.demo;

 import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View; import com.example.firstapp.R; @SuppressLint("DrawAllocation")
public class ProgressButton extends View {
private FontMetrics fm;
private int progress = 0;
private int textColor = Color.WHITE;
private Paint paint;
private float textSize = 10;
private int foreground;
private int backgroundColor;
private String text;
private int max = 100;
private int corner = 5;// 圆角的弧度
private OnProgressButtonClickListener buttonClickListener; public ProgressButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
} public ProgressButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
} private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressButton);
this.backgroundColor = typedArray.getInteger(R.styleable.ProgressButton_background, Color.parseColor("#C6C6C6"));
this.foreground = typedArray.getInteger(R.styleable.ProgressButton_foreground, Color.rgb(20,131,214));
this.textColor = typedArray.getInteger(R.styleable.ProgressButton_textcolor, Color.WHITE);
this.max = typedArray.getInteger(R.styleable.ProgressButton_max, 100);
this.progress = typedArray.getInteger(R.styleable.ProgressButton_progress, 0);
this.text = typedArray.getString(R.styleable.ProgressButton_text);
this.textSize = typedArray.getDimension(R.styleable.ProgressButton_textSize, 20);
typedArray.recycle();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5); /**
* 绘制背景
*/
RectF oval = new RectF(0, 0, getWidth(), getHeight()); paint.setColor(this.backgroundColor);
canvas.drawRoundRect(oval, corner, corner, paint); /***
* 绘制进度值
*/ paint.setColor(foreground);
if (progress <= corner) {
oval = new RectF(0, corner - progress, getWidth() * this.progress / this.max, getHeight()
- corner + progress);
canvas.drawRoundRect(oval, progress,progress, paint);
} else {
oval = new RectF(0, 0, getWidth() * this.progress / this.max, getHeight());
canvas.drawRoundRect(oval, corner, corner, paint);
} /***
* 绘制文本
*/
if ("".equals(text) || text == null) {
return;
}
paint.setTextSize(this.textSize);
fm = paint.getFontMetrics();
paint.setColor(this.textColor); float textCenterVerticalBaselineY = getHeight() / 2 - fm.descent + (fm.descent - fm.ascent) / 2;
canvas.drawText(this.text, (getMeasuredWidth() - paint.measureText(this.text)) / 2, textCenterVerticalBaselineY,
paint); } /**
* 设置最大值
*
* @param max
*/
public void setMax(int max) {
this.max = max;
} /**
* 设置文本提示信息
*
* @param text
*/
public void setText(String text) {
this.text = text;
} /**
* 设置进度条的颜色值
*
* @param color
*/
public void setForeground(int color) {
this.foreground = color;
} /**
* 设置进度条的背景色
*/
public void setBackgroundColor(int color) {
this.backgroundColor = color;
} /***
* 设置文本的大小
*/
public void setTextSize(int size) {
this.textSize = size;
} /**
* 设置文本的颜色值
*
* @param color
*/
public void setTextColor(int color) {
this.textColor = color;
} /**
* 设置进度值
*
* @param progress
*/
public void setProgress(int progress) {
if(progress>max){
return;
}
this.progress=progress;
//设置进度之后,要求UI强制进行重绘
postInvalidate();
} public int getMax(){
return max;
} public int getProgress(){
return progress;
} @SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
buttonClickListener.onClickListener();
break;
default:
break;
}
return true;
} public void setOnProgressButtonClickListener(OnProgressButtonClickListener clickListener){
buttonClickListener = clickListener;
} public interface OnProgressButtonClickListener{
public void onClickListener();
}
<declare-styleable name="ProgressButton">
  <attr name="background" format="color" />
  <attr name="foreground" format="color" />
  <attr name="textcolor" format="color" />
  <attr name="max" />
  <attr name="progress" />
  <attr name="textSize" />
  <attr name="text" format="string" />
</declare-styleable>

  

 }


最新文章

  1. 菜鸟学Struts2——零配置(Convention )
  2. [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1
  3. 用Ghost进行备份还原
  4. Mysql运行SQL文件 错误Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
  5. Hibernate,JPA注解@Entity
  6. 每天2个android小例子----简单计算器源代码
  7. 【IE】trim()方法失效
  8. Oracle字符串操作[转:http://www.cnblogs.com/xd502djj/archive/2010/08/11/1797577.html]
  9. redux (一)
  10. GEETEST极验召集互联网大佬齐聚光谷,共同探讨交互安全问题
  11. 仿 ELEMENTUI 实现一个简单的 Form 表单
  12. day 21 内存管理,正则
  13. MySQL准备
  14. Nancy 寄宿IIS
  15. NODE获取节点删除空格的操作
  16. SharePoint 2007 页面及用户控件
  17. 文件上传---form表单,ajax,jquery,以及iframe无刷新上传 (processData,contentType讲解)
  18. iOS打包上传问题
  19. 使用httpClient调用接口获取响应数据
  20. java web中的多条件查询

热门文章

  1. Git CMD - pull: Fetch from and integrate with another repository or a local branch
  2. CSS3条件判断——@supports/window.CSS.supports()(转)
  3. ASP.NET Excel数据导出数据库
  4. 【转】iOS开发6:UIActionSheet与UIAlertView
  5. ios strong weak 的区别 与 理解
  6. Visual Studio2010 安装msdn
  7. jstl--c:choose标签
  8. Windows的命令行怎么支持通配符
  9. contos 安装jdk1.8
  10. java浮点数剖析