android 自定义饼图

论坛 期权论坛     
选择匿名的用户   2021-6-2 15:38   64   0
<p>     本代码是一年前公司项目需要自己所写,当时做android项目需要统计的功能,统计的图表包括饼图、柱状图、折线图,代码写的有些笨拙!希望大家能够看懂!其代码做抛砖引玉,希望大家能够再改写下!可以用多线程做出动画,是不是更好呢?这些后期我也没有整理过,所以谁能再整理下!</p>
<p>CookieData.java</p>
<p><span style="color:#ff0000">class CookieData</span></p>
<pre class="blockcode"><code class="language-java">package com.wforyou.dhh.ui.widget;

public class CookieData {

/**名称**/
private String name;

/**数值**/
private double data;

/**颜色**/
private int color;

public String getName() {
  return name;
}

public void setName(String name) {
  this.name &#61; name;
}

public double getData() {
  return data;
}

public void setData(double data) {
  this.data &#61; data;
}

public int getColor() {
  return color;
}

public void setColor(int color) {
  this.color &#61; color;
}

&#64;Override
public String toString() {
  return &#34;CookieData [name&#61;&#34; &#43; name &#43; &#34;, data&#61;&#34; &#43; data &#43; &#34;, color&#61;&#34;
    &#43; color &#43; &#34;]&#34;;
}
}
</code></pre>
<p><span style="color:#ff0000">Cookie.java</span></p>
<pre class="blockcode"><code class="language-java">package com.wforyou.dhh.ui.widget;

import com.wforyou.dhh.util.Logs;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.TextView;

public class Cookie extends View {

private int mAscent;

private String mText &#61; &#34;&#34;;
// 屏幕的高度
public static int heightPixels &#61; 0;

// 屏幕的宽度
public static int widthPixels &#61; 0;

/** 画笔 **/
private static Paint paint;

/** 半径 **/
private float radius &#61; 130;

/** X起点坐标 **/
private static float xstartcoordinate &#61; 100;

/** Y起点坐标 **/
private static float ystartcoordinate &#61; 180;

/** X终点坐标 **/
private static float xendcoordinate &#61; 400;

/** Y终点坐标 **/
private static float yendcoordinate &#61; 180;

private static RectF f;

private static int[] colors &#61; { Color.parseColor(&#34;#29A3A4&#34;),
   Color.parseColor(&#34;#4239A2&#34;), Color.parseColor(&#34;#9C46FF&#34;),
   Color.parseColor(&#34;#E8C000&#34;), Color.parseColor(&#34;#4B0064&#34;),
   Color.parseColor(&#34;#16699B&#34;), Color.parseColor(&#34;#3E635B&#34;),
   Color.parseColor(&#34;#B19862&#34;), Color.parseColor(&#34;#F736F7&#34;),
   Color.parseColor(&#34;#164988&#34;) };

private String[] textValues &#61; { &#34;裤子&#34;, &#34;裙子&#34;, &#34;内裤&#34;, &#34;文胸&#34;, &#34;保暖衣&#34; };

private double[] numberValues &#61; { 63, 80, 0, 150, 140 };

private String noteText &#61; &#34;&#34;;

private static float statar &#61; 0;

private static float addx &#61; 0;

private static float addy &#61; 0;

// 距离屏幕左边的距离
private static float marginleft &#61; 0;

// 距离屏幕上边的距离
private static float margintop &#61; 40;

// 距离屏幕右边的距离
private static float marginrigth &#61; 0;

// 距离屏幕下边的距离
private static float margindown &#61; 50;

/** 圆下方提示 **/
private String downNoteText &#61; &#34;大类  金额&#34;;

public Cookie(Context context) {
  super(context);
}

public Cookie(Context context, AttributeSet attrs) {
  super(context, attrs);
}

public Cookie(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
}

public String[] getTextValues() {
  return textValues;
}

public void setTextValues(String[] textValues) {
  this.textValues &#61; textValues;
}

public double[] getNumberValues() {
  return numberValues;
}

public void setNumberValues(double[] numberValues) {
  this.numberValues &#61; numberValues;
}

public String getNoteText() {
  return noteText;
}

public void setNoteText(String noteText) {
  this.noteText &#61; noteText;
}

public String getDownNoteText() {
  return downNoteText;
}

public void setDownNoteText(String downNoteText) {
  this.downNoteText &#61; downNoteText;
}

&#64;Override
protected void onDraw(Canvas canvas) {
  canvas.drawColor(Color.WHITE);

  getScreenData();
  paint &#61; new Paint();
  canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
    paint);
  // 抗锯齿
  paint.setAntiAlias(true);

  // 实心扇形
  paint.setStyle(Style.FILL);

  // 设置矩形
  getRectF();
  // 得到总数据
  double dsum &#61; getSum();

  getaddy();
  int intlength &#61; numberValues.length;
  float temp &#61; 0;
  float dtemp &#61; 0;
  for (int i &#61; 0; i &lt; intlength; i&#43;&#43;) {
   if (numberValues[i] &gt; 0) {
    doDrawTextNote(colors[i], textValues[i], 50, canvas);
    dodrawdegree(canvas, colors[i], temp,
      (float) (numberValues[i] / dsum) * 360);// 画扇形
    dtemp &#61; temp &#43; (float) (numberValues[i] / dsum) * 360 / 2;
    temp &#43;&#61; ((float) (numberValues[i] / dsum) *
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP