Android drawXXX()直方图和饼图

题目见Github
通过一下午的时间做了个练习,比较复杂的就是直方图和饼图。

  • 直方图


    device-2017-07-13-183502.png
package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Arrays;
import java.util.List;

public class Practice10HistogramView extends View {
    private Paint mPaint;
    private List<String> mStrings = Arrays.asList("Froyo", "GB", "ICS", "JB", "KitKat", "L", "M");
    private List<Integer> mIntegerList = Arrays.asList(2, 5, 6, 40, 80, 100, 30);
    private RectF rectF = new RectF();
    private Rect descRectF = new Rect();

    public Practice10HistogramView(Context context) {
        super(context);
        init();
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth() - 100 - 100;
        int marign = 20;
        int eachWidth = (width - marign * (mStrings.size() + 1)) / mStrings.size();
        canvas.drawLine(100, 30, 100, getHeight() - 160, mPaint);
        canvas.drawLine(100, getHeight() - 160, getWidth() - 100, getHeight() - 160, mPaint);
        mPaint.setTextSize(28);
        for (int i = 0; i < mStrings.size(); i++) {
            int x = eachWidth * i + marign * (i + 1) + 100;
            Rect rect = new Rect();
            mPaint.getTextBounds(mStrings.get(i), 0, mStrings.get(i).length(), rect);
            canvas.drawText(mStrings.get(i), x + eachWidth / 2 - rect.width() / 2, getHeight() - 120, mPaint);
        }
        mPaint.setTextSize(48);
        String desc = "直方图";
        mPaint.getTextBounds(desc, 0, desc.length(), descRectF);
        canvas.drawText(desc, getWidth() / 2 - descRectF.width() / 2, getHeight() - 20, mPaint);

        mPaint.setColor(Color.parseColor("#72B916"));
        mPaint.setStyle(Paint.Style.FILL);
        int height = getHeight() - 230;
        int eachHeight = height / 100;
        for (int i = 0; i < mIntegerList.size(); i++) {
            rectF.left = eachWidth * i + marign * (i + 1) + 100;
            rectF.top = height - eachHeight * mIntegerList.get(i);
            rectF.right = rectF.left + eachWidth;
            rectF.bottom = getHeight() - 160;
            canvas.drawRect(rectF, mPaint);
        }
    }
}
  • 饼图
device-2017-07-13-183511.png
package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Arrays;
import java.util.List;

public class Practice11PieChartView extends View {
    private Paint mPaint;
    private List<Integer> mIntegerList = Arrays.asList(40, 20, 7, 3, 20, 30);
    private List<String> mStrings = Arrays.asList("AAFSF", "BFDBC", "CDF", "DFE", "EDSFSDFD", "FFDCX");
    private int sum = 0;
    private int radius = 300;
    private Paint mWhitePaint;
    private RectF mRectF1 = new RectF();
    private RectF mRectF2 = new RectF();
    private RectF mRectF3 = new RectF();
    private RectF mRectF4 = new RectF();

    public Practice11PieChartView(Context context) {
        super(context);
        init();
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(2);
        for (int a : mIntegerList) {
            sum += a;
        }
        mWhitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mWhitePaint.setColor(Color.WHITE);
        mWhitePaint.setStrokeWidth(3);
        mWhitePaint.setTextSize(30);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mRectF1.left = getWidth() / 2 - radius + 5;
        mRectF1.top = getHeight() / 2 - radius + 5;
        mRectF1.right = getWidth() / 2 + radius + 5;
        mRectF1.bottom = getHeight() / 2 + radius + 5;

        mRectF2.left = getWidth() / 2 - radius - 5;
        mRectF2.top = getHeight() / 2 - radius + 5;
        mRectF2.right = getWidth() / 2 + radius - 5;
        mRectF2.bottom = getHeight() / 2 + radius + 5;

        mRectF3.left = getWidth() / 2 - radius - 5;
        mRectF3.top = getHeight() / 2 - radius - 5;
        mRectF3.right = getWidth() / 2 + radius - 5;
        mRectF3.bottom = getHeight() / 2 + radius - 5;

        mRectF4.left = getWidth() / 2 - radius + 5;
        mRectF4.top = getHeight() / 2 - radius - 5;
        mRectF4.right = getWidth() / 2 + radius + 5;
        mRectF4.bottom = getHeight() / 2 + radius - 5;
    }

