线上生理属性进度条


public class InfoProgress extends View
{
private Bitmap background_Bitmap = null;
private Bitmap progress_Bitmap = null;
private int max = 0;
private double current = 0;
private Rect destRect = null;
private int width = 0;
private int height = 0;
private String percent = "";
private Paint text_Paint = null;
private int text_X = 0;
private int text_Y = 0;

private static final int STATUS_GOOD = 1;
private static final int STATUS_WARNING = STATUS_GOOD + 1;
private static final int STATUS_DEATHBED = STATUS_WARNING + 1;
private int currentStatus = STATUS_GOOD;

public InfoProgress(Context context)
{
super(context);
initResource();
}

public InfoProgress(Context context, AttributeSet attrs)
{
super(context, attrs);
initResource();
}

public InfoProgress(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initResource();
}

private void initResource()
{
background_Bitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.progress_green_bg);
progress_Bitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.progress_green);
currentStatus = STATUS_GOOD;
destRect = new Rect(0, 0, progress_Bitmap.getWidth() / 2,
        progress_Bitmap.getHeight());
text_Paint = new Paint();
text_Paint.setColor(Color.BLACK);
text_Paint.setTextSize(12);
width = progress_Bitmap.getWidth();
height = progress_Bitmap.getHeight();
text_X = progress_Bitmap.getWidth() / 2;
text_Y = progress_Bitmap.getHeight() - 1;
max = 100;
Random rand = new Random();
setProcess(rand.nextInt(100));
rand = null;
}

public void setMax(int max)
{
this.max = max;
}
public void setProcess(double process)
{
if(process < 0)
{
process = 0;
}
if(process > max)
{
process = max;
}
if(process != current)
{
current = process;
if(destRect == null)
{
destRect = new Rect(0, 0, progress_Bitmap.getWidth() / 2,
        progress_Bitmap.getHeight());
}
double value = (current / max);
int per = (int) (value * 100   + 0.5f) ;
changeBackground(per);
percent = per +"%";
if(text_Paint == null)
{
text_Paint = new Paint();
text_Paint.setColor(Color.BLACK);
text_Paint.setTextSize(12);
}
int w = (int) text_Paint.measureText(percent);
text_X = (width - w) / 2;
destRect.right = (int) (progress_Bitmap.getWidth() * (value));
this.invalidate();
}
}

private void changeBackground(int process)
{
int status = 0;
if(process > 30)
{
status = STATUS_GOOD;
}else if(process > 10)
{
status = STATUS_WARNING;
}else
{
status = STATUS_DEATHBED;
}

if(status != currentStatus)
{
currentStatus = status;
if(background_Bitmap != null)
{
background_Bitmap.recycle();
background_Bitmap = null;
}
if(progress_Bitmap != null)
{
progress_Bitmap.recycle();
progress_Bitmap = null;
}
switch(currentStatus)
{
case STATUS_GOOD:
{
background_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_green_bg);
progress_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_green);
break;
}
case STATUS_WARNING:
{
background_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_yellow_bg);
progress_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_yellow);
break;
}
case STATUS_DEATHBED:
{
background_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_red_bg);
progress_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_red);
break;
}
}
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Bitmap b = progress_Bitmap;
int dw = 0;
int dh = 0;
if (b != null)
{
dw = b.getWidth();
dh = b.getHeight();
}
setMeasuredDimension(resolveSize(dw, widthMeasureSpec),
        resolveSize(dh, heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(background_Bitmap != null)
{
canvas.drawBitmap(background_Bitmap, 0, 0, null);
}
if(progress_Bitmap != null)
{
canvas.drawBitmap(progress_Bitmap, null, destRect, null);
}
if(text_Paint != null)
{
canvas.drawText(percent, text_X, text_Y, text_Paint);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值