91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發中怎么使用canvas繪制一個統計圖

發布時間:2020-11-21 15:24:21 來源:億速云 閱讀:159 作者:Leah 欄目:移動開發

Android開發中怎么使用canvas繪制一個統計圖?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

效果如下:

Android開發中怎么使用canvas繪制一個統計圖 Android開發中怎么使用canvas繪制一個統計圖 Android開發中怎么使用canvas繪制一個統計圖

特點:

1.使用非常方便,可放在xml布局文件中,然后在代碼中設置內容,即:

PieChartView pieChartView = (PieChartView) findViewById(R.id.pie_chart);
PieChartView.PieItemBean[] items = new PieChartView.PieItemBean[]{
    new PieChartView.PieItemBean("娛樂", 200),
    new PieChartView.PieItemBean("旅行", 100),
    new PieChartView.PieItemBean("學習", 120),
    new PieChartView.PieItemBean("人際關系", 160),
    new PieChartView.PieItemBean("交通", 100),
    new PieChartView.PieItemBean("餐飲", 480)
};
pieChartView.setPieItems(items);

2.條目數量,大小及折線位置,長度均自適應。左側條目往左側劃線,右側條目往右側劃線,文字描述與百分比居中對齊,并且文字“下劃線”與文字長度自適應。對于很小的條目,將自動將折線延長以盡可能避免文字遮蓋

核心代碼:PieChartView.Java:

