最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android動(dòng)態(tài)繪制餅狀圖的示例代碼

 更新時(shí)間:2018年03月19日 14:11:19   作者:_xiangpan  
這篇文章主要介紹了Android動(dòng)態(tài)繪制餅狀圖的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

項(xiàng)目里面的需求,當(dāng)時(shí)搜索到MPAndroidChart庫,可以實(shí)現(xiàn),但是只是一個(gè)需求就引用偌大的一個(gè)庫,感覺不太爽,打算自己自定義一個(gè)。

一、慣例先上效果圖

更新圖

二、GitHub

代碼地址,歡迎指正https://github.com/MNXP/XPPieChart

三、思路

  1、空心圖(一個(gè)大圓中心繪制一個(gè)小圓)
  2、根據(jù)數(shù)據(jù)算出所占的角度
  3、根據(jù)動(dòng)畫獲取當(dāng)前繪制的角度
  4、根據(jù)當(dāng)前角度獲取Paint使用的顏色
  5、動(dòng)態(tài)繪制即將繪制的 和 繪制已經(jīng)繪制的部分(最重要)

四、實(shí)現(xiàn)

1、空心圖(一個(gè)大圓中心繪制一個(gè)小圓)初始化數(shù)據(jù)

   paint = new Paint();
   paint.setAntiAlias(true);
   paint.setStyle(Paint.Style.FILL_AND_STROKE);

   screenW = DensityUtils.getScreenWidth(context);

   int width = DensityUtils.dip2px(context, 15);//圓環(huán)寬度
   int widthXY = DensityUtils.dip2px(context, 10);//微調(diào)距離

   int pieCenterX = screenW / 2;//餅狀圖中心X
   int pieCenterY = screenW / 3;//餅狀圖中心Y
   int pieRadius = screenW / 4;// 大圓半徑

   //整個(gè)餅狀圖rect
   pieOval = new RectF();
   pieOval.left = pieCenterX - pieRadius;
   pieOval.top = pieCenterY - pieRadius + widthXY;
   pieOval.right = pieCenterX + pieRadius;
   pieOval.bottom = pieCenterY + pieRadius + widthXY;

   //里面的空白rect
   pieOvalIn = new RectF();
   pieOvalIn.left = pieOval.left + width;
   pieOvalIn.top = pieOval.top + width;
   pieOvalIn.right = pieOval.right - width;
   pieOvalIn.bottom = pieOval.bottom - width;

   //里面的空白畫筆
   piePaintIn = new Paint();
   piePaintIn.setAntiAlias(true);
   piePaintIn.setStyle(Paint.Style.FILL);
   piePaintIn.setColor(Color.parseColor("#f4f4f4"));

2、根據(jù)數(shù)據(jù)算出所占的角度

使用遞歸保證cakeValues的值的總和必為100,然后根據(jù)值求出角度

  private void settleCakeValues(int i) {
    float sum = getSum(cakeValues, i);
    CakeValue value = cakeValues.get(i);
    if (sum <= 100f) {
      value.setItemValue(100f - sum);
      cakeValues.set(i, value);
    } else {
      value.setItemValue(0);
      settleCakeValues(i - 1);
    }
  }

3、根據(jù)動(dòng)畫獲取當(dāng)前繪制的角度

curAngle就是當(dāng)前繪制的角度,drawArc()就是繪制的方法

cakeValueAnimator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float mAngle = obj2Float(animation.getAnimatedValue("angle"));
        curAngle = mAngle;
        drawArc();
      }
    });

4、根據(jù)當(dāng)前角度獲取Paint使用的顏色

根據(jù)當(dāng)前的角度,計(jì)算當(dāng)前是第幾個(gè)item,通過paint.setColor(Color.parseColor(cakeValues.get(colorIndex).getColors()));來設(shè)置paint的顏色

private int getCurItem(float curAngle) {
    int res = 0;
    for (int i = 0; i < itemFrame.length; i++) {
      if (curAngle <= itemFrame[i] * ANGLE_NUM) {
        res = i;
        break;
      }
    }
    return res;
  }

