Skip to content

Instantly share code, notes, and snippets.

@junsuk5
Last active September 22, 2016 00:20
Show Gist options
  • Save junsuk5/ed012dcb5dc2b83fd75c2bbe16750919 to your computer and use it in GitHub Desktop.
Save junsuk5/ed012dcb5dc2b83fd75c2bbe16750919 to your computer and use it in GitHub Desktop.
커스텀 뷰 예제
package com.exam.order;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by junsuk on 2016. 9. 21..
*/
public class CustomView extends View {
private Paint mBackgroundPaint;
private Paint mForegroundPaint;
private int mMax = 3;
private int mProgress = 2;
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(Color.parseColor("#515560"));
mForegroundPaint = new Paint();
mForegroundPaint.setColor(Color.parseColor("#0990a1"));
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "onDraw: width " + getWidth() + " height " + getHeight());
float circleRadius = 10f;
float lineHeight = 6f;
float circleY = (getHeight() / 2);
float rectYstart = ((getHeight() / 2) - (lineHeight / 2));
float rectYend = ((getHeight() / 2) + (lineHeight / 2));
canvas.drawRect(circleRadius, rectYstart, getWidth() - circleRadius, rectYend, mBackgroundPaint);
canvas.drawCircle(circleRadius, circleY, circleRadius, mBackgroundPaint);
canvas.drawCircle(getWidth() - circleRadius, circleY, circleRadius, mBackgroundPaint);
float width = getWidth() / (mMax - 1);
int count = mMax - 2;
while (count > 0) {
canvas.drawCircle(width * count, circleY, circleRadius, mBackgroundPaint);
count--;
}
float progressWidth = 0f;
for (int i = 0; i < mMax; i++) {
if (i < mProgress) {
progressWidth = width * i;
if (i == 0) {
canvas.drawCircle(circleRadius, circleY, circleRadius, mForegroundPaint);
} else if (i == mMax - 1) {
canvas.drawCircle(progressWidth - circleRadius, circleY, circleRadius, mForegroundPaint);
} else {
canvas.drawCircle(progressWidth, circleY, circleRadius, mForegroundPaint);
}
}
}
if (progressWidth != 0) {
canvas.drawRect(circleRadius, rectYstart, progressWidth, rectYend, mForegroundPaint);
}
}
public void setMax(int max) {
mMax = max;
invalidate(); // onDraw 호출
}
public void setProgress(int progress) {
mProgress = progress;
invalidate();
}
public void setProgressColor(int color) {
mForegroundPaint.setColor(color);
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment