Android 사용자 정의 컨트롤 가로 기둥 모양 통계 도

5106 단어 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;
 }
 
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기