• android canvas 二


    引用:http://hi.baidu.com/xdsh99/blog/item/1ae160ec0733dae5b2fb9591.html

    注:1.数据都是从前一个页面传递过来的;

           2.不同区域设置不同的颜色,通过设置画笔的颜色即可;

           3.左上角定点坐标为(0,0);

           4.包含画点,画线,画矩形,画椭圆,画圆形,写字

    package com.xdsh.weeklyaccount;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.RectF;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import com.xdsh.entity.*;

    public class PaintGraphy extends Activity
    {
     CountEntity entity = new CountEntity();

     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      DrawView myView = new DrawView(PaintGraphy.this);
      setContentView(myView);
     }

     public class DrawView extends View
     {
      private int value, location;
      // 用于统计
      private double flagIn, flagOut, flagLeft;
      private float clotheRate, studyRate, trafficRate, otherRate;

      public DrawView(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.setOutClothe(bundle.getDouble("first"));
       entity.setOutStudy(bundle.getDouble("second"));
       entity.setOutTraffic(bundle.getDouble("third"));
       entity.setOutOther(bundle.getDouble("fourth"));
       entity.setIncome(bundle.getDouble("inTotal"));
       entity.setOutcome(bundle.getDouble("outTotal"));
       entity.setLeft(bundle.getDouble("leftTotal"));
       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);
       }
       
      }
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
      // TODO Auto-generated method stub
      menu.add(0, 0, 1, "Back").setIcon(android.R.drawable.ic_menu_revert);
      return super.onCreateOptionsMenu(menu);
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item)
     {
      // TODO Auto-generated method stub
      if (item.getItemId() == 0)
      {
       Intent intent = new Intent();
       intent.setClass(PaintGraphy.this, CountAccount.class);
       startActivity(intent);
      }
      return super.onOptionsItemSelected(item);
     }
    }


  • 相关阅读:
    第二十四节:ESModule简介、按需导出导入、默认导出导入、动态加载、内部原理等
    第二十三节:JS模块化历史、CommonJs详解、AMD和CMD规范知悉
    第二十六节:cnpm的使用和yarn的用法详解
    第二十五节:npm介绍、package.json详解、npm install原理、常用指令、npx工具、发布自己的包
    CSS系列总结【更新中】
    第二十二节:进程/线程、node事件机制、微任务/宏任务、相关面试题剖析
    ES6中新增了Array.isArray
    C# WebException 获取http错误码和错误消息
    DataSet序列化成JSON格式字符串
    linux shell 中小数进行比较
  • 原文地址:https://www.cnblogs.com/sode/p/2350112.html
Copyright © 2020-2023  润新知