Android 사용자 정의 컨트롤 가로 기둥 모양 통계 도
하나의 항목 을 만 나 려 면 통계 도 기능 을 사용 해 야 하 며,비교적 간단 하면 사용자 정의 로 하 나 를 썼 다.스타일 과 기능 을 많이 쓰 지 않 고 간단 한 가로 기둥 모양 통계 도 만 있 습 니 다.
데 이 터 를 입력 한 후 대략 다음 과 같 습 니 다.
/**
* Created by Administrator on 2018/1/16 0016.
*/
public class HorizontalChartView extends View {
/**
*
*/
private Paint paint;
/**
*
*/
private int color_line = Color.rgb(230, 230, 230);
/**
*
*/
private int color_font = Color.rgb(51, 51, 51);
/**
*
*/
private int color_plan = Color.rgb(22, 85, 164);
/**
*
*/
private Paint paint_plan;
/**
*
*/
private int plan_height;
/**
*
*/
private Float[] ratio = {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};
/**
* 1
*/
private Paint paint_font;
/**
* 2
*/
private Paint paint_font2;
/**
*
*/
private int line_num = 11;
/**
*
*/
private String ratio_num = "0";
/**
*
*/
private String month_num = "1 ";
public HorizontalChartView(Context context) {
super(context);
}
public HorizontalChartView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public HorizontalChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public HorizontalChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
/**
*
*
* @param context
* @param attrs
*/
public void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setColor(color_line);
paint_plan = new Paint();
paint_plan.setColor(color_plan);
paint_font = new Paint();
paint_font.setColor(color_font);
paint_font.setTextSize(DensityUtils.dp2px(context, 12));
paint_font.setAntiAlias(true);
paint_font.setTextAlign(Paint.Align.CENTER);
paint_font2 = new Paint();
paint_font2.setColor(color_font);
paint_font2.setTextSize(DensityUtils.dp2px(context, 12));
paint_font2.setAntiAlias(true);
paint_font2.setTextAlign(Paint.Align.RIGHT);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
int lift_width = (int) (width * 0.15);
int line_width = (int) (width * 0.78);
//
Paint.FontMetrics fm = paint_font.getFontMetrics();
int line_length = (int) (height - (fm.bottom - fm.top) - 4);
plan_height = (int) (line_length / 12 * 0.3);
for (int i = 0; i < line_num; i++) {
canvas.save();
if (i == 0) {
ratio_num = "0";
} else {
ratio_num = i + "0";
}
//
canvas.drawText(ratio_num, lift_width + i * line_width / 10, height - 10, paint_font);
//
canvas.drawLine(lift_width + i * line_width / 10, 0, lift_width + i * line_width / 10, line_length, paint);
canvas.restore();
}
//
Paint.FontMetrics fm1 = paint_font2.getFontMetrics();
for (int n = 12; n > 0; n--) {
canvas.save();
month_num = n + " ";
//
canvas.drawText(month_num, lift_width / 4 * 3, ((line_length / 12)) * (13 - n) - line_length / 24 + (fm1.bottom - fm1.top) / 2, paint_font2);
//
canvas.drawRect(lift_width, line_length / 12 * (13 - n) - (line_length / 24 + plan_height / 2) + fm1.bottom,
line_width * (ratio[n - 1] / 100) + lift_width, line_length / 12 * (13 - n) - (line_length / 24 + plan_height / 2) + plan_height + fm1.bottom, paint_plan);
canvas.restore();
}
}
/**
*
*
* @param ratio
*/
public void setRatio(Float[] ratio) {
this.ratio = ratio;
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.