80,471
社区成员




@Override
public boolean onTouchEvent(MotionEvent event) {
isClicked = true;
this.xPosition = (int) event.getX();
this.yPosition = (int) event.getY();
double cR = Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
Log.e("onTouchEvent", "realRadius=" + realRadius + "\nmainRadius=" + mainRadius);
if (cR > realRadius) {
// double Yrate = (this.yPosition - this.centerY) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// double Xrate = (this.xPosition - this.centerX) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// this.yPosition = (int) (mainRadius * Yrate) + this.centerY;
// this.xPosition = (int) (mainRadius * Xrate) + this.centerX;
this.xPosition = (int) ((xPosition - centerX) * realRadius / cR) + this.centerX;
this.yPosition = (int) ((yPosition - centerY) * realRadius / cR) + this.centerY;
}
// 向量1的坐标:(x1,y1),向量2的坐标:(x2,y2); 则
double x1=0;
double y1=-realRadius;
double x2=xPosition-centerX;
double y2=yPosition-centerY;
double value = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)); // 余弦值
double angle = Math.toDegrees(Math.acos(value)); // 角度
//上面ange算的是夹角,所以是不会超过180度的,实际情况要区分处理下
if(x2<0){
angle=360-angle;
}
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
if (event.getAction() == 1) {
isClicked = false;
this.yPosition = this.centerY;
this.xPosition = this.centerX;
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
}
return true;
}
[/quote]
可以用,太感谢了,能最后再问一个问题吗?
我在这把原来的画出来的紫色的圆想改成图片,我这么写了,部分机型没问题,部分机型有些偏差(紫色的圆的中心点和外面那个圆的中心点不重合),我不知道是为什么
Rect mSrcRect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
//View 总宽度 width
//绘制需要总宽度 2*mainRadius+2*secondRadius
//所以 绘制背景色的时候需要往内至少margin secondRadius宽度
Rect mDestRect = new Rect(secondRadius, secondRadius, getWidth()- secondRadius, getHeight()- secondRadius);
canvas.drawBitmap(bm, mSrcRect, mDestRect, BackgroundPaint);
bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.joy_stick_handle)).getBitmap();
canvas.drawBitmap(bm, this.xPosition - secondRadius/2, this.yPosition - secondRadius/2, null);
[/quote]
而且这么写用来替换的图太小了,这个图的半径应该要是secondRadius,不知道该怎么设置,drawBitmap这个方式实在没看懂
@Override
public boolean onTouchEvent(MotionEvent event) {
isClicked = true;
this.xPosition = (int) event.getX();
this.yPosition = (int) event.getY();
double cR = Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
Log.e("onTouchEvent", "realRadius=" + realRadius + "\nmainRadius=" + mainRadius);
if (cR > realRadius) {
// double Yrate = (this.yPosition - this.centerY) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// double Xrate = (this.xPosition - this.centerX) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// this.yPosition = (int) (mainRadius * Yrate) + this.centerY;
// this.xPosition = (int) (mainRadius * Xrate) + this.centerX;
this.xPosition = (int) ((xPosition - centerX) * realRadius / cR) + this.centerX;
this.yPosition = (int) ((yPosition - centerY) * realRadius / cR) + this.centerY;
}
// 向量1的坐标:(x1,y1),向量2的坐标:(x2,y2); 则
double x1=0;
double y1=-realRadius;
double x2=xPosition-centerX;
double y2=yPosition-centerY;
double value = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)); // 余弦值
double angle = Math.toDegrees(Math.acos(value)); // 角度
//上面ange算的是夹角,所以是不会超过180度的,实际情况要区分处理下
if(x2<0){
angle=360-angle;
}
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
if (event.getAction() == 1) {
isClicked = false;
this.yPosition = this.centerY;
this.xPosition = this.centerX;
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
}
return true;
}
[/quote]
可以用,太感谢了,能最后再问一个问题吗?
我在这把原来的画出来的紫色的圆想改成图片,我这么写了,部分机型没问题,部分机型有些偏差(紫色的圆的中心点和外面那个圆的中心点不重合),我不知道是为什么
Rect mSrcRect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
//View 总宽度 width
//绘制需要总宽度 2*mainRadius+2*secondRadius
//所以 绘制背景色的时候需要往内至少margin secondRadius宽度
Rect mDestRect = new Rect(secondRadius, secondRadius, getWidth()- secondRadius, getHeight()- secondRadius);
canvas.drawBitmap(bm, mSrcRect, mDestRect, BackgroundPaint);
bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.joy_stick_handle)).getBitmap();
canvas.drawBitmap(bm, this.xPosition - secondRadius/2, this.yPosition - secondRadius/2, null);
canvas.drawCircle(this.xPosition, this.yPosition, secondRadius, circlePaint);
这个是他之前绘制圆的情况,不知道在这种情况有没有出现圆小了和这个圆的中心没有与背景圆的中心重合的问题,如果没有的话,那么继续下面的。
将上面的圆替换成你需要的图片
Bitmap yourBm = ((BitmapDrawable) getResources().getDrawable(R.mipmap.test)).getBitmap();
Rect secondRect= new Rect(this.xPosition-secondRadius,this.yPosition-secondRadius,this.xPosition+secondRadius,this.yPosition+secondRadius);
canvas.drawBitmap(yourBm, null,secondRect,circlePaint);
如果是在demo的圆的那个地方就有问题了,那就要具体在看了,后面有问题的话你加我qq或者微信吧,631298455
@Override
public boolean onTouchEvent(MotionEvent event) {
isClicked = true;
this.xPosition = (int) event.getX();
this.yPosition = (int) event.getY();
double cR = Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
Log.e("onTouchEvent", "realRadius=" + realRadius + "\nmainRadius=" + mainRadius);
if (cR > realRadius) {
// double Yrate = (this.yPosition - this.centerY) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// double Xrate = (this.xPosition - this.centerX) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// this.yPosition = (int) (mainRadius * Yrate) + this.centerY;
// this.xPosition = (int) (mainRadius * Xrate) + this.centerX;
this.xPosition = (int) ((xPosition - centerX) * realRadius / cR) + this.centerX;
this.yPosition = (int) ((yPosition - centerY) * realRadius / cR) + this.centerY;
}
// 向量1的坐标:(x1,y1),向量2的坐标:(x2,y2); 则
double x1=0;
double y1=-realRadius;
double x2=xPosition-centerX;
double y2=yPosition-centerY;
double value = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)); // 余弦值
double angle = Math.toDegrees(Math.acos(value)); // 角度
//上面ange算的是夹角,所以是不会超过180度的,实际情况要区分处理下
if(x2<0){
angle=360-angle;
}
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
if (event.getAction() == 1) {
isClicked = false;
this.yPosition = this.centerY;
this.xPosition = this.centerX;
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition,angle);
}
invalidate();
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED ? 100 : MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED ? 100 : MeasureSpec.getSize(heightMeasureSpec);
if (width > height) {
width = height;
} else {
height = width;
}
this.mainRadius = (width - 100) / 2;
this.secondRadius = mainRadius / 5 * 2;
setMeasuredDimension(width, height);
this.centerX = width / 2;
this.centerY = height / 2;
this.xPosition = centerX;
this.yPosition = centerY;
//用于记录实际真正的背景圆的半径,实际上就是 mDestRect.width()/2
this.realRadius = width / 2 - secondRadius;
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bm;
Paint BackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
BackgroundPaint.setFilterBitmap(true);
BackgroundPaint.setDither(true);
Paint circlePaint = new Paint();
circlePaint.setColor(Color.parseColor("#d81e06"));
circlePaint.setStyle(Paint.Style.FILL);
if (!isClicked) {
bm = ((BitmapDrawable) getResources().getDrawable(R.mipmap.circle1)).getBitmap();
} else {
bm = ((BitmapDrawable) getResources().getDrawable(R.mipmap.circle)).getBitmap();
}
Rect mSrcRect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
//View 总宽度 width
//绘制需要总宽度 2*mainRadius+2*secondRadius
//所以 绘制背景色的时候需要往内至少margin secondRadius宽度
Rect mDestRect = new Rect(secondRadius, secondRadius, getWidth() - secondRadius, getHeight() - secondRadius);
canvas.drawBitmap(bm, mSrcRect, mDestRect, BackgroundPaint);
canvas.drawCircle(this.xPosition, this.yPosition, secondRadius, circlePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
isClicked = true;
this.xPosition = (int) event.getX();
this.yPosition = (int) event.getY();
double cR = Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
Log.e("onTouchEvent", "realRadius=" + realRadius + "\nmainRadius=" + mainRadius);
if (cR > realRadius) {
// double Yrate = (this.yPosition - this.centerY) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// double Xrate = (this.xPosition - this.centerX) / Math.sqrt((this.xPosition - this.centerX) * (this.xPosition - this.centerX) + (this.yPosition - this.centerY) * (this.yPosition - this.centerY));
// this.yPosition = (int) (mainRadius * Yrate) + this.centerY;
// this.xPosition = (int) (mainRadius * Xrate) + this.centerX;
this.xPosition = (int) ((xPosition - centerX) * realRadius / cR) + this.centerX;
this.yPosition = (int) ((yPosition - centerY) * realRadius / cR) + this.centerY;
}
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition);
}
invalidate();
if (event.getAction() == 1) {
isClicked = false;
this.yPosition = this.centerY;
this.xPosition = this.centerX;
if (this.myWheelMoveListener != null) {
this.myWheelMoveListener.onValueChanged(this.xPosition, this.yPosition);
}
invalidate();
}
return true;
}
onTouchEvent里面计算方法都一样的,关键是要判断的是否超过了realRadius,以及用realRadius换算,按博主的算法将mainRadius换成realRadius应该也是可以的。
你可以将代码复制进去运行下,不知道是不是你要的效果