5、動(dòng)態(tài)繪制即將繪制的 和 繪制已經(jīng)繪制的部分

最重要的一步,我的需求是4類,用不同的顏色

繪制當(dāng)前顏色的扇形,curStartAngle扇形的起始位置,curSweepAngle扇形的終止位置

 paint.setColor(Color.parseColor(cakeValues.get(colorIndex).getColors()));
 float curStartAngle = 0;
 float curSweepAngle = curAngle;
 if (curItem > 0) {
   curStartAngle = itemFrame[curItem - 1] * ANGLE_NUM;
   curSweepAngle = curAngle - (itemFrame[curItem - 1] * ANGLE_NUM);
  }
  canvas.drawArc(pieOval, curStartAngle, curSweepAngle, true, paint);

繪制已經(jīng)繪制的扇形。根據(jù)curItem判斷繪制過得扇形

for (int i = 0; i < curItem; i++) {
  paint.setColor(Color.parseColor(cakeValues.get(i).getColors()));
  if (i == 0) {
    canvas.drawArc(pieOval, startAngle,(float) cakeValues.get(i).getItemValue() * ANGLE_NUM, true, paint);
    continue;
  }
  canvas.drawArc(pieOval,itemFrame[i - 1] * ANGLE_NUM,(float) cakeValues.get(i).getItemValue() * ANGLE_NUM, true, paint);
}

繪制中心的圓

canvas.drawArc(pieOvalIn, 0, 360, true, piePaintIn);

6、特別注意

isFirst判斷是夠是第一次繪制(繪制完成后,home鍵進(jìn)入后臺(tái),再次進(jìn)入,不需要?jiǎng)討B(tài)繪制)

 @Override
  protected void onDraw(Canvas canvas) {
    if (isFirst && isDrawByAnim) {
      drawCakeByAnim();
    }
    isFirst = false;
  }

isDrawByAnim判斷是否需要?jiǎng)赢嬂L制

drawCake()為靜態(tài)繪制餅狀圖

public void surfaceCreated(SurfaceHolder holder) {
  if (!isFirst||!isDrawByAnim)
     drawCake();
}

更新

增加立體效果,提取配置參數(shù)

<declare-styleable name="CakeSurfaceView">
    <attr name="isDrawByAnim" format="boolean"/>//是否動(dòng)畫
    <attr name="isSolid" format="boolean"/>//是否立體
    <attr name="duration" format="integer|reference"/>//動(dòng)畫時(shí)間
    <attr name="defaultColor" format="string"/>//默認(rèn)顏色

    <attr name="ringWidth" format="integer|reference"/>//圓環(huán)寬度
    <attr name="solidWidth" format="integer|reference"/>//立體寬度
    <attr name="fineTuningWidth" format="integer|reference"/>//微調(diào)寬度
  </declare-styleable>

xml中使用

<com.xp.xppiechart.view.CakeSurfaceView
      android:id="@+id/assets_pie_chart"
      android:background="#ffffff"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:defaultColor="#ff8712"
      app:ringWidth="20"
      app:solidWidth="5"
      app:duration="3000"
      app:isSolid="true"
      app:isDrawByAnim="true"/>

以上就是簡(jiǎn)單的實(shí)現(xiàn)動(dòng)態(tài)繪制餅狀圖,待完善,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

民权县| 双桥区| 曲麻莱县| 兴山县| 雷山县| 甘南县| 辽源市| 陇南市| 乐亭县| 莲花县| 沈丘县| 达州市| 三明市| 厦门市| 四会市| 石屏县| 贺兰县| 民乐县| 舟曲县| 宜兰市| 山东省| 阳城县| 富平县| 河源市| 罗平县| 德庆县| 诸城市| 奉贤区| 昭平县| 师宗县| 高雄县| 西乡县| 隆德县| 鸡东县| 福州市| 镇原县| 津市市| 阳城县| 定结县| 舒城县| 开原市|