Untitled diff

Created Diff never expires
3 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
65 lines
3 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
65 lines
public class CustomProgressBarDemo extends View
public class CustomProgressBar extends View
{
{
// % value of the progressbar.
// % value of the progressbar.
int progressBarValue = 0;
int progressBarValue = 0;


public CustomProgressBarDemo(Context context)
public CustomProgressBar(Context context)
{
{
super(context);
super(context);
}
}


public CustomProgressBarDemo(Context context, AttributeSet attrs)
public CustomProgressBar(Context context, AttributeSet attrs)
{
{
super(context, attrs);
super(context, attrs);
progressBarValue = attrs.getAttributeIntValue(null, "progressBarValue", 0);
progressBarValue = attrs.getAttributeIntValue(null, "progressBarValue", 0);
}
}


public void setValue(int value)
public void setValue(int value)
{
{
progressBarValue = value;
progressBarValue = value;
invalidate();
invalidate();
}
}


protected void onDraw(Canvas canvas)
protected void onDraw(Canvas canvas)
{
{
super.onDraw(canvas);
super.onDraw(canvas);


float cornerRadius = 30.0f;
float cornerRadius = 30.0f;


// Draw the background of the bar.
// Draw the background of the bar.
Paint backgroundPaint = new Paint();
Paint backgroundPaint = new Paint();
backgroundPaint.setColor(Color.LTGRAY);
backgroundPaint.setColor(Color.LTGRAY);
backgroundPaint.setStyle(Paint.Style.FILL);
backgroundPaint.setStyle(Paint.Style.FILL);
backgroundPaint.setAntiAlias(true);
backgroundPaint.setAntiAlias(true);


RectF backgroundRect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
RectF backgroundRect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawRoundRect(backgroundRect, cornerRadius, cornerRadius, backgroundPaint);
canvas.drawRoundRect(backgroundRect, cornerRadius, cornerRadius, backgroundPaint);


// Draw the progress bar.
// Draw the progress bar.
Paint barPaint = new Paint();
Paint barPaint = new Paint();
barPaint.setColor(Color.GREEN);
barPaint.setColor(Color.GREEN);
barPaint.setStyle(Paint.Style.FILL);
barPaint.setStyle(Paint.Style.FILL);
barPaint.setAntiAlias(true);
barPaint.setAntiAlias(true);


float progress = (backgroundRect.width() / 100) * progressBarValue;
float progress = (backgroundRect.width() / 100) * progressBarValue;
RectF barRect = new RectF(0, 0, progress, canvas.getClipBounds().bottom);
RectF barRect = new RectF(0, 0, progress, canvas.getClipBounds().bottom);


canvas.drawRoundRect(barRect, cornerRadius, cornerRadius, barPaint);
canvas.drawRoundRect(barRect, cornerRadius, cornerRadius, barPaint);


// Draw progress text in the middle.
// Draw progress text in the middle.
Paint textPaint = new Paint();
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(34);
textPaint.setTextSize(34);


String text = progressBarValue + "%";
String text = progressBarValue + "%";
Rect textBounds = new Rect();
Rect textBounds = new Rect();


textPaint.getTextBounds(text, 0, text.length(), textBounds);
textPaint.getTextBounds(text, 0, text.length(), textBounds);


canvas.drawText(text,
canvas.drawText(text,
backgroundRect.centerX() - (textBounds.width() / 2),
backgroundRect.centerX() - (textBounds.width() / 2),
backgroundRect.centerY() + (textBounds.height() / 2),
backgroundRect.centerY() + (textBounds.height() / 2),
textPaint);
textPaint);


}
}
}
}