Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼
具體代碼如下所示:
//MyCircleView類 public class MyCircleView extends View{ //當(dāng)前畫(huà)筆畫(huà)圓的顏色 private int CurrenCircleBoundColor; private Paint paint; ////從xml中獲取的顏色 private int circleBundColor; private float circleBoundWidth; private float pivotX; private float pivotY; private float radius=130; private float currentDegree=0; private int currentSpeed=1; private boolean isPause=false; public MyCircleView(Context context) { super(context); initView(context); } public MyCircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCircleView); for (int i = 0; i < typedArray.getIndexCount(); i++) { //就是我們自定義的屬性的資源id int attr = typedArray.getIndex(i); switch (attr){ case R.styleable.MyCircleView_circlr_bound_color: circleBundColor = typedArray.getColor(attr, Color.RED); CurrenCircleBoundColor=circleBundColor; break; case R.styleable.MyCircleView_circlr_bound_width: circleBoundWidth = typedArray.getDimension(attr, 3); break; } } } public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context){ paint = new Paint(); } public void setColor(int color){ if (CurrenCircleBoundColor!=color){ CurrenCircleBoundColor=color; }else { CurrenCircleBoundColor=circleBundColor; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setAntiAlias(true); paint.setColor(CurrenCircleBoundColor); paint.setStrokeWidth(circleBoundWidth); paint.setStyle(Paint.Style.STROKE); pivotX = getWidth() / 2; pivotY = getHeight() / 2; canvas.drawCircle(pivotX,pivotY,radius,paint); canvas.save(); //旋轉(zhuǎn)畫(huà)布 , 如果旋轉(zhuǎn)的的度數(shù)大的話,視覺(jué)上看著是旋轉(zhuǎn)快的 canvas.rotate(currentDegree,pivotX,pivotY); //提供了一些api可以用來(lái)畫(huà)線(畫(huà)路徑) Path path = new Path(); //從哪開(kāi)始畫(huà) 從A開(kāi)始畫(huà) path.moveTo(pivotX+radius,pivotY); //從A點(diǎn)畫(huà)一個(gè)直線到D點(diǎn) path.lineTo(pivotX+radius-20,pivotY-20); //從D點(diǎn)畫(huà)一個(gè)直線到B點(diǎn) path.lineTo(pivotX+radius,pivotY+20); //從B點(diǎn)畫(huà)一個(gè)直線到C點(diǎn) path.lineTo(pivotX+radius+20,pivotY-20); //閉合 -- 從C點(diǎn)畫(huà)一個(gè)直線到A點(diǎn) path.close(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLACK); canvas.drawPath(path,paint); canvas.restore(); //旋轉(zhuǎn)的度數(shù)一個(gè)一個(gè)度數(shù)增加, 如果乘以一個(gè)速度的話,按一個(gè)速度速度增加 currentDegree+=1*currentSpeed; if (!isPause){ invalidate(); } } public void speed(){ ++currentSpeed; if (currentSpeed>=10){ currentSpeed=10; Toast.makeText(getContext(),"我比閃電還快",Toast.LENGTH_SHORT).show(); } } public void slowDown(){ --currentSpeed; if (currentSpeed<=1){ currentSpeed=1; } } public void pauseOrStart(){ //如果是開(kāi)始狀態(tài)的話去重新繪制 if (isPause){ isPause=!isPause; invalidate(); }else { isPause=!isPause; } } } //主頁(yè)面 public class MainActivity extends AppCompatActivity { //全局變量 private MyCircleView my_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 my_view = (MyCircleView) findViewById(R.id.my_view); } public void onClick(View view){ my_view.setColor(Color.BLUE); } public void add(View view){ my_view.speed(); } public void slow(View view){ my_view.slowDown(); } public void pauseOrStart(View view){ my_view.pauseOrStart(); } } 主頁(yè)面布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lx_20170928.MainActivity"> <Button android:id="@+id/set_color_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="onClick" android:text="設(shè)置顏色" /> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/set_color_btn" android:layout_centerHorizontal="true" android:onClick="add" android:text="加速" /> <Button android:id="@+id/slow" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/add" android:layout_centerHorizontal="true" android:onClick="slow" android:text="減速" /> <Button android:id="@+id/pause_or_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/slow" android:layout_centerHorizontal="true" android:onClick="pauseOrStart" android:text="暫定/開(kāi)始" /> <com.example.lx_20170928.MyCircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_view" android:layout_centerInParent="true" app:circlr_bound_color="@color/colorAccent" app:circlr_bound_width="3dp" /> </RelativeLayout> //在values建一個(gè)attrs.xml <resources> <declare-styleable name="MyCustomCircleArrowView"> <attr name="circlr_bound_width" format="dimension"></attr> <attr name="circlr_bound_color" format="color"></attr> </declare-styleable> </resources>
效果圖如下所示:
總結(jié)
以上所述是小編給大家介紹的Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android隱式意圖激活瀏覽器的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇android隱式意圖激活瀏覽器的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Android進(jìn)階——安卓調(diào)用ESC/POS打印機(jī)打印實(shí)例
本篇文章主要介紹了Android進(jìn)階——安卓調(diào)用ESC/POS打印機(jī)打印實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04android webview 簡(jiǎn)單瀏覽器實(shí)現(xiàn)代碼
android webview 簡(jiǎn)單瀏覽器實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05Android SharedPreferences四種操作模式使用詳解
這篇文章主要介紹了Android SharedPreferences四種操作模式使用詳解的相關(guān)資料,這里介紹了獲取Android SharedPreferences的兩種方法及比較,和操作模式的介紹,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Kotlin整合Vertx開(kāi)發(fā)Web應(yīng)用
這篇文章主要介紹了Kotlin整合Vertx開(kāi)發(fā)Web應(yīng)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02使用Android開(kāi)發(fā)接入第三方原生SDK實(shí)現(xiàn)微信登錄
這篇文章主要介紹了使用Android開(kāi)發(fā)接入第三方原生SDK實(shí)現(xiàn)微信登錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android開(kāi)發(fā)之獲取單選與復(fù)選框的值操作示例
這篇文章主要介紹了Android開(kāi)發(fā)之獲取單選與復(fù)選框的值操作,結(jié)合實(shí)例形式分析了Android針對(duì)單選按鈕、復(fù)選框的事件響應(yīng)、數(shù)值獲取等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04