    private int degreeed = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        综合练习
//        练习内容:使用各种 Canvas.drawXXX() 方法画饼图
        for (int i = 0; i < mIntegerList.size(); i++) {
            int degree = mIntegerList.get(i) * 360 / sum;
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            mPaint.setColor(Color.rgb(red, green, blue));
            if (degreeed < 180) {
                int centerDegree = degreeed + degree / 2;
                if (degreeed < 90) {
                    float startX = (float) (Math.cos(Math.PI * centerDegree / 180.0) * radius) + getWidth() / 2;
                    float startY = (float) (Math.sin(Math.PI * centerDegree / 180.0) * radius) + getHeight() / 2;
                    float endX = startX + 25;
                    float endY = startY + 25;
                    canvas.drawLine(startX, startY, endX, endY, mWhitePaint);
                    canvas.drawLine(endX, endY, endX + 70, endY, mWhitePaint);
                    canvas.drawText(mStrings.get(i), endX + 80, endY, mWhitePaint);
                    canvas.drawArc(mRectF1, degreeed, degree, true, mPaint);
                } else {
                    float startX = getWidth() / 2 + (float) (Math.cos(Math.PI * centerDegree / 180.0) * radius);
                    float startY = (float) (Math.sin(Math.PI * centerDegree / 180.0) * radius) + getHeight() / 2;
                    float endX = startX - 25;
                    float endY = startY + 25;
                    canvas.drawLine(startX, startY, endX, endY, mWhitePaint);
                    canvas.drawLine(endX, endY, endX - 70, endY, mWhitePaint);
                    Rect rect = new Rect();
                    mWhitePaint.getTextBounds(mStrings.get(i), 0, mStrings.get(i).length(), rect);
                    canvas.drawText(mStrings.get(i), endX - 80 - rect.width(), endY, mWhitePaint);
                    canvas.drawArc(mRectF2, degreeed, degree, true, mPaint);
                }
            } else {
                int centerDegree = degreeed + degree / 2;
                if (degreeed < 270) {
                    float startX = getWidth() / 2 + (float) (Math.cos(Math.PI * centerDegree / 180.0) * radius);
                    float startY = getHeight() / 2 + (float) (Math.sin(Math.PI * centerDegree / 180.0) * radius);
                    float endX = startX - 25;
                    float endY = startY - 25;
                    canvas.drawLine(startX, startY, endX, endY, mWhitePaint);
                    canvas.drawLine(endX, endY, endX - 70, endY, mWhitePaint);
                    Rect rect = new Rect();
                    mWhitePaint.getTextBounds(mStrings.get(i), 0, mStrings.get(i).length(), rect);
                    canvas.drawText(mStrings.get(i), endX - 80 - rect.width(), endY, mWhitePaint);
                    canvas.drawArc(mRectF3, degreeed - 360, degree, true, mPaint);
                } else {
                    float startX = getWidth() / 2 + (float) (Math.cos(Math.PI * centerDegree / 180.0) * radius);
                    float startY = getHeight() / 2 + (float) (Math.sin(Math.PI * centerDegree / 180.0) * radius);
                    float endX = startX + 25;
                    float endY = startY - 25;
                    canvas.drawLine(startX, startY, endX, endY, mWhitePaint);
                    canvas.drawLine(endX, endY, endX + 70, endY, mWhitePaint);
                    canvas.drawText(mStrings.get(i), endX + 80, endY, mWhitePaint);
                    canvas.drawArc(mRectF4, degreeed - 360, degree, true, mPaint);
                }
            }
            degreeed += degree;
        }
        mPaint.setColor(Color.BLACK);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容