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

Android自定義控件實(shí)現(xiàn)帶數(shù)值和動畫的圓形進(jìn)度條

 更新時間:2018年12月25日 09:51:16   作者:王世暉  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)帶數(shù)值和動畫的圓形進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例實(shí)現(xiàn)一個如下圖所示的Android自定義控件,可以直觀地展示某個球隊在某個賽季的積分?jǐn)?shù)和勝場、負(fù)場、平局?jǐn)?shù)

首先對畫布進(jìn)行區(qū)域劃分,整個控件分上下兩部分

上邊是個大的圓環(huán),圓環(huán)中間兩行文字,沒什么難度,選好圓心坐標(biāo)和半徑后直接繪制即可,繪制文字也是如此。

下部分是三個小的圓弧進(jìn)度條,弧的末端繪制一個小的實(shí)心圓

首先選好坐標(biāo)和半徑,然后先繪制三個圓環(huán)作為弧形進(jìn)度條的背景

之后從12點(diǎn)鐘開始繪制進(jìn)度弧,知道了圓環(huán)的圓心和半徑,也知道了弧對應(yīng)于12點(diǎn)鐘和圓環(huán)圓心的偏移角度

通過三角函數(shù)可以計算出進(jìn)度弧終點(diǎn)坐標(biāo),以進(jìn)度弧終點(diǎn)坐標(biāo)為圓心繪制一個小的實(shí)心圓即可

動畫效果通過Handler的postDelayed方法觸發(fā)重繪即可實(shí)現(xiàn)

在項(xiàng)目中的效果如圖所示:

測試代碼如下:

final Random random=new Random();
final ScoreBoardView myView=(ScoreBoardView)findViewById(R.id.custom_view);
myView.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v){
  myView.setColor(Color.BLUE);
  myView.setScore(random.nextInt(28));
  myView.setWinDrawLose(random.nextInt(12),random.nextInt(15),random.nextInt(26));
 }
});

完整代碼如下:

public class ScoreBoardView extends View {
 private Context context;
 /*弧的顏色*/
 private int mColor;
 /*積分?jǐn)?shù),勝場數(shù),平局?jǐn)?shù),負(fù)場數(shù)*/
 private int mScore, mWinNumber, mDrawNumber, mLoseNumber;
 /*傳入數(shù)字的最大值*/
 private final int FULL_SCORE = 30;
 /*動畫插值器*/
 DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator();
 /*動畫持續(xù)時間(刷新次數(shù))*/
 private int mDuration = 10;
 /*動畫刷新過程中的當(dāng)前值*/
 private int mCurrentTime = 0;
 private TypedValue typedValue;
 private TypedValue typedValue1;
 private Handler mHandler = new Handler();
 private Runnable mAnimation = new Runnable() {
  @Override
  public void run() {
   if (mCurrentTime < mDuration) {
    mCurrentTime++;
    /*導(dǎo)致重繪調(diào)用onDraw,onDraw最后再次postDelay執(zhí)行此動畫,直到達(dá)到指定的次數(shù)*/
    ScoreBoardView.this.invalidate();
   }
  }
 };
 /*繪制圖形*/
 private Paint paint = new Paint();
 /*繪制文字*/
 private Paint paintText = new Paint();
 
 public ScoreBoardView(Context context) {
  super(context);
  this.context=context;
  init();
 }
 
 public ScoreBoardView(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.context=context;
  init();
 }
 
