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(),
progress_Bitmap = BitmapFactory.decodeResource(getResources(),
currentStatus = STATUS_GOOD;
destRect = new Rect(0, 0, progress_Bitmap.getWidth() / 2,
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,
}
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),
}
@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);
}
}
}