您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android中利用View實現一個分段選擇按鈕,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1.1 獲取item大小、起始位置
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(isItemZero() || getMeasuredWidth() == 0) return; mHeight = getMeasuredHeight(); int width = getMeasuredWidth(); mItemWidth = (width - 2 * itemHorizontalMargin)/getCount(); mStart = itemHorizontalMargin + mItemWidth * selectedItem; mEnd = width - itemHorizontalMargin - mItemWidth; }
繪制背景,所有的Item,以及選中項
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(isItemZero()) return; drawBackgroundRect(canvas); drawUnselectedItemsText(canvas); drawSelectedItem(canvas); drawSelectedItemsText(canvas); }
* 繪制背景區域
背景區域就是個帶圓角的長方形
/** * 畫背景區域 * @param canvas */ private void drawBackgroundRect(Canvas canvas) { float r = cornersMode == Round?cornersRadius: mHeight >> 1; mPaint.setXfermode(null); mPaint.setColor(backgroundColor); mRectF.set(0, 0, getWidth(), getHeight()); canvas.drawRoundRect(mRectF, r, r, mPaint); }
* 繪制所有未選中Item的文字
輪流繪制所有Item的文字
/** * 畫所有未選中Item的文字 * @param canvas */ private void drawUnselectedItemsText(Canvas canvas) { mTextPaint.setColor(textColor); mTextPaint.setXfermode(null); for (int i = 0; i< getCount(); i++){ int start = itemHorizontalMargin + i * mItemWidth; float x = start + (mItemWidth >> 1) - mTextPaint.measureText(getName(i))/2; float y = (getHeight() >> 1) - (mTextPaint.ascent() + mTextPaint.descent())/2; canvas.drawText(getName(i), x, y, mTextPaint); } }
* 繪制選中項
/** * 畫選中項 * @param canvas */ private void drawSelectedItem(Canvas canvas) { float r = cornersMode == Round?cornersRadius: (mHeight >> 1) - itemVerticalMargin; mPaint.setColor(selectedItemBackgroundColor); mRectF.set(mStart, itemVerticalMargin, mStart + mItemWidth, getHeight() - itemVerticalMargin); canvas.drawRoundRect(mRectF, r, r, mPaint); }
* 繪制選中Item的文字
當選中項移動時,剛移動到下一個Item時,顏色應該是選中的顏色。這里在原來文字之上再畫選中Item的文字顏色,就有了被選中的效果。
/** * 畫選中Item的文字 * @param canvas */ private void drawSelectedItemsText(Canvas canvas) { canvas.saveLayer(mStart, 0, mStart + mItemWidth, getHeight(), null, Canvas.ALL_SAVE_FLAG); mTextPaint.setColor(selectedItemTextColor); mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); int begin = mStart/mItemWidth; int end = (begin + 2) < getCount()?begin+2:getCount(); for (int i = begin; i< end; i++){ int start = itemHorizontalMargin + i * mItemWidth; float x = start + (mItemWidth >> 1) - mTextPaint.measureText(getName(i))/2; float y = (getHeight() >> 1) - (mTextPaint.ascent() + mTextPaint.descent())/2; canvas.drawText(getName(i), x, y, mTextPaint); } canvas.restore(); }
手勢分為三種,ACTION_DOWN、ACTION_MOVE、ACTION_UP,對應動作就是按下,滑動,按起。
當按下時確定按下位置,是在當前Item,則不做處理,當按下位置為其它Item位置,就滑動到其它Item位置。
當手勢滑動時,計算相對滑動值,通過改變mStart
,改變選中項的位置。
當手勢按起時,根據按下位置、速度和方向,判斷是否可用移動到下一個Item。
@Override public boolean onTouchEvent(MotionEvent event) { if(!isEnabled() || !isInTouchMode() || getCount() == 0) return false; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getActionMasked(); if(action == MotionEvent.ACTION_DOWN){ x = event.getX(); onClickDownPosition = -1; final float y = event.getY(); if(isItemInside(x, y)){ return scrollSelectEnabled; }else if(isItemOutside(x, y)){ if(!mScroller.isFinished()){ mScroller.abortAnimation(); } onClickDownPosition = (int) ((x - itemHorizontalMargin)/ mItemWidth); startScroll(positionStart(x)); return true; } return false; }else if(action == MotionEvent.ACTION_MOVE){ if(!mScroller.isFinished() || !scrollSelectEnabled){ return true; } float dx = event.getX() - x; if(Math.abs(dx) > MIN_MOVE_X){ mStart = (int) (mStart + dx); mStart = Math.min(Math.max(mStart, itemHorizontalMargin), mEnd); postInvalidate(); x = event.getX(); } return true; }else if(action == MotionEvent.ACTION_UP){ int newSelectedItem; float offset = (mStart - itemHorizontalMargin)%mItemWidth; float itemStartPosition = (mStart - itemHorizontalMargin) * 1.0f/ mItemWidth; if(!mScroller.isFinished() && onClickDownPosition != -1){ newSelectedItem = onClickDownPosition; }else{ if(offset == 0f){ newSelectedItem = (int)itemStartPosition; }else { VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(VELOCITY_UNITS, mMaximumFlingVelocity); int initialVelocity = (int) velocityTracker.getXVelocity(); float itemRate = offset/mItemWidth; if (isXVelocityCanMoveNextItem(initialVelocity, itemRate)){ newSelectedItem = initialVelocity > 0?(int)itemStartPosition+1:(int)itemStartPosition; }else { newSelectedItem = Math.round(itemStartPosition); } newSelectedItem = Math.max(Math.min(newSelectedItem, getCount() - 1), 0); startScroll(getXByPosition(newSelectedItem)); } } onStateChange(newSelectedItem); mVelocityTracker = null; onClickDownPosition = -1; return true; } return super.onTouchEvent(event); }
當手機屏幕方向轉換或者內存不足等情況下, 視圖會重新加載,這樣就會導致狀態丟失。使用onSaveInstanceState
和onRestoreInstanceState
方法保存并恢復狀態。
@Override public Parcelable onSaveInstanceState() { Parcelable parcelable = super.onSaveInstanceState(); SelectedItemState pullToLoadState = new SelectedItemState(parcelable); pullToLoadState.setSelectedItem(selectedItem); return pullToLoadState; } @Override public void onRestoreInstanceState(Parcelable state) { if(!(state instanceof SelectedItemState)) return; SelectedItemState pullToLoadState = ((SelectedItemState)state); super.onRestoreInstanceState(pullToLoadState.getSuperState()); selectedItem = pullToLoadState.getSelectedItem(); invalidate(); }
上述內容就是怎么在Android中利用View實現一個分段選擇按鈕,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。