 public ScoreBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  this.context=context;
  init();
 }
 
 private void init() {
  /*數(shù)據(jù)初始化,默認(rèn)屬性*/
  mColor = Color.rgb(95, 112, 72);
  mScore = 0;
  mWinNumber = 0;
  mDrawNumber = 0;
  mLoseNumber = 0;
  typedValue=new TypedValue();
  typedValue1=new TypedValue();
  context.getTheme().resolveAttribute(R.attr.maintextclor, typedValue, true);
  context.getTheme().resolveAttribute(R.attr.maintextclor_reverse,typedValue1,true);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  /*獲取控件總的寬高*/
  float totalWidth = getWidth();
  float totalHeight = getHeight();
  /*
  * DecelerateInterpolator:動畫從開始到結(jié)束,變化率是一個減速的過程。
  * AccelerateInterpolator:動畫從開始到結(jié)束,變化率是一個加速的過程。
  * CycleInterpolator:動畫從開始到結(jié)束,變化率是循環(huán)給定次數(shù)的正弦曲線
  * AccelerateDecelerateInterpolator:動畫從開始到結(jié)束,變化率是先加速后減速的過程。
  * LinearInterpolator:動畫從開始到結(jié)束,變化率是線性變化。
  * */
  /*計算當(dāng)前時刻動畫進(jìn)度值*/
  float AnimCurrentValue =mDecelerateInterpolator.getInterpolation(1.0f * mCurrentTime / mDuration);
 
  /*圖形畫筆設(shè)置*/
  paint.setAntiAlias(true);
  paint.setStyle(Paint.Style.STROKE);
 
  /*積分?jǐn)?shù),上邊的大圓*/
  paint.setStrokeWidth(4);
  paint.setColor(mColor);
  /*積分大圓的中心坐標(biāo)和半徑*/
  float score_radius = totalHeight * 1 / 5, score_circle_x = totalWidth / 2, score_circle_y = totalHeight / 3;
  /*繪制圓弧*/
  canvas.drawCircle(score_circle_x, score_circle_y, score_radius, paint);
  /*文字畫筆基本設(shè)置*/
  paintText.setAntiAlias(true);
  paintText.setStyle(Paint.Style.STROKE);
  /*文字從中間開始繪制*/
  /*Paint.Align.CENTER:The text is drawn centered horizontally on the x,y origin*/
  paintText.setTextAlign(Paint.Align.CENTER);
  /*文字畫筆大小和顏色設(shè)置*/
  paintText.setTextSize(score_radius * 3 / 4);
  paintText.setColor(getResources().getColor(typedValue.resourceId));
  /*圓心位置繪制積分?jǐn)?shù)值*/
  canvas.drawText("" + mScore, score_circle_x, score_circle_y, paintText);
  /*縮小字體繪制文本信息*/
  paintText.setTextSize(score_radius * 1 / 4);
  paintText.setAlpha(80);
  /*圓心下邊繪制文本*/
  canvas.drawText("積分", score_circle_x, score_circle_y + score_radius / 2, paintText);
 
  /*設(shè)置畫筆,畫下邊的三個小圓*/
  paint.setStrokeWidth(4);
  paint.setAlpha(255);
  /*下邊三個小圓的半徑*/
  float small_radius = totalHeight / 10;
  /*三個小圓的圓心的x坐標(biāo)*/
  float[] circleXs = new float[]{totalWidth / 2 - score_radius * 3 / 2,
          totalWidth / 2,
          totalWidth / 2 + score_radius * 3 / 2};
  /*三個小圓的圓心的y坐標(biāo)*/
  float circleY = totalHeight * 3 / 4;
  /*計算三個小圓弧掃過的角度*/
  float[] theta_values = new float[]{360 * mWinNumber / FULL_SCORE* AnimCurrentValue,
           360 * mDrawNumber / FULL_SCORE* AnimCurrentValue,
           360 * mLoseNumber / FULL_SCORE* AnimCurrentValue};
  /*設(shè)置畫筆顏色,繪制外圍圓環(huán)*/
  paint.setColor(getResources().getColor(typedValue1.resourceId));
  /*分別繪制三個外圍圓環(huán)*/
  canvas.drawCircle(circleXs[0], circleY, small_radius, paint);//畫WIN背景圓
  canvas.drawCircle(circleXs[1], circleY, small_radius, paint);//畫DRAW背景圓
  canvas.drawCircle(circleXs[2], circleY, small_radius, paint);//畫LOSE背景圓
  /*更改畫筆顏色,繪制圓弧進(jìn)度條*/
  paint.setColor(mColor);
  /*drawArc的起始角度是3點(diǎn)鐘方向,因此要從12點(diǎn)鐘方向開始繪制,起始角度為270度*/
  canvas.drawArc(new RectF(circleXs[0] - small_radius,
         circleY - small_radius,
         circleXs[0] + small_radius,
         circleY + small_radius),
      270, theta_values[0], false, paint);//畫WIN圓形進(jìn)度條
  canvas.drawArc(new RectF(circleXs[1] - small_radius,
         circleY - small_radius,
         circleXs[1] + small_radius,
         circleY + small_radius),
      270, theta_values[1], false, paint);//畫DRAW圓形進(jìn)度條
  canvas.drawArc(new RectF(circleXs[2] - small_radius,
         circleY - small_radius,
         circleXs[2] + small_radius,
         circleY + small_radius),
      270, theta_values[2], false, paint);//畫LOSE圓形進(jìn)度條
  /*繪制圓弧結(jié)束處的小圓點(diǎn),實(shí)心圓*/
  paint.setStyle(Paint.Style.FILL);
  /*已知半徑、圓心位置、便宜角度,根據(jù)三角函數(shù)很方便計算出小實(shí)心圓圓心坐標(biāo)*/
  canvas.drawCircle(circleXs[0] + small_radius * (float) Math.sin(theta_values[0] * Math.PI / 180),
    circleY - small_radius * (float) Math.cos(theta_values[0] * Math.PI / 180), 6, paint);//畫WIN末尾小圓點(diǎn)
  canvas.drawCircle(circleXs[1] + small_radius * (float) Math.sin(theta_values[1] * Math.PI / 180),
    circleY - small_radius * (float) Math.cos(theta_values[1] * Math.PI / 180), 6, paint);//畫DRAW末尾小圓點(diǎn)
  canvas.drawCircle(circleXs[2] + small_radius * (float) Math.sin(theta_values[2] * Math.PI / 180),
    circleY - small_radius * (float) Math.cos(theta_values[2] * Math.PI / 180), 6, paint);//畫LOSE末尾小圓點(diǎn)
 
  /*繪制文字*/
  paintText.setColor(getResources().getColor(typedValue.resourceId));
  paintText.setTextSize(small_radius * 2 / 3);
  /*測量文字大小,確定繪制文字時垂直方向的位置*/
  Paint.FontMetrics fontMetrics = paint.getFontMetrics();
  float textBaseLineOffset = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
  /*在三個小圓的正中心位置繪制相應(yīng)的數(shù)字*/
  canvas.drawText("" + (int)(mWinNumber * AnimCurrentValue), circleXs[0], circleY + textBaseLineOffset, paintText);
  canvas.drawText("" + (int)(mDrawNumber * AnimCurrentValue), circleXs[1], circleY + textBaseLineOffset, paintText);
  canvas.drawText("" + (int)(mLoseNumber * AnimCurrentValue), circleXs[2], circleY + textBaseLineOffset, paintText);
  /*調(diào)整字體大小,繪制文本信息*/
  paintText.setTextSize(small_radius * 4 / 9);
  canvas.drawText("勝場", circleXs[0], circleY - small_radius*4/3, paintText);
  canvas.drawText("平局", circleXs[1], circleY - small_radius*4/3, paintText);
  canvas.drawText("負(fù)場", circleXs[2], circleY - small_radius*4/3, paintText);
  /*20ms刷新一次數(shù)據(jù)*/
  mHandler.postDelayed(mAnimation, 20);//啟動動畫
 }
 
 public void setColor(int mColor) {
  this.mColor = mColor;
  invalidate();
 }
 
 public void setScore(int score) {
  this.mScore = score;
  invalidate();
 }
 
 public void setWinDrawLose(int win,int draw,int lose) {
  this.mWinNumber = win;
  this.mDrawNumber = draw;
  this.mLoseNumber = lose;
  mCurrentTime =0;
  invalidate();
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

五台县| 江津市| 措勤县| 文水县| 丰顺县| 眉山市| 恩平市| 西安市| 江门市| 徐闻县| 收藏| 伊春市| 牙克石市| 浮山县| 沐川县| 桂平市| 彩票| 昆山市| 玉树县| 卫辉市| 林口县| 界首市| 南昌市| 涞源县| 怀远县| 云霄县| 石河子市| 石台县| 松潘县| 忻城县| 平原县| 周口市| 徐闻县| 马关县| 阿拉善右旗| 江川县| 新河县| 木兰县| 陈巴尔虎旗| 丁青县| 仙居县|