国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義View實現(xiàn)圓環(huán)交替效果

2019-12-12 05:35:59
字體:
來源:轉載
供稿:網(wǎng)友

下面請先看效果圖:

  

看上去是不很炫的樣子,它的實現(xiàn)上也不是很復雜,重點在與onDraw()方法的繪制。

首先是我們的attrs文件:

<?xml version="1.0" encoding="utf-8"?><resources>  <attr name="firstColor" format="color"/> <attr name="secondColor" format="color"/> <attr name="circleWidth" format="dimension"/> <attr name="speed" format="integer"/>  <declare-styleable name="CustomView">  <attr name="firstColor" />  <attr name="secondColor" />  <attr name="circleWidth" />  <attr name="speed" /> </declare-styleable> </resources>

接下來是我們重寫View類的自定義View類:

public class MySelfCircleView extends View {  /*  * 第一圈顏色  */ int firstColor; /*  * 第二圈顏色  */ int secondColor; /*  * 圓的寬度  */ int circleWidth; /*  * 速率  */ int speed; /*  * 畫筆  */ Paint mPaint; /*  * 進度  */ int mProgress; /*  * 是否切換標志  */ boolean isNext;  public MySelfCircleView(Context context, AttributeSet attrs,   int defStyleAttr) {  super(context, attrs, defStyleAttr);   TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);  int n = typedArray.getIndexCount();  for(int i=0; i<n; i++){   int attr = typedArray.getIndex(i);   switch (attr) {    case R.styleable.CustomView_firstColor:     firstColor = typedArray.getColor(attr, Color.RED);     break;    case R.styleable.CustomView_secondColor:     secondColor = typedArray.getColor(attr, Color.RED);     break;    case R.styleable.CustomView_circleWidth:     circleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(        TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));      break;    case R.styleable.CustomView_speed:     speed = typedArray.getInt(attr, 20);     break;   }  }  typedArray.recycle();    mPaint = new Paint();  new Thread(new Runnable() {   @Override   public void run() {    while (true) {     mProgress++;     if (mProgress == 360) {       mProgress = 0;       if (!isNext)        isNext = true;       else        isNext = false;      }      postInvalidate();      try {       Thread.sleep(speed);      } catch (InterruptedException e) {       e.printStackTrace();      }     }   }  }).start(); } public MySelfCircleView(Context context, AttributeSet attrs) {  this(context, attrs, 0); } public MySelfCircleView(Context context) {  this(context, null); }  @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  int centre = getWidth() / 2; // 獲取圓心的x坐標   int radius = centre - circleWidth / 2;// 半徑   mPaint.setStrokeWidth(circleWidth); // 設置圓環(huán)的寬度   mPaint.setAntiAlias(true); // 消除鋸齒   mPaint.setStyle(Paint.Style.STROKE); // 設置空心   RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定義的圓弧的形狀和大小的界限   if (!isNext) {// 第一顏色的圈完整,第二顏色跑    mPaint.setColor(firstColor); // 設置圓環(huán)的顏色    canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán)    mPaint.setColor(secondColor); // 設置圓環(huán)的顏色    canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進度畫圓弧   } else {    mPaint.setColor(secondColor); // 設置圓環(huán)的顏色    canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán)    mPaint.setColor(firstColor); // 設置圓環(huán)的顏色    canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進度畫圓弧   } } }

最后是我們的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:zhy="http://schemas.android.com/apk/res/com.example.myselfview" android:layout_width="match_parent" android:layout_height="match_parent" >  <com.example.myselfview.view.MySelfCircleView   android:layout_width="120dp"  android:layout_height="120dp"  android:layout_marginTop="20dp"  android:layout_alignParentTop="true"  android:layout_centerHorizontal="true"  zhy:circleWidth="15dp"  zhy:firstColor="#D4F668"  zhy:secondColor="#2F9DD2"  zhy:speed="10" />   <com.example.myselfview.view.MySelfCircleView   android:layout_width="200dp"  android:layout_height="200dp"  android:layout_alignParentBottom="true"  android:layout_centerHorizontal="true"  zhy:circleWidth="24dp"  android:layout_marginBottom="40dp"  zhy:firstColor="#16A3FA"  zhy:secondColor="#D20F02"  zhy:speed="5" />  </RelativeLayout>

總結

好了,到這里我們的效果就算大工告成,感興趣的朋友可以寫寫看,個人感覺自定義View需要大量的練習,才能為我所用。希望本文對大家開發(fā)Android能有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永春县| 拜泉县| 四川省| 射洪县| 永川市| 拜泉县| 遂昌县| 九江市| 龙川县| 临沭县| 科技| 丰城市| 陕西省| 蛟河市| 黑水县| 云浮市| 千阳县| 泾阳县| 晴隆县| 岗巴县| 宁乡县| 鄢陵县| 灌阳县| 循化| 大安市| 吕梁市| 苍溪县| 林芝县| 新巴尔虎左旗| 白玉县| 威远县| 浠水县| 新泰市| 上蔡县| 开阳县| 登封市| 印江| 达拉特旗| 额尔古纳市| 清苑县| 九江市|