zoukankan      html  css  js  c++  java
  • 代码积累1统计图

    效果如图:
    代码积累1----统计图 - Alaric - 记录在编程世界里成长的历程
     
    1、类MyView 
     
    package com.alaric.test;
     
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.RectF;
    import android.view.View;
     
     
     
    public class MyView extends View {
     private int value, location;
     // 用于统计
     private double flagIn, flagOut, flagLeft;
     private float clotheRate, studyRate, trafficRate, otherRate;
     
     public MyView(Context context)
     {
      super(context);
      // TODO Auto-generated constructor stub
     }
     
     @Override
     protected void onDraw(Canvas canvas)
     {
      // TODO Auto-generated method stub
      super.onDraw(canvas);
      // 设置背景色为浅灰色
      canvas.drawColor(Color.LTGRAY);
      // 定义一个画笔
      Paint paint = new Paint();
      // 抗锯齿
      paint.setAntiAlias(true);
      /*
       * 绘制小计柱状图
       */
      // 绘制 横坐标
      paint.setColor(Color.BLACK);
      canvas.drawLine(40, 200, 300, 200, paint);
      // for (int i = 70; i <= 250; i += 30)
      // {
      // paint.setColor(Color.WHITE);
      // // 绘制横坐标上的点
      // canvas.drawPoint(i, 200, paint);
      // }
      // 绘制纵坐标
      paint.setColor(Color.BLACK);
      canvas.drawLine(40, 30, 40, 200, paint);
      // for (int i = 170; i > 40; i -= 30)
      // {
      // // 绘制纵坐标上的点
      // paint.setColor(Color.WHITE);
      // canvas.drawPoint(40, i, paint);
      // }
      // 绘制水平线
      paint.setColor(Color.BLACK);
      for (int i = 170; i > 20; i -= 30)
      {
       canvas.drawLine(40, i, 280, i, paint);
      }
      // 设置标题字体大小
      paint.setTextSize(18);
      paint.setColor(Color.RED);
      canvas.drawText(getResources().getString(R.string.graph_page_count), 65, 26, paint);
      canvas.drawText(getResources().getString(R.string.graph_page_consume), 100, 250, paint);
      // 设置各项目字体为黑色
      paint.setColor(Color.BLACK);
      paint.setTextSize(15);
      // 收入,支出,余额
      canvas.drawText(getResources().getString(R.string.second_page_countIn), 60, 220, paint);
      canvas.drawText(getResources().getString(R.string.second_page_countOut), 130, 220, paint);
      canvas.drawText(getResources().getString(R.string.second_page_countLeft), 200, 220, paint);
      canvas.drawText(getResources().getString(R.string.graph_page_top), 5, 40, paint);
      canvas.drawText(getResources().getString(R.string.graph_page_right), 265, 218, paint);
      // 纵坐标对应的值
      for (value = 300, location = 180; value <= 1500; value += 300, location -= 30)
      {
       canvas.drawText("" + value, 5, location, paint);
      }
      canvas.drawText("0", 9, 210, paint);
      // 向右的箭头
      Path path1 = new Path();
      path1.moveTo(300, 200);
      path1.lineTo(295, 195);
      path1.lineTo(305, 200);
      path1.lineTo(295, 205);
      canvas.drawPath(path1, paint);
      // 向上的箭头
      Path path = new Path();
      path.moveTo(40, 30);
      path.lineTo(35, 35);
      path.lineTo(40, 25);
      path.lineTo(45, 35);
      canvas.drawPath(path, paint);
      // 接收数据
      //Intent intent = getIntent();
      // Bundle bundle = intent.getExtras();
      Entity entity =new Entity();
      entity.setOutClothe(500.00);
      entity.setOutStudy(100.0);
      entity.setOutTraffic(100.0);
      entity.setOutOther(300.0);
      entity.setIncome(1500.0);
      entity.setOutcome(1000);
      entity.setLeft(500);
      flagIn = entity.getIncome();
      flagOut = entity.getOutcome();
      flagLeft = entity.getLeft();
      clotheRate = (float) (entity.getOutClothe() / entity.getOutcome());
      studyRate = (float) (entity.getOutStudy() / entity.getOutcome());
      trafficRate = (float) (entity.getOutTraffic() / entity.getOutcome());
      otherRate = (float) (entity.getOutOther() / entity.getOutcome());
      /*
       * 绘制小计矩形 canvas.drawRect(left, top, right, bottom, paint) (float)
       * clothe * 30 / 300得到高度 200 - (float) clothe * 30 / 300 得到坐标
       */
      paint.setColor(Color.RED);
      /*
       * 对收入进行判断 ,大于1500,设为1500,并显示金额 ;大于等于0小于等于1500,显示为柱状图;收入小于等于0,设为0
       */
      if (entity.getIncome() > 1500)
      {
       entity.setIncome(1500);
       canvas.drawText(flagIn + "", 100, 45, paint);
      } else if (entity.getIncome() > 0 && entity.getIncome() <= 1500)
      {
       canvas.drawText(flagIn + "", 100, 195 - (float) (entity.getIncome() * 30 / 300), paint);
      } else
      {
       entity.setIncome(0);
       canvas.drawText("0", 100, 195, paint);
      }
      canvas.drawRect(85, 200 - (float) (entity.getIncome() * 30 / 300), 115, 200, paint);
      /*
       * 对支出进行判断 ,大于1500,设为1500,并显示金额; 大于等于0小于等于1500,显示为柱状图;支出小于等于0,设为0
       */
      paint.setColor(Color.BLUE);
      if (entity.getOutcome() > 1500)
      {
       entity.setOutcome(1500);
       canvas.drawText(flagOut + "", 160, 45, paint);
      } else if (entity.getOutcome() >0 && entity.getOutcome() <= 1500)
      {
       canvas.drawText(flagOut + "", 160, 195 - (float) (entity.getOutcome() * 30 / 300), paint);
      } else
      {
       entity.setOutcome(0);
       canvas.drawText("0", 160, 195, paint);
      }
      canvas.drawRect(145, 200 - (float) (entity.getOutcome() * 30 / 300), 175, 200, paint);
     
      paint.setColor(Color.GREEN);
      /*
       * 对余额进行判断 ,大于1500,设为1500,并显示金额; 大于等于0小于等于1500,显示为柱状图;
       * 小于等于0;设置为0,显示超支金额
       */
      if (entity.getLeft() > 1500)
      {
       entity.setLeft(1500);
       canvas.drawText(flagLeft + "", 220, 45, paint);
      } else if (entity.getLeft() >= 0 && entity.getLeft() <= 1500)
      {
       canvas.drawText(flagLeft + "", 220, 195 - (float) (entity.getLeft() * 30 / 300), paint);
      } else
      {
       entity.setLeft(0);
       canvas.drawText("-" + flagLeft, 220, 195, paint);
      }
      canvas.drawRect(205, 200 - (float) entity.getLeft() * 30 / 300, 235, 200, paint);
     
      /*
       * 消费项明细
       * 
       * void drawArc (RectF oval, float startAngle, float sweepAngle,
       * boolean useCenter, Paint paint) 第1个参数oval表示这个弧形的边界。
       * 第2个参数startAngle是指开始的角度。 第3个参数sweepAngle是指圆弧的角度。
       * 第4个参数useCenter是指卖描画的图形包不包括圆心。 第5个参数是Paint的实例。
       */
      RectF rectF = new RectF(10, 265, 160, 415);
     
      // canvas.drawCircle(85, 340, 75, paint);
      
      // 衣服饰品
      paint.setColor(Color.rgb(200, 255, 180));
      canvas.drawArc(rectF, 0, clotheRate * 360, true, paint);
      // 矩形图例
      canvas.drawRect(175, 270, 195, 290, paint);
      // 文字图例
      canvas.drawText(getResources().getString(R.string.second_page_moneyoutOne), 203, 285, paint);
      // 百分比
      canvas.drawText(Math.round(clotheRate * 100) + "%", 273, 285, paint);
      // 学习
      paint.setColor(Color.rgb(180, 60, 250));
      canvas.drawArc(rectF, clotheRate * 360, studyRate * 360, true, paint);
      canvas.drawRect(175, 310, 195, 330, paint);
      canvas.drawText(getResources().getString(R.string.second_page_moneyoutTwo), 203, 325, paint);
      canvas.drawText(Math.round(studyRate * 100) + "%", 273, 325, paint);
      // 交通
      paint.setColor(Color.rgb(240, 180, 40));
      canvas.drawArc(rectF, (clotheRate + studyRate) * 360, trafficRate * 360, true, paint);
      canvas.drawRect(175, 350, 195, 370, paint);
      canvas.drawText(getResources().getString(R.string.second_page_moneyoutThree), 203, 365, paint);
      canvas.drawText(Math.round(trafficRate * 100) + "%", 273, 365, paint);
      // 其它
      paint.setColor(Color.rgb(240, 100, 220));
      canvas.drawArc(rectF, (clotheRate + studyRate + trafficRate) * 360, otherRate * 360, true, paint);
      canvas.drawRect(175, 390, 195, 410, paint);
      canvas.drawText(getResources().getString(R.string.second_page_moneyoutFour), 203, 405, paint);
      if((entity.getOutClothe()==0.0) && (entity.getOutStudy()==0.0) && (entity.getOutTraffic()==0.0) && (entity.getOutOther()==0.0)){
       canvas.drawText("0" + "%", 273, 405, paint);
      }else{
       canvas.drawText(100 - (Math.round(trafficRate * 100) + Math.round(studyRate * 100) + Math.round(clotheRate * 100)) + "%", 273, 405, paint);
      }
      
     }
     
     
     
    }
     
     
    2、类AndroidTest6Activity 
    package com.alaric.test;
     
    import android.app.Activity;
    import android.os.Bundle;
     
    public class AndroidTest6Activity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView(this));
        }
    }
     
     
    3、类Entity 
    package com.alaric.test;
     
    public class Entity {
    private double outClothe;
    private double outStudy;
    private double outTraffic;
    private double outOther;
    private double Income;
    private double outcome;
    private double left;
    public double getOutClothe() {
    return outClothe;
    }
    public void setOutClothe(double outClothe) {
    this.outClothe = outClothe;
    }
    public double getOutStudy() {
    return outStudy;
    }
    public void setOutStudy(double outStudy) {
    this.outStudy = outStudy;
    }
    public double getOutTraffic() {
    return outTraffic;
    }
    public void setOutTraffic(double outTraffic) {
    this.outTraffic = outTraffic;
    }
    public double getOutOther() {
    return outOther;
    }
    public void setOutOther(double outOther) {
    this.outOther = outOther;
    }
    public double getIncome() {
    return Income;
    }
    public void setIncome(double income) {
    Income = income;
    }
    public double getOutcome() {
    return outcome;
    }
    public void setOutcome(double outcome) {
    this.outcome = outcome;
    }
    public double getLeft() {
    return left;
    }
    public void setLeft(double left) {
    this.left = left;
    }
     
    }
     
  • 相关阅读:
    iPhone网络编程之--Reachability
    ASIHTTPRequest 详解, http 请求终结者2
    什么情况下使用break关键字? 什么情况下使用Continue关键字? Java如何声明一个数组?JS如何声明一个数组?如何获取数组长度? 如何遍历数组?
    说说三元运算和if...else的相同之处? Switch语句的条件只能接受什么类型的值? 说说do...while和while的区别? 说说for循环的两种写法?
    String类的常用方法
    逻辑结算的结果是什么类型? 比较运算的值是什么类型? 声明字符串有哪几种方式?怎么写? Math类有哪些常用的方法? 三元运算怎么写?
    算术运算有哪些?逻辑运算有哪些?比较运算有哪些?
    Java中8种基本数据类型是哪些?
    Java如何声明变量?JS如何声明变量?
    回顾之前知识: 注释
  • 原文地址:https://www.cnblogs.com/alaricblog/p/3264641.html
Copyright © 2011-2022 走看看