Android自定義控制條效果
本文實例為大家分享了Android自定義控制條效果的具體代碼,供大家參考,具體內(nèi)容如下
ControlBar
自定義一個可以調節(jié)大小的控件,可以根據(jù)寬高來指定控制條方向。當width >= heigth時,為橫向控制條,否則為豎向控制條
onMeasure
根據(jù)用戶給定的width與height計算控制條的坐標。
1.主要的計算思路
先計算橫向的的坐標點,豎向的坐標點即橫向的逆時針旋轉90度再向下移一個heigth的長度。
//橫向坐標點mHorLArcFirstPathX = mRadius + mLArcLength;mHorLArcFirstPathY = startY + mBarHeight * (1.0f - LITTLE_ARC_PER_WIDTH) / 2.0f ;//對應豎向坐標點mLArcFirstPathX = mHorLArcFirstPathY;mLArcFirstPathY = -mHorLArcFirstPathX + longSide;
onDraw
根據(jù)計算所得坐標點,構建路徑,繪圖
super.onDraw(canvas); mBgPaint.setColor(Color.WHITE); canvas.drawPath(mBgPath, mBgPaint); mBgPaint.setColor(Color.GRAY); canvas.drawPath(mMaxPath, mBgPaint); canvas.drawPath(mPath, mPaint); mBgPaint.setColor(Color.WHITE); if(mDirection == HORIZONTAL){ canvas.drawCircle(mRadius + mPercent * mBarWidth, mRadius, mRadius, mBgPaint); canvas.drawCircle(mRadius + mPercent * mBarWidth, mRadius, mRadius - SPACING, mPaint); }else { canvas.drawCircle(mRadius, mHeight - (mRadius + mPercent * mBarWidth), mRadius, mBgPaint); canvas.drawCircle(mRadius, mHeight - (mRadius + mPercent * mBarWidth), mRadius - SPACING, mPaint); }
onTouchEvent
根據(jù)手指滑動,動態(tài)調整數(shù)值大小
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: float distance = 0; float maxDist = 0; switch (mDirection){ case HORIZONTAL: distance = event.getX(); maxDist = mWidth; break; case VERTICAL: distance = mHeight - event.getY(); maxDist = mHeight; break; } if(distance <= mRadius){ updateView(MIN_VALUE); }else if(distance >= maxDist - mRadius){ updateView(MAX_VALUE); }else { updateView(calculatingValue(distance)); } return true; default: return super.onTouchEvent(event); } }
實際效果如圖所示
橫向控制條
豎向控制條
項目github地址
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊2. python實現(xiàn)PolynomialFeatures多項式的方法3. HTML 絕對路徑與相對路徑概念詳細4. python 利用toapi庫自動生成api5. Spring如何使用xml創(chuàng)建bean對象6. Android Studio設置顏色拾色器工具Color Picker教程7. python實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼8. IntelliJ IDEA設置默認瀏覽器的方法9. Java程序的編碼規(guī)范(6)10. python實現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例
