亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

Android自定義控件實現支付寶記賬餅圖

發布時間:2020-10-26 06:22:22 來源:腳本之家 閱讀:148 作者:匡效國 欄目:移動開發

本文實例為大家分享了Android實現支付寶記賬餅圖,點擊旋轉到最下面,供大家參考,具體內容如下

Android自定義控件實現支付寶記賬餅圖

代碼:

package com.example.a_102.myapplication7.ui; 
 
import java.util.ArrayList; 
import java.util.List; 
 
 
import android.animation.Animator; 
import android.animation.ValueAnimator; 
import android.annotation.TargetApi; 
import android.content.Context; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.os.Build; 
import android.text.TextUtils; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.animation.AccelerateInterpolator; 
 
import com.example.a_102.myapplication7.util.Util; 
 
/** 
 * 
 */ 
public class SelectPieView extends View { 
 
 private static final String TAG = "CustomPie_tag"; 
 private int width; 
 private SelectPieCallBack mCallBack; 
 private boolean initPostion = false; 
 /** 
  * 當前選中的區域 
  */ 
 private int currentDownPostion; 
 
 public SelectPieView(Context context) { 
  super(context); 
 } 
 
 public SelectPieView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
 } 
 
 public SelectPieView(Context context, AttributeSet attrs, int defStyleAttr) { 
  super(context, attrs, defStyleAttr); 
 } 
 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  int width; 
  int hight; 
 
  int widthmode = MeasureSpec.getMode(widthMeasureSpec); 
  int widthsize = MeasureSpec.getSize(widthMeasureSpec); 
 
  int hightmode = MeasureSpec.getMode(heightMeasureSpec); 
  int hightsize = MeasureSpec.getSize(heightMeasureSpec); 
 
  if (MeasureSpec.EXACTLY == widthmode) { 
   width = widthsize; 
  } else { 
   width = 200; 
   if (MeasureSpec.AT_MOST == widthmode) { 
    width = Math.min(widthsize, 200); 
   } 
  } 
 
  if (MeasureSpec.EXACTLY == hightmode) { 
   hight = hightsize; 
  } else { 
   hight = 200; 
   if (MeasureSpec.AT_MOST == hightmode) { 
    hight = Math.min(hightsize, 200); 
   } 
  } 
 
  setMeasuredDimension(Math.min(width, hight), Math.min(width, hight)); 
 } 
 
 /** 
  * 筆寬 
  */ 
 private int mPaintWid; 
 /** 
  * 外邊圓半徑 
  */ 
 private int mOutRoot; 
 /** 
  * 內邊圓半徑 
  */ 
 private int mIntRoot; 
 /** 
  * 空白處寬度 
  */ 
 private int emptysize = -1; 
 /** 
  * 點擊前的圓和點擊后的圓半徑差距 
  */ 
 private float betweenSize = 10; 
 /** 
  * 向限 
  */ 
 private int XIANGXAIN; 
 /** 
  * 開始的角度 
  */ 
 private float start = 360; 
 /** 
  * 旋轉過的角度 
  */ 
 private List<startAndRoatData> haveRoats = new ArrayList<>(); 
 /** 
  * 
  */ 
 private String mTitle = "總消費"; 
 /** 
  * 
  */ 
 private String mSubTitle = "00"; 
 /** 
  * 
  */ 
 private String mSubTitleDot = "00"; 
 /** 
  * 是否在運行 
  */ 
 private boolean isRun = true; 
 
 private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 
 private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 /** 
  * 整數部分 
  */ 
 private Paint textPaintSubTitle= new Paint(Paint.ANTI_ALIAS_FLAG); 
 /** 
  * 小數部分 
  */ 
 private Paint textPaintSubTitleDot= new Paint(Paint.ANTI_ALIAS_FLAG); 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
  // reSetData(); 
  if (null == datas || datas.size() == 0) 
   return; 
  width = getWidth(); 
  mOutRoot = width / 2; 
  mPaintWid = Util.dip2px(getContext(), 40); 
  mIntRoot = mOutRoot - mPaintWid; 
 
  paint.setStyle(Paint.Style.STROKE); 
  paint.setColor(Color.RED); 
  paint.setStrokeWidth(mPaintWid); 
  RectF rt = new RectF(mPaintWid / 2 + betweenSize, mPaintWid / 2 + betweenSize, 
    width - mPaintWid / 2 - betweenSize, width - mPaintWid / 2 - betweenSize); 
  RectF rt2 = new RectF(mPaintWid / 2, mPaintWid / 2, width - mPaintWid / 2, width - mPaintWid / 2); 
 
  int size = datas.size(); 
  float allValues = 0; 
  for (int i = 0; i < datas.size(); i++) { 
   allValues += datas.get(i).getValuse(); 
  } 
  // allValues = allValues + emptysize * size; 
  float sigleSize = (360 - emptysize * datas.size()) / (allValues * 1f); 
 
  float end = 0; 
  haveRoats.clear(); 
  for (int i = 0; i < size; i++) { 
   paint.setColor(getResources().getColor(datas.get(i).getColor())); 
   end = datas.get(i).getValuse() * sigleSize; 
 
   if (!isRun && datas.get(i).getPostion() == currentDownPostion && datas.size()>1) { 
    canvas.drawArc(rt2, start + 3, end - 6, false, paint); 
    canvas.drawArc(rt, start + 3, end - 6, false, paint); 
   } else { 
    canvas.drawArc(rt, start, end, false, paint); 
   } 
   Log.i(TAG, "first=" + start % 360 + "==" + end + "postion=" + datas.get(i).getPostion()); 
 
   haveRoats.add(new startAndRoatData(datas.get(i).getPostion(), start % 360, end)); 
 
   start = start + end + emptysize; 
 
  } 
 
  textPaint.setStrokeWidth(Util.dip2px(getContext(), 1)); 
  /** 畫圖片 */ 
  for (int i = 0; i < haveRoats.size(); i++) { 
   startAndRoatData startAndRoatData = haveRoats.get(i); 
 
   float x = 0; 
   float y = 0; 
 
   if (!isRun && currentDownPostion == haveRoats.get(i).getPostion() && datas.size()>1) { 
 
    x = (float) (Math 
      .cos(Math.PI / 180 * (startAndRoatData.getStartAng() + startAndRoatData.getRoatAng() / 2)) 
      * (mIntRoot + mPaintWid / 2) + mOutRoot); 
    y = (float) (Math 
      .sin(Math.PI / 180 * (startAndRoatData.getStartAng() + startAndRoatData.getRoatAng() / 2)) 
      * (mIntRoot + mPaintWid / 2) + mOutRoot); 
   } else { 
    x = (float) (Math 
      .cos(Math.PI / 180 * (startAndRoatData.getStartAng() + startAndRoatData.getRoatAng() / 2)) 
      * (mIntRoot + mPaintWid / 2 - betweenSize) + mOutRoot); 
    y = (float) (Math 
      .sin(Math.PI / 180 * (startAndRoatData.getStartAng() + startAndRoatData.getRoatAng() / 2)) 
      * (mIntRoot + mPaintWid / 2 - betweenSize) + mOutRoot); 
   } 
 
   Rect rect = new Rect((int) (x - mPaintWid / 3), (int) (y - mPaintWid / 3), (int) (x + mPaintWid / 3), 
     (int) (y + mPaintWid / 3)); 
 
   int width = BitmapFactory.decodeResource(getResources(), datas.get(i).getIcon()).getWidth(); 
   // L=n(圓心角度數)× π(圓周率)× r(半徑)/180(角度制) 
   if (startAndRoatData.getRoatAng() * Math.PI * (mIntRoot + mPaintWid / 2) / 180 > width) { 
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), datas.get(i).getIcon()), null, rect, 
      null); 
   } 
  } 
 
  textPaint.setStyle(Paint.Style.FILL); 
  textPaint.setTextSize(Util.dip2px(getContext(), 14)); 
  /** 寫文字 */ 
  canvas.drawText(mTitle, width / 2 - (textPaint.measureText(mTitle)) / 2, 
    width / 2 - Util.dip2px(getContext(), 8), textPaint); 
 
  textPaintSubTitle.setTextSize(Util.dip2px(getContext(), 20)); 
  canvas.drawText(mSubTitle, 
    width / 2 - (textPaintSubTitle.measureText(mSubTitle)) / 2 - (textPaintSubTitleDot.measureText("."+mSubTitleDot) / 2), 
    width / 2 + Util.dip2px(getContext(), 15), textPaintSubTitle); 
 
  textPaintSubTitleDot.setTextSize(Util.dip2px(getContext(), 15)); 
  canvas.drawText("." + mSubTitleDot, 
    width / 2 + textPaintSubTitle.measureText(mSubTitle) /2 - (textPaintSubTitleDot.measureText("." + mSubTitleDot))/2, 
    width / 2 + Util.dip2px(getContext(), 15), textPaintSubTitleDot); 
  //Toast.makeText(getContext(), "=="+textPaint.measureText(mSubTitle), Toast.LENGTH_SHORT).show(); 
  /** 測試基線 */ 
  /* 
   * paint.setColor(Color.BLACK); 
   * paint.setStrokeWidth(Util.dip2px(getContext(), 1)); 
   * 
   * canvas.drawLine(0, width / 2, width, width / 2, paint); 
   * canvas.drawLine(width / 2, 0, width / 2, width, paint); 
   */ 
  /** 
   * 初始化位置 
   * */ 
  if (!initPostion) { 
   initPostion = true; 
 
   startAndRoatData roatData = haveRoats.get(0); 
   float currentCenterAng = roatData.getStartAng() + roatData.getRoatAng() / 2; 
   if (currentCenterAng < 90) { 
    starRoat(start, start + 90 - (roatData.getStartAng() + roatData.getRoatAng() / 2), false); 
   } else if (currentCenterAng > 90 && currentCenterAng < 270) { 
    starRoat(start, start - (currentCenterAng - 90), false); 
   } else { 
    starRoat(start, start + 360 + 90 - (roatData.getStartAng() + roatData.getRoatAng() / 2), false); 
   } 
   currentDownPostion = roatData.getPostion(); 
   isRun = false; 
   invalidate(); 
  } 
 
 } 
 
 /** 
  * 設置顯示金額 
  * 
  * @param number 
  */ 
 public void setNumber(String number) { 
  if (TextUtils.isEmpty(number)) { 
   number = "0.00"; 
  } 
  if (number.contains(".")) { 
   String[] split = number.split("\\."); 
   mSubTitle = split[0]; 
   mSubTitleDot = split[1]; 
  } else { 
   mSubTitle = number; 
   mSubTitleDot = "00"; 
  } 
  if (mSubTitleDot.length() > 2) { 
   mSubTitleDot = mSubTitleDot.substring(0, 2); 
  } 
 } 
 
 private int Lxy2; 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
 
  switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN: 
   float x = event.getX(); 
   float y = event.getY(); 
   double atan = Math.atan((x - mOutRoot) / (mOutRoot - y)); 
   double currntAngle = (atan / Math.PI * 180); 
   double clickAngle = 0; 
   Lxy2 = (int) Math.pow(x - mOutRoot, 2) + (int) Math.pow(mOutRoot - y, 2); 
   if (Math.pow(mIntRoot, 2) < Lxy2 && Lxy2 < Math.pow(mOutRoot, 2)) { 
 
    if (x > width / 2 && y > width / 2) { 
     /** currntAngle第四象限是負數 */ 
     // starRoat(start, (float) (start - currntAngle), true); 
     clickAngle = currntAngle +90; 
 
    } else if (x > width / 2 && y < width / 2) { 
     /** currntAngle第一象限是負數 */ 
     //starRoat(start, (float) (start + 180 - currntAngle), true); 
     clickAngle = currntAngle +270; 
    } else if (x < width / 2 && y < width / 2) { 
     /** currntAngle第二象限是正數 */ 
     //starRoat(start, (float) (start - (180 - Math.abs(currntAngle))), true); 
     clickAngle = currntAngle +270; 
    } else { 
     /** currntAngle第三象限是正數 */ 
     //starRoat(start, (float) (start - Math.abs(currntAngle)), true); 
     clickAngle = currntAngle +90; 
    } 
 
    int i = clickDownPostion(clickAngle); 
    startAndRoatData roatData = haveRoats.get(i); 
    currentDownPostion = roatData.getPostion(); 
    float currentCenterAng = roatData.getStartAng() + roatData.getRoatAng() / 2; 
    if (currentCenterAng < 90) { 
     starRoat(start, start + 90 - (roatData.getStartAng() + roatData.getRoatAng() / 2), true); 
    } else if (currentCenterAng > 90 && currentCenterAng < 270) { 
     starRoat(start, start - (currentCenterAng - 90), true); 
    } else { 
     starRoat(start, start + 360 + 90 - (roatData.getStartAng() + roatData.getRoatAng() / 2), true); 
    } 
 
   } 
 
   return true; 
  } 
 
  return super.onTouchEvent(event); 
 } 
 
 private int clickDownPostion(double clickAngle) { 
  for (int i = 0; i < haveRoats.size(); i++) { 
   startAndRoatData data = haveRoats.get(i); 
   if ((data.getStartAng() < clickAngle && data.getStartAng() + data.getRoatAng() > clickAngle) 
     || (data.getStartAng() + data.getRoatAng() > 360 
       && ((data.getStartAng() + data.getRoatAng()) % 360) > clickAngle)) { 
    return i; 
   } 
  } 
  return 0; 
 } 
 
 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
 public void starRoat(final float star, final float end, boolean isSmooth) { 
  ValueAnimator valueAnimator = ValueAnimator.ofFloat(star, end); 
  valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
   @Override 
   public void onAnimationUpdate(ValueAnimator animation) { 
    float animatedValue = (float) animation.getAnimatedValue(); 
    start = animatedValue; 
    isRun = true; 
    invalidate(); 
   } 
  }); 
  valueAnimator.setDuration(isSmooth ? (long) (700) : 10); 
  valueAnimator.setInterpolator(new AccelerateInterpolator()); 
  valueAnimator.start(); 
  valueAnimator.addListener(new Animator.AnimatorListener() { 
   @Override 
   public void onAnimationStart(Animator animation) { 
 
   } 
 
   @Override 
   public void onAnimationEnd(Animator animation) { 
 
    if (currentDownPostion == -1) { 
     start++; 
     starRoat(start, start++, false); 
    } else { 
     isRun = false; 
     invalidate();//畫突出部分 
     mCallBack.currentPostion(currentDownPostion); 
    } 
   } 
 
   @Override 
   public void onAnimationCancel(Animator animation) { 
 
   } 
 
   @Override 
   public void onAnimationRepeat(Animator animation) { 
 
   } 
  }); 
 } 
 
 private List<PieData> datas = new ArrayList<>(); 
 
 public void reSetData(List<PieData> data) { 
  datas.clear(); 
  datas.addAll(data); 
  float all =0; 
  for (PieData da : datas) { 
   all += da.getValuse(); 
  } 
  if (all < 360) { 
   for (PieData da : datas) { 
    da.setValuse(da.getValuse() * 200); 
   } 
  } 
  for (PieData da : datas) { 
   all += da.getValuse(); 
  } 
  /**強制設置最低值*/ 
  for (PieData da : datas) { 
   if (da.getValuse() / all < 0.03) { 
    da.setValuse((float) (all * 0.03)); 
   } 
  } 
 
  invalidate(); 
 
 } 
 
 /** 
  * 判斷當前選擇的所在區間 
  * 
  * @return 
  */ 
 private int findCurrentDownPostion() { 
  if (haveRoats == null || haveRoats.size() <= 0) { 
   return 1; 
  } 
 
  for (int i = 0; i < haveRoats.size(); i++) { 
 
   float startAng = haveRoats.get(i).getStartAng(); 
   float roatAng = haveRoats.get(i).getRoatAng(); 
   //Utility.Logi(TAG, "currentpostion=sstar=" + startAng + "===rroat=" + roatAng); 
   if (startAng < 90 && (startAng <= 90 && startAng + roatAng > 90)) { 
    // Utility.Logi(TAG, "currentpostion=" + haveRoats.get(i).getPostion()); 
    return haveRoats.get(i).getPostion(); 
   } else if (startAng > 90 && startAng - 360 + roatAng > 90) { 
    //Utility.Logi(TAG, "currentpostion=" + haveRoats.get(i).getPostion()); 
    return haveRoats.get(i).getPostion(); 
   } 
  } 
  return -1; 
 } 
 
 public void setCallBack(SelectPieCallBack callBack) { 
  this.mCallBack = callBack; 
 } 
 
 public interface SelectPieCallBack { 
  void currentPostion(int postion); 
 } 
 
 public static class PieData { 
 
  public PieData(int postion, float valuse, int color, int icon) { 
   this.postion = postion; 
   this.valuse = valuse; 
   this.color = color; 
   this.icon = icon; 
  } 
 
  private int postion; 
 
  private float valuse; 
 
  private int color; 
 
  private int icon; 
 
  public int getPostion() { 
   return postion; 
  } 
 
  public void setPostion(int postion) { 
   this.postion = postion; 
  } 
 
  public float getValuse() { 
   return valuse; 
  } 
 
  public void setValuse(float valuse) { 
   this.valuse = valuse; 
  } 
 
  public int getColor() { 
   return color; 
  } 
 
  public void setColor(int color) { 
   this.color = color; 
  } 
 
  public int getIcon() { 
   return icon; 
  } 
 
  public void setIcon(int icon) { 
   this.icon = icon; 
  } 
 } 
 
 class startAndRoatData { 
 
  private int postion; 
 
  private float startAng; 
 
  private float roatAng; 
 
  public startAndRoatData(int postion, float startAng, float roatAng) { 
   this.postion = postion; 
   this.startAng = startAng; 
   this.roatAng = roatAng; 
  } 
 
  public int getPostion() { 
   return postion; 
  } 
 
  public void setPostion(int postion) { 
   this.postion = postion; 
  } 
 
  public float getStartAng() { 
   return startAng; 
  } 
 
  public void setStartAng(float startAng) { 
   this.startAng = startAng; 
  } 
 
  public float getRoatAng() { 
   return roatAng; 
  } 
 
  public void setRoatAng(float roatAng) { 
   this.roatAng = roatAng; 
  } 
 
  @Override 
  public String toString() { 
   return "startAndRoatData{" + "postion=" + postion + ", startAng=" + startAng + ", roatAng=" + roatAng + '}'; 
  } 
 } 
} 

