小編給大家分享一下Android如何實現(xiàn)自定義View圓形和拖動圓、跟隨手指拖動效果,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)公司專注于周村網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供周村營銷型網(wǎng)站建設,周村網(wǎng)站制作、周村網(wǎng)頁設計、周村網(wǎng)站官網(wǎng)定制、微信小程序服務,打造周村網(wǎng)絡公司原創(chuàng)品牌,更為您提供周村網(wǎng)站排名全網(wǎng)營銷落地服務。單純的自定義一個圓非常簡單 只需要幾步就完成 拖動圓添加實現(xiàn)觸摸事件即可
我在第一次自定義View圓遇到的幾個Bug:
1.拖動圓的話在xml里面設置的自定義圓的寬和高是它能活動的空間的大小 不是圓控件的大小 如果你定義了100dp 拖動它的時候超過100dp這個距離這個圓就會看不見 就像下面這樣 如果想活動于整個屏幕直接給寬和高match_parent
屬性就好了
2.我在定義充滿屬性match_parent
的時候運行會報錯,什么方法都用了就是不行,耐心等待過一會就好了…有可能是studio沒來得及編譯過來
下面開始寫代碼: 先是單純的創(chuàng)建一個圓形 創(chuàng)建一個類繼承View 實現(xiàn)onDraw方法
public class CustomView extends View { //創(chuàng)建point對象 參數(shù)為x坐標和y坐標 private PointF point = new PointF(100, 100); public CustomView(Context context) { super(context); } public CustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //參數(shù)為圓的橫坐標 ,縱坐標,半徑,創(chuàng)建 canvas.drawCircle(point.x,point.y, 50, new Paint()); } }
XML里、自己定義的view類的名字:
<ydtx.bwie.com.xiangmu_project02.CustomView android:layout_width="match_parent" android:layout_height="match_parent" />
一個圓就這樣創(chuàng)建好了 直接運行就可以了 ManActivity里什么也不用改
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
下面是添加拖動圓的功能 非常簡單 實現(xiàn)觸摸監(jiān)聽即可 代碼非常少 如下:
public class CustomView extends View { //創(chuàng)建point對象 參數(shù)為x坐標和y坐標 private PointF point = new PointF(100, 100); public CustomView(Context context) { super(context); } public CustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //參數(shù)為圓的橫坐標 ,縱坐標,半徑,創(chuàng)建 canvas.drawCircle(point.x,point.y, 50, new Paint()); } //觸摸事件 @Override public boolean onTouchEvent(MotionEvent event) { //獲得觸摸事件 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; //ACTION_MOVE不要設置break,否則圓形不會跟隨手指活動 只會手指松開屏幕的時候圓形直接到了屏幕停止的位置 case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: //獲取手指觸摸位置的x坐標 point.x = event.getX(); //獲取手指觸摸位置的y坐標 point.y = event.getY(); //啟動 postInvalidate(); break; } return true; } }
看完了這篇文章,相信你對“Android如何實現(xiàn)自定義View圓形和拖動圓、跟隨手指拖動效果”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:Android如何實現(xiàn)自定義View圓形和拖動圓、跟隨手指拖動效果-創(chuàng)新互聯(lián)
當前鏈接:http://www.rwnh.cn/article30/dsdcpo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、做網(wǎng)站、自適應網(wǎng)站、商城網(wǎng)站、網(wǎng)站制作、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容