您好,登錄后才能下訂單哦!
小編給大家分享一下Android如何實現(xiàn)自定義開關(guān)按鈕,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
獲取背景顏色,按鈕顏色,開關(guān)狀態(tài)
@SuppressLint("ResourceAsColor") private void initParame(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ToggleColorY, defStyleAttr, 0); mOpenBGColor = typedArray.getColor(R.styleable.ToggleColorY_tby_open_bg, getResources().getColor(R.color.tby_orange)); mCloseBGColor = typedArray.getColor(R.styleable.ToggleColorY_tby_close_bg, getResources().getColor(R.color.tby_gray)); mTouchColor = typedArray.getColor(R.styleable.ToggleColorY_tby_touch, getResources().getColor(R.color.tby_read)); mIsOpen = typedArray.getBoolean(R.styleable.ToggleColorY_tby_state, false); typedArray.recycle(); }
初始化畫筆和 RectF
// Paint.Style.FILL設(shè)置只繪制圖形內(nèi)容 // Paint.Style.STROKE設(shè)置只繪制圖形的邊 // Paint.Style.FILL_AND_STROKE設(shè)置都繪制 private void initPaint() { //開關(guān) 開背景 mOpenBGPaint = new Paint(); mOpenBGPaint.setAntiAlias(true); mOpenBGPaint.setStyle(Paint.Style.FILL_AND_STROKE); mOpenBGPaint.setColor(mOpenBGColor); //開關(guān) 關(guān)背景 mCloseBGPaint = new Paint(); mCloseBGPaint.setAntiAlias(true); mCloseBGPaint.setStyle(Paint.Style.FILL_AND_STROKE); mCloseBGPaint.setColor(mCloseBGColor); //Touch 圓形 mTouchPaint = new Paint(); mTouchPaint.setAntiAlias(true); mTouchPaint.setStyle(Paint.Style.FILL_AND_STROKE); mTouchPaint.setColor(mTouchColor); //開 RectF mRectFOpen = new RectF(); //關(guān) RectF mRectFClose = new RectF(); }
在 onLayout 時獲取整個控件寬高,并且設(shè)置 RectF 尺寸
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mWidth = getWidth(); mHeight = getHeight(); initView(); } private void initView() { mRectFClose.set(0, 0, mWidth, mHeight); mRectFOpen.set(0, 0, mWidth, mHeight); }
繪制開關(guān)初始狀態(tài)以及滑動按鈕得初始狀態(tài)
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //開關(guān)初始狀態(tài) if (mIsOpen) { canvas.drawRoundRect(mRectFOpen, mHeight / 2, mHeight / 2, mOpenBGPaint);//開 } else { canvas.drawRoundRect(mRectFClose, mHeight / 2, mHeight / 2, mCloseBGPaint);//關(guān) } firstDraw(); //繪制滑動按鈕 canvas.drawCircle(mSlidingDistance, mHeight / 2, mHeight / 2, mTouchPaint); } /** * 根據(jù)開關(guān)初始狀態(tài)設(shè)置滑動按鈕位置 */ private void firstDraw() { if (!mIsFirst) { if (mIsOpen) { mSlidingDistance = mWidth - mHeight / 2; } else { mSlidingDistance = mHeight / 2; } mIsFirst = !mIsFirst; } }
剩下就是處理開關(guān)的滑動和點擊
Down->Up, 點擊事件.
Down->Move->Up,滑動事件
此處分Move小于一定距離也認(rèn)為是點擊,只有Move距離大于定值才認(rèn)為是一個滑動事件組
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //mLastX 是記錄手指按下的點X坐標(biāo)值 //mStartX 表示的是當(dāng)前滑動的起始點X坐標(biāo)值 mLastX = mStartX = event.getX(); //2種情況 //1, Down->Up,若是這個順序,手指抬起時候,按照點擊邏輯切換開關(guān) //2, Down->Move->Up,若是這個順序, 手指抬起時候, 就按照滑動邏輯切換開關(guān) mIsEnableClick = true; break; case MotionEvent.ACTION_MOVE: float mEndX = event.getX(); //滑動起始坐標(biāo)與滑動后坐標(biāo)值相減,得到手指移動距離 float distanceX = mEndX - mStartX; //對手指移動距離進(jìn)行累加,這個距離是圓心X軸坐標(biāo) mSlidingDistance += distanceX; //判斷左右兩個臨界值,不能超出左右側(cè)邊值 if (mSlidingDistance < mHeight / 2) { mSlidingDistance = mHeight / 2; } if (mSlidingDistance >= mWidth - mHeight / 2) { mSlidingDistance = mWidth - mHeight / 2; } //重繪,到這一步,圓就隨手指開始移動了 invalidate(); //手指按下坐標(biāo)與滑動最后坐標(biāo)差值 float mMinDistanceX = Math.abs(mEndX - mLastX); //判斷差值 //1,如果差值大于8, 則認(rèn)為是滑動, 如果用戶松開按鈕則按照滑動條件判斷 //1,如果差值小于8, 則認(rèn)為是點擊, 如果用戶此時松開按鈕則按照點擊條件判斷 if (mMinDistanceX > 8) { mIsEnableClick = false; } else { mIsEnableClick = true; } //更新滑動X軸起始坐標(biāo),為下一次Move事件滑動做準(zhǔn)備 mStartX = event.getX(); break; case MotionEvent.ACTION_UP: if (!mIsEnableClick) { //當(dāng)判定為滑動時, 首先判斷這次滑動累加的距離, 如果大于一半則開關(guān)取反 if (mSlidingDistance >= mWidth / 2) { mIsOpen = true; } else { mIsOpen = false; } //設(shè)置好開關(guān)Flag,執(zhí)行替換背景 drawToggle(); } else { mIsOpen = !mIsOpen; drawToggle(); } break; } return true; } /** * 按鈕重繪 */ public void drawToggle() { if (mIsOpen) { //開 mSlidingDistance = mWidth - mHeight / 2; } else { //關(guān) mSlidingDistance = mHeight / 2; } if (onClick != null) { onClick.click(mIsOpen); } invalidate(); }
項目地址
效果圖
看完了這篇文章,相信你對“Android如何實現(xiàn)自定義開關(guān)按鈕”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。