用法如下:

SelectPieView mPie = (SelectPieView) findViewById(R.id.pie); 
mPie.setCallBack(new SelectPieView.SelectPieCallBack() { 
 @Override 
 public void currentPostion(int postion) { 
  Toast.makeText(CustomViewActivity.this, "postion="+postion, Toast.LENGTH_SHORT).show(); 
 } 
}); 
ArrayList<SelectPieView.PieData> datas = new ArrayList<>(); 
datas.add(new SelectPieView.PieData(1, 200.3f, R.color.read_color, R.drawable.arrow)); 
datas.add(new SelectPieView.PieData(2, 200.3f, R.color.hx_red, R.drawable.arrow)); 
datas.add(new SelectPieView.PieData(3, 200.3f, R.color.text_blue, R.drawable.arrow)); 
mPie.reSetData(datas); 
mPie.setNumber("333.33"); 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

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

AI

佳木斯市| 海盐县| 合水县| 调兵山市| 双峰县| 勃利县| 黄石市| 思茅市| 富阳市| 临朐县| 通化市| 宜兰县| 贡山| 兰坪| 华池县| 康定县| 葫芦岛市| 永善县| 湘潭县| 宝山区| 台中市| 花垣县| 定州市| 衡阳县| 大宁县| 库尔勒市| 吉水县| 彰化市| 丰宁| 施秉县| 康平县| 龙胜| 卢龙县| 商水县| 梧州市| 兴业县| 万宁市| 正安县| 武胜县| 保山市| 周口市|