Android自定義控制條效果
本文實例為大家分享了Android自定義控制條效果的具體代碼,供大家參考,具體內(nèi)容如下
ControlBar
自定義一個可以調(diào)節(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)調(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);
}
}
實際效果如圖所示
橫向控制條

豎向控制條

項目github地址
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android WebView無法彈出軟鍵盤的原因及解決辦法
這篇文章主要介紹了Android WebView無法彈出軟鍵盤的原因及解決辦法的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
Android App端與PHP Web端的簡單數(shù)據(jù)交互實現(xiàn)示例
本篇文章主要介紹了Android App端與PHP Web端的簡單數(shù)據(jù)交互實現(xiàn)示例,詳細的介紹了交互的代碼,非常具有實用價值,有興趣的可以了解一下2017-10-10
Android 點擊ImageButton時有“按下”的效果的實現(xiàn)
這篇文章主要介紹了 Android 點擊ImageButton時有“按下”的效果的實現(xiàn)的相關資料,需要的朋友可以參考下2017-03-03

