Skip to content

Instantly share code, notes, and snippets.

@hector6872
Created April 26, 2016 08:58
Show Gist options
  • Save hector6872/cc6ee3a7a1fef6c3fd2aeff0fe3eec20 to your computer and use it in GitHub Desktop.
Save hector6872/cc6ee3a7a1fef6c3fd2aeff0fe3eec20 to your computer and use it in GitHub Desktop.
ProgressView
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressView">
<attr name="max" format="integer"/>
<attr name="progress" format="integer"/>
<attr name="progressColor" format="color"/>
<attr name="backgroundColor" format="color"/>
</declare-styleable>
</resources>
public class ProgressView extends View {
private static final int DEFAULT_PROGRESS = 0;
private static final int DEFAULT_PROGRESS_MAX = 100;
private static final int DEFAULT_COLOR_BACKGROUND = Color.GRAY;
private static final int DEFAULT_COLOR_PROGRESS = Color.BLUE;
private Paint paintProgress;
private Paint paintProgressBackground;
private int height;
private int width;
private int max;
private int progress;
public ProgressView(Context context) {
this(context, null);
}
public ProgressView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, width, height, paintProgressBackground);
canvas.drawRect(0, 0, calculateProgressPosition(progress), height, paintProgress); // TODO
}
private void init(Context context, AttributeSet attrs) {
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
setMax(typedArray.getInt(R.styleable.ProgressView_max, DEFAULT_PROGRESS_MAX));
setProgress(typedArray.getInt(R.styleable.ProgressView_progress, DEFAULT_PROGRESS));
int progressColor =
typedArray.getColor(R.styleable.ProgressView_progressColor, DEFAULT_COLOR_PROGRESS);
int backgroundColor =
typedArray.getColor(R.styleable.ProgressView_backgroundColor, DEFAULT_COLOR_BACKGROUND);
typedArray.recycle();
paintProgressBackground = new Paint();
paintProgressBackground.setColor(backgroundColor);
paintProgressBackground.setStyle(Paint.Style.FILL);
paintProgressBackground.setAntiAlias(true);
paintProgress = new Paint();
paintProgress.setColor(progressColor);
paintProgress.setStyle(Paint.Style.FILL);
paintProgress.setAntiAlias(true);
}
private int calculateProgressPosition(int currentProgress) {
if (max == 0) {
return 0;
}
return (currentProgress * width) / max;
}
public void reset() {
progress = 0;
invalidate();
}
public int getMax() {
return max;
}
public void setMax(int max) {
if (max < 0) {
max = 0;
}
if (max < progress) {
max = progress;
}
this.max = max;
invalidate();
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
if (progress < 0) {
progress = 0;
}
if (progress > max) {
progress = max;
}
this.progress = progress;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment