프로그램/Android

둥근 프로그래스바 Circle ProgressBar

잡식성초보 2016. 9. 26. 15:18


지니모션으로 돌려서 나온 모향의 프로그래스바...

실제 폰으로 돌리면 정상적으로 나온다...


가져가서 쓰실때 댓글이라도 달아주세요~~~.~~~

public class CustomProgress extends ProgressBar {

private int mWidth = 13; //프로그래스바 굵기
private final RectF mRectF = new RectF();
private final Paint mprogressPaint = new Paint(); //프로그래스바
private final Paint mbackgroundPaint = new Paint(); //배경이 되는 프로그래스바

public CustomProgress(Context context) {

super(context);
Log.e("Custom", "CustomProgress1");
init(null, 0);
}

public CustomProgress(Context context, AttributeSet attrs) {

super(context, attrs);
Log.e("Custom", "CustomProgress2");
init(attrs, 0);
}

public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);
Log.e("Custom", "CustomProgress3");
init(attrs, defStyleAttr);
}

public void init(AttributeSet attrs, int style){

mprogressPaint.setAntiAlias(true);
mprogressPaint.setStyle(Paint.Style.STROKE);
mprogressPaint.setStrokeWidth(PixelUtil.dpToPx(getContext(), mWidth));

mbackgroundPaint.setAntiAlias(true);
mbackgroundPaint.setStyle(Paint.Style.STROKE);
mbackgroundPaint.setStrokeWidth(PixelUtil.dpToPx(getContext(), mWidth));

}

@Override
protected synchronized void onDraw(Canvas canvas) {

Matrix matrix = new Matrix();
Shader gradient;

Resources res = getResources();

int progress = getProgress();

float scale = getMax() > 0 ? (float)progress/getMax() *360: 0;
float[] position = {0, 90};

mbackgroundPaint.setColor(res.getColor(R.color.color_default));
canvas.drawArc(mRectF, 0, 360, false, mbackgroundPaint);

int[] color = {res.getColor(R.color.color_start), res.getColor(R.color.color_end)}; //처음색 그라데이션 들어가면서 변경될 색을 넣어준다

gradient = new SweepGradient(getWidth()/2 ,getHeight()/2, color , position);
matrix.postRotate(progress, getWidth()/2 ,getHeight()/2);
gradient.setLocalMatrix(matrix);
mprogressPaint.setShader(gradient);

canvas.drawArc(mRectF, 270, scale, false, mprogressPaint);

Log.e("Custom", "onDraw : " + scale);
super.onDraw(canvas);
}

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int min = Math.min(width, height);
setMeasuredDimension(min + 2 * mWidth, min + 2 * mWidth);

mRectF.set(mWidth, mWidth, min + mWidth, min + mWidth);
Log.e("Custom", "onMeasure");
}

@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
Log.e("Custom", "setProgress");
invalidate();
}

}

xml에는 indeterminateOnly 이게 필수~

<kr.com.kwon.myhealth.common.CustomProgress
android:id="@+id/customProgress"
android:layout_width="150dp"
android:layout_height="90dp"
android:indeterminateOnly="false" />



반응형