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

首頁 > 學院 > 開發設計 > 正文

自定義控件實戰<三> 動態圓弧控件.

2019-11-06 10:03:53
字體:
來源:轉載
供稿:網友

上一篇文章實現一個圖片和文字混合展示的控件,這篇文章在此基礎上面繼續實現一個動態的圓弧控件.

一、在attrs.xml中添加自定義屬性:

<?xml version="1.0" encoding="utf-8"?><resources> <attr name="firstColor" format="color" /> <attr name="secondColor" format="color" /> <attr name="circleLength" format="dimension" /> <attr name="circleWidth" format="dimension" /> <attr name="speed" format="integer" /> <declare-styleable name="Custom二、對應的布局文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context="com.shi.androidstudio.brokenline.MainActivity"> <com.shi.androidstudio.brokenline.SimpleView_03 android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" app:firstColor = "@color/colorAccent" app:secondColor = "@color/colorPrimaryDark" app:speed = "20" app:circleLength = "100dp" app:circleWidth = "20dp" /></LinearLayout>

三、重寫SimpleView_03控件的構造函數:

public SimpleView_03(Context context) { this(context, null); } public SimpleView_03(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SimpleView_03(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); /** * 獲得我們所定義的自定義樣式屬性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0); int length = a.getIndexCount(); for (int i = 0; i < length; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomProgressBar_firstColor: mFirstColor = a.getColor(attr,Color.BLACK); break; case R.styleable.CustomProgressBar_secondColor: mSecondColor = a.getColor(attr,Color.WHITE); break; case R.styleable.CustomProgressBar_circleLength: mCircleLength = a.getDimension(attr, 50.00f); break; case R.styleable.CustomProgressBar_circleWidth: mCircleWidth = a.getDimension(attr, 20.00f); break; case R.styleable.CustomProgressBar_speed: mSpeed = a.getInt(attr,10); break; } } a.recycle(); new Thread(new Runnable() { @Override public void run() { while (true) { mProgress = mProgress % 360; postInvalidate(); mProgress++; if (mProgress == 360) { isNext = !isNext; } SystemClock.sleep(mSpeed); } } }).start(); }

四、重寫SimpleView_03控件的onDraw:

@Override protected void onDraw(Canvas canvas) { Paint mPaint_01 = new Paint(); mPaint_01.setAntiAlias(true); //設置畫筆為無鋸齒 mPaint_01.setColor(mFirstColor); //設置畫筆顏色 canvas.drawColor(Color.WHITE); //白色背景 mPaint_01.setStrokeWidth(mCircleWidth); //線寬 mPaint_01.setStyle(Paint.Style.STROKE); int centerX = getMeasuredWidth()/2; int centerY = getMeasuredHeight()/2; RectF oval = new RectF(); //RectF對象 oval.left = centerX - mCircleLength; //左邊 oval.top = centerY - mCircleLength; //上邊 oval.right = centerX + mCircleLength; //右邊 oval.bottom = centerY + mCircleLength; //下邊 Paint mPaint_02 = new Paint(); mPaint_02.setAntiAlias(true); //設置畫筆為無鋸齒 mPaint_02.setColor(mSecondColor); //設置畫筆顏色 canvas.drawColor(Color.WHITE); //白色背景 mPaint_02.setStrokeWidth(mCircleWidth); //線寬 mPaint_02.setStyle(Paint.Style.STROKE); if(!isNext){ canvas.drawCircle(centerX,centerY,mCircleLength,mPaint_01); canvas.drawArc(oval, 0, mProgress, false, mPaint_02); //繪制圓弧 }else{ canvas.drawCircle(centerX,centerY,mCircleLength,mPaint_02); canvas.drawArc(oval, 0, mProgress, false, mPaint_01); //繪制圓弧 } }

這里講解一下drawCircle和drawArc函數:

drawCircle(float cx, float cy, float radius, Paint paint)官方解釋:Draw the specified circle using the specified paint.使用指定的畫筆繪制一個指定的圓,其中cx,cy為圓的圓心,radius為圓的半徑,paint為畫筆. drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)官方解釋:Draw the specified arc, which will be scaled to fit inside the specified oval.使用指定的畫筆繪制一個指定圓弧,其中oval為圓弧所在的橢圓對象;系統默認在當前頁面建立一個X軸向右,Y軸向下的坐標系,其中的startAngle為圓弧的起始角度,sweepAngle為圓弧的角度,useCenter表示是否顯示半徑連線,為true則顯示圓弧與圓心的半徑連線,false不顯示,paint為畫筆.

五、效果圖:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 志丹县| 昌江| 德阳市| 三台县| 襄汾县| 射洪县| 荆州市| 甘洛县| 泽普县| 肥城市| 杭锦旗| 林西县| 太湖县| 客服| 郸城县| 昆山市| 辉县市| 吉水县| 峡江县| 淳安县| 汝南县| 荆门市| 阳西县| 松潘县| 新密市| 博野县| 余姚市| 元氏县| 肃北| 马鞍山市| 峨边| 定边县| 肇东市| 奎屯市| 壤塘县| 辉县市| 七台河市| 岑溪市| 莲花县| 宜黄县| 郎溪县|