Android自定義控制條效果
本文實(shí)例為大家分享了Android自定義控制條效果的具體代碼,供大家參考,具體內(nèi)容如下
ControlBar
自定義一個(gè)可以調(diào)節(jié)大小的控件,可以根據(jù)寬高來(lái)指定控制條方向。當(dāng)width >= heigth時(shí),為橫向控制條,否則為豎向控制條
onMeasure
根據(jù)用戶給定的width與height計(jì)算控制條的坐標(biāo)。
1.主要的計(jì)算思路
先計(jì)算橫向的的坐標(biāo)點(diǎn),豎向的坐標(biāo)點(diǎn)即橫向的逆時(shí)針旋轉(zhuǎn)90度再向下移一個(gè)heigth的長(zhǎng)度。
//橫向坐標(biāo)點(diǎn)mHorLArcFirstPathX = mRadius + mLArcLength;mHorLArcFirstPathY = startY + mBarHeight * (1.0f - LITTLE_ARC_PER_WIDTH) / 2.0f ;//對(duì)應(yīng)豎向坐標(biāo)點(diǎn)mLArcFirstPathX = mHorLArcFirstPathY;mLArcFirstPathY = -mHorLArcFirstPathX + longSide;
onDraw
根據(jù)計(jì)算所得坐標(biāo)點(diǎn),構(gòu)建路徑,繪圖
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ù)手指滑動(dòng),動(dòng)態(tài)調(diào)整數(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); } }
實(shí)際效果如圖所示
橫向控制條
豎向控制條
項(xiàng)目github地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp知識(shí)整理筆記4(問(wèn)答模式)2. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享3. XML入門的常見(jiàn)問(wèn)題(二)4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題6. jsp文件下載功能實(shí)現(xiàn)代碼7. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案8. ASP實(shí)現(xiàn)加法驗(yàn)證碼9. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)10. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序
