//todo 放效果图
使用方式:
//偷个懒用的像素 没用dp
<ScrollRuleView
android:layout_width="match_parent"
android:layout_height="200dp"
app:count="100"//配置最大刻度
app:fatLine="10"//粗线宽
app:thinLine="8"//细线宽
app:gap="30"//
app:length="80"
>
</com.xiaov.ruleview.ScrollRuleView>
实现思路:
三角形TriangleView和RuleView封装在一个ScrollRuleView中,供外部直接使用。
1.draw出来刻度线和三角形。
2.增加跟随手指移动
3.在move里回调value给上边显示当前刻度值。
//画刻度线
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
startX= (int)(mPaint.measureText("0")/2);//fix view destroy data exit
for (int i = 0; i < count+1; i++) {
if(i%10==0){
String text = String.valueOf(i);
mPaint.setStrokeWidth(fatLine);
canvas.drawLine(startX, 0, startX , 100, mPaint);//line height 100
canvas.drawText(text,startX- mPaint.measureText(text)/2,150, mPaint);//from y 150
}else{
mPaint.setStrokeWidth(thinLine);
canvas.drawLine(startX, 0, startX ,60, mPaint);//line height 60
}
startX = startX + gap;
}
}
//这里用的属性动画移动 layout方式回调后 移动失效
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = x;
break;
case MotionEvent.ACTION_MOVE:
float offsetX = x - lastX;
// layout(getLeft() + offsetX, getTop() ,getRight() + offsetX, getBottom());//用layout移动的方式 回调给上层View时 move失效
float translationX = getTranslationX() + offsetX;
//限制向右滑动 0
if(translationX>= mOffsetRight && mOffsetRight !=0 ){
translationX= mOffsetRight;
}
//限制向左滑动 100
if(Math.abs(translationX)>=getMeasuredWidth()- mOffsetRight -mPaint.measureText(count+"")/2-mPaint.measureText("0")/2){
translationX=-getMeasuredWidth()+ mOffsetRight +mPaint.measureText(count+"")/2+mPaint.measureText("0")/2;
}
setTranslationX(translationX);
if(mListener!=null){
mListener.onMoveChange(getTranslationX());
}
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
//画三角形
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
mPath.moveTo(getMeasuredWidth()/2-length/2,0);
mPath.lineTo(getMeasuredWidth()/2+length/2,0);
mPath.lineTo(getMeasuredWidth()/2,length);
mPath.close();
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(mPath, mPaint);
}