Android 사용자 정의 컨트롤 은 텍스트 와 숫자 가 있 는 원형 진도 바 를 실현 합 니 다.
실 현 된 효과 도 는 다음 과 같다.
첫 번 째 단계:아래 에 구멍 이 있 는 중 공 원 을 그 려 외곽 의 큰 호 라 고 합 니 다.
anvas.clipRect(0, 0, mWidth, mHeight / 2 + radius - textHeight * 3 / 4);
두 번 째 단계:원호 진도 조 를 그 릴 때의 시작 각 도 를 계산 하고 외곽 큰 호의 왼쪽 끝 점 을 진도 로 설정 하 는 것 은 출발점 이 될 만 한 가치 가 있 습 니 다.스 캔 한 각도 가 외곽 큰 호 를 차지 하 는 백분율 은 바로 진도 값 입 니 다.세 번 째 단계:숫자,문자,백분율 그리 기
STEP 4:Handler Runnable 과 DecelerateInterpolator 를 사용 하면 진도 표 와 숫자 가 움 직 입 니 다.
테스트 코드:
final CustomCircleBar circle=(CustomCircleBar)findViewById(R.id.win_home);
circle.setPercent(10);
circle.setCustomText(" ");
circle.setProgessColor(getResources().getColor(R.color.blue));
final Random random=new Random();
circle.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
circle.setPercent(random.nextInt(100));
}
});
완성 코드 는 다음 과 같 습 니 다:
public class CustomCircleBar extends View {
private Context context;
/**
*
*/
private int percent;
/**
*
*/
private int mProgessColor;
/**
*
*/
private String mCustomText;
/**
*
*/
private Paint paintBar = new Paint();
/**
*
*/
private Paint paintText = new Paint();
/**
*
*/
private TypedValue typedValue;
/**
*
*/
DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator();
/**
*
*/
private int duration = 10;
private int curTime = 0;
public CustomCircleBar(Context context) {
super(context);
this.context=context;
init();
}
public CustomCircleBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
init();
}
public CustomCircleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
init();
}
public void setPercent(int percent) {
this.percent = percent;
/*isShown():Returns the visibility of this view and all of its ancestors*/
if (isShown()) {
/**
*
*/
curTime=0;
this.invalidate();
}
}
public void setProgessColor(int mProgessColor) {
this.mProgessColor = mProgessColor;
if (isShown()) {
this.invalidate();
}
}
public void setCustomText(String mCustomText) {
this.mCustomText = mCustomText;
}
private Handler mHandler = new Handler();
private Runnable mAnimation = new Runnable() {
@Override
public void run() {
if (curTime < duration) {
curTime++;
/** , onDraw,onDraw
* mHandler.postDelayed(mAnimation, 20); ,
* 20 , 10 , 200
*/
CustomCircleBar.this.invalidate();
}
}
};
private void init() {
/**
* ,
*/
percent = 0;
mProgessColor=Color.rgb(95,112,72);
mCustomText="Home";
typedValue=new TypedValue();
context.getTheme().resolveAttribute(R.attr.maintextclor,typedValue,true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float mWidth = getWidth();
float mHeight = getHeight();
/**
*
*/
/** Restores the paint to its default settings. */
paintBar.reset();
/**
* 4
*/
paintBar.setStrokeWidth(4);
/**
*
*/
paintBar.setStyle(Paint.Style.STROKE);
paintBar.setAntiAlias(true);
paintBar.setColor(mProgessColor);
/**
* ,
*/
paintBar.setAlpha(80);
/**
*
*/
paintText.setTextSize(20);
paintText.setColor(getResources().getColor(typedValue.resourceId));
paintText.setStyle(Paint.Style.STROKE);
paintText.setAntiAlias(true);
/**
*
*/
paintText.setTextAlign(Paint.Align.CENTER);
/**
*
*/
Paint.FontMetrics fontMetrics = paintText.getFontMetrics();
/**
*
*/
float textHeight = fontMetrics.bottom - fontMetrics.top;
/**
*
*/
float radius = Math.min(mWidth, mHeight) / 2 - 10;
/* ❑ save: Canvas 。save , Canvas 、 、 、 、 。
❑ restore: Canvas 。 save Canvas 。*/
/* , */
canvas.save();
/*clipRect: ,
clipRect() , , */
canvas.clipRect(0, 0, mWidth, mHeight / 2 + radius - textHeight * 3 / 4);
/* clipRect , */
canvas.drawCircle(mWidth / 2, mHeight / 2, radius, paintBar);
/**
* ,
*/
float theta_offset = (float) Math.acos((radius - textHeight / 2) / radius);
/**
*
*/
float theta_full = 360 - 2 * theta_offset;
/**
*
*/
float thetaProcess = mDecelerateInterpolator.getInterpolation(1.0f * curTime / duration) * percent * theta_full / 100;
/**
*
*/
paintBar.setAlpha(255);
paintBar.setColor(mProgessColor);
/**
* , , ,
*/
canvas.drawArc(new RectF(mWidth / 2 - radius, mHeight / 2 - radius, mWidth / 2 + radius, mHeight / 2 + radius), theta_offset+90, thetaProcess, false, paintBar);
/**
*
*/
canvas.restore();
/**
*
*/
paintText.setTextSize(20);
fontMetrics = paintText.getFontMetrics();
float textBaseLineOffset = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
canvas.drawText(mCustomText, mWidth / 2, mHeight / 2 + radius - textHeight / 2 + textBaseLineOffset, paintText);
/**
*
*/
paintText.setTextSize(mHeight * 1 / 8);
fontMetrics = paintText.getFontMetrics();
textBaseLineOffset = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
canvas.drawText("%", mWidth / 2, mHeight / 2 + radius / 3 + textBaseLineOffset, paintText);
/**
*
*/
paintText.setTextSize(mHeight * 3 / 8);
canvas.drawText("" + (int)(percent*mDecelerateInterpolator.getInterpolation(1.0f * curTime / duration)), mWidth / 2, mHeight / 2, paintText);
/**
* 20
*/
mHandler.postDelayed(mAnimation, 20);
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.