public class PieChartView extends View {
  private int screenW, screenH;
  /**
   * The paint to draw text, pie and line.
   */
  private Paint textPaint, piePaint, linePaint;
  /**
   * The center and the radius of the pie.
   */
  private int pieCenterX, pieCenterY, pieRadius;
  /**
   * The oval to draw the oval in.
   */
  private RectF pieOval;
  private float smallMargin;
  private int[] mPieColors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.MAGENTA, Color.CYAN};
  private PieItemBean[] mPieItems;
  private float totalValue;
  public PieChartView(Context context) {
    super(context);
    init(context);
  }
  public PieChartView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }
  public PieChartView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }
  private void init(Context context) {
    //init screen
    screenW = ScreenUtils.getScreenW(context);
    screenH = ScreenUtils.getScreenH(context);
    pieCenterX = screenW / 2;
    pieCenterY = screenH / 3;
    pieRadius = screenW / 4;
    smallMargin = ScreenUtils.dp2px(context, 5);
    pieOval = new RectF();
    pieOval.left = pieCenterX - pieRadius;
    pieOval.top = pieCenterY - pieRadius;
    pieOval.right = pieCenterX + pieRadius;
    pieOval.bottom = pieCenterY + pieRadius;
    //The paint to draw text.
    textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(ScreenUtils.dp2px(context, 16));
    //The paint to draw circle.
    piePaint = new Paint();
    piePaint.setAntiAlias(true);
    piePaint.setStyle(Paint.Style.FILL);
    //The paint to draw line to show the concrete text
    linePaint = new Paint();
    linePaint.setAntiAlias(true);
    linePaint.setStrokeWidth(ScreenUtils.dp2px(context, 1));
  }
  //The degree position of the last item arc's center.
  private float lastDegree = 0;
  //The count of the continues 'small' item.
  private int addTimes = 0;
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mPieItems != null && mPieItems.length > 0) {
      float start = 0.0f;
      for (int i = 0; i < mPieItems.length; i++) {
        //draw pie
        piePaint.setColor(mPieColors[i % mPieColors.length]);
        float sweep = mPieItems[i].getItemValue() / totalValue * 360;
        canvas.drawArc(pieOval, start, sweep, true, piePaint);
        //draw line away from the pie
        float radians = (float) ((start + sweep / 2) / 180 * Math.PI);
        float lineStartX = pieCenterX + pieRadius * 0.7f * (float) (Math.cos(radians));
        float lineStartY = pieCenterY + pieRadius * 0.7f * (float) (Math.sin(radians));
        float lineStopX, lineStopY;
        float rate;
        if (getOffset(start + sweep / 2) > 60) {
          rate = 1.3f;
        } else if (getOffset(start + sweep / 2) > 30) {
          rate = 1.2f;
        } else {
          rate = 1.1f;
        }
        //If the item is very small, make the text further away from the pie to avoid being hided by other text.
        if (start + sweep / 2 - lastDegree < 30) {
          addTimes++;
          rate += 0.2f * addTimes;
        } else {
          addTimes = 0;
        }
        lineStopX = pieCenterX + pieRadius * rate * (float) (Math.cos(radians));
        lineStopY = pieCenterY + pieRadius * rate * (float) (Math.sin(radians));
        canvas.drawLine(lineStartX, lineStartY, lineStopX, lineStopY, linePaint);
        //write text
        String itemTypeText = mPieItems[i].getItemType();
        String itemPercentText = Utility.formatFloat(mPieItems[i].getItemValue() / totalValue * 100) + "%";
        float itemTypeTextLen = textPaint.measureText(itemTypeText);
        float itemPercentTextLen = textPaint.measureText(itemPercentText);
        float lineTextWidth = Math.max(itemTypeTextLen, itemPercentTextLen);
        float textStartX = lineStopX;
        float textStartY = lineStopY - smallMargin;
        float percentStartX = lineStopX;
        float percentStartY = lineStopY + textPaint.getTextSize();
        if (lineStartX > pieCenterX) {
          textStartX += (smallMargin + Math.abs(itemTypeTextLen - lineTextWidth) / 2);
          percentStartX += (smallMargin + Math.abs(itemPercentTextLen - lineTextWidth) / 2);
        } else {
          textStartX -= (smallMargin + lineTextWidth - Math.abs(itemTypeTextLen - lineTextWidth) / 2);
          percentStartX -= (smallMargin + lineTextWidth - Math.abs(itemPercentTextLen - lineTextWidth) / 2);
        }
        canvas.drawText(itemTypeText, textStartX, textStartY, textPaint);
        //draw percent text
        canvas.drawText(itemPercentText, percentStartX, percentStartY, textPaint);
        //draw text underline
        float textLineStopX = lineStopX;
        if (lineStartX > pieCenterX) {
          textLineStopX += (lineTextWidth + smallMargin * 2);
        } else {
          textLineStopX -= (lineTextWidth + smallMargin * 2);
        }
        canvas.drawLine(lineStopX, lineStopY, textLineStopX, lineStopY, linePaint);
        lastDegree = start + sweep / 2;
        start += sweep;
      }
    }
  }
  public PieItemBean[] getPieItems() {
    return mPieItems;
  }
  public void setPieItems(PieItemBean[] pieItems) {
    this.mPieItems = pieItems;
    totalValue = 0;
    for (PieItemBean item : mPieItems) {
      totalValue += item.getItemValue();
    }
    invalidate();
  }
  private float getOffset(float radius) {
    int a = (int) (radius % 360 / 90);
    switch (a) {
      case 0:
        return radius;
      case 1:
        return 180 - radius;
      case 2:
        return radius - 180;
      case 3:
        return 360 - radius;
    }
    return radius;
  }
  static class PieItemBean {
    private String itemType;
    private float itemValue;
    PieItemBean(String itemType, float itemValue) {
      this.itemType = itemType;
      this.itemValue = itemValue;
    }
    public String getItemType() {
      return itemType;
    }
    public void setItemType(String itemType) {
      this.itemType = itemType;
    }
    public float getItemValue() {
      return itemValue;
    }
    public void setItemValue(float itemValue) {
      this.itemValue = itemValue;
    }
  }
}

關于Android開發中怎么使用canvas繪制一個統計圖問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

衡水市| 宁安市| 江口县| 横峰县| 十堰市| 兴义市| 承德市| 修文县| 孝感市| 孙吴县| 中山市| 泰兴市| 盐亭县| 阜南县| 内江市| 个旧市| 西乡县| 饶阳县| 永吉县| 灵璧县| 龙口市| 当雄县| 桂阳县| 米泉市| 玉树县| 资阳市| 芦溪县| 迁西县| 曲阜市| 昌乐县| 宁津县| 达拉特旗| 漳浦县| 连城县| 玉龙| 南昌县| 双桥区| 沿河| 庐江县| 阿荣旗| 永定县|