项目要求需要在滑动圆弧的时候设置蓝牙针灸的强度,只能够在触碰圆弧的时候才能够滑动
主要原理是1、判断手指是否触碰到的是圆弧,在Event事件的能够在ACTION_MOVE 根据触摸点到圆中心的距离,
2、通过点的x和y轴到原点的坐标通过tan算出夹角
package com.example.customviewarctest.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.time.Month;
/**
* Created by 11028 on 4018/6/14.
*/
public class Arc extends View {
String XQLOG="XQLOG";
int progress=0;
float radius =0;
String mTitle="强度 0";
Paint mTextPaint;
Rect mTextBound;
int mTextSize=20;
double each =100/(double)180;
public Arc(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Arc(Context context) {
this(context, null);
}
public Arc(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// for(int i =0 ;i < attrs.getAttributeCount();i++){
// if("layout_height".equals(attrs.getAttributeName(i))){
// centerY=attrs.getAttributeValue(i);
// }else if("layout_width".equals(attrs.getAttributeName(i))){
// centerX=attrs.getAttributeValue(i);
// }
// }
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setStrokeWidth(4);
mTextBound = new Rect();
mTextPaint.setTextSize(mTextSize);
}
float centerX ;
float centerY ;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mTextPaint);
// 计算了描绘字体需要的范围
mTextPaint.getTextBounds(mTitle, 0, mTitle.length(), mTextBound);
canvas.drawText(mTitle, getWidth() / 2 - mTextBound.width() * 1.0f / 2, getHeight() / 2 , mTextPaint);
centerX= getWidth() / 2;
centerY= getHeight() / 2;
radius = (centerX - 40);// 半径
Paint paint =new Paint();
paint.setStrokeWidth(40);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setColor(getResources().getColor(android.R.color.holo_blue_dark));
paint.setStrokeCap(Paint.Cap.ROUND);
int centre = getWidth() / 2; // 获取圆心的x坐标
int radius = (centre - 40);// 半径
RectF rectF =new RectF(centre-radius,centre-radius,centre+radius,centre+radius);
canvas.drawArc(rectF,180,180,false,paint);
Paint paint1 =new Paint();
paint1.setStrokeWidth(40);
paint1.setStyle(Paint.Style.STROKE);
paint1.setAntiAlias(true);
paint1.setColor(getResources().getColor(android.R.color.black));
paint1.setStrokeCap(Paint.Cap.ROUND);
int centre1 = getWidth() / 2; // 获取圆心的x坐标
int radius1 = (centre1 - 40);// 半径
RectF rectF1 =new RectF(centre1-radius1,centre1-radius1,centre1+radius1,centre1+radius1);
canvas.drawArc(rectF1,180,progress,false,paint1);
//canvas.drawRect
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
float downX=0;
float downY=0;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
Log.i(XQLOG,"onTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(XQLOG,"onTouchEvent ACTION_MOVE");
float tempX= event.getX();
float tempY =event.getY();
Log.i(XQLOG,"tempY ="+tempY+"centerY="+centerY);
if(((tempY<centerY+20)&&tempX<centerX)||tempY<centerY){
//90度以前切且0度以下
if(((tempY<centerY+20)&¢erY<tempY)&&tempX<centerX){
progress=0;
Log.i(XQLOG,"progress ="+progress);
mTitle ="强度 0";
postInvalidate();
break;
}
float tempabsX= Math.abs(tempX);
float tempabsY= Math.abs(tempY);
double distance= Math.sqrt((centerX-tempabsX)*(centerX-tempabsX)+(centerY-tempabsY)*(centerY-tempabsY));
Log.i(XQLOG,"radius ="+radius+"distance ="+distance);
distance-=radius;
Log.i(XQLOG,"distance ="+distance);
if(distance<40&&distance>-10) {
//progress=40;
float y = Math.abs(centerY - tempabsY);
float x = Math.abs(centerX - tempabsX);
float k = y / x;
double angle = Math.atan(k);
angle = angle / 0.017;
if (tempX > centerX) {
angle = 180 - angle;
}
Log.i(XQLOG, "y=" + y + "x=" + x + "angle=" + angle);
progress = (int) angle;
if(angle>179){
progress=180;
}
Log.i(XQLOG,"progress ="+progress+"progress*(100/180)="+progress*(each));
mTitle ="强度 "+(Math.round(progress*(each)));
Log.i(XQLOG,"mTitle="+mTitle);
postInvalidate();
}
}
break;
case MotionEvent.ACTION_UP:
Log.i(XQLOG,"onTouchEvent ACTION_UP");
break;
}
return true;
}
}