我已经尝试了很多方法来做到这一点.....我能够将分针移动到触摸视图上,但问题是即使我不触摸它也能移动。
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
//find an approximate angle between them.
Log.i("test", "value x is"+x);
Log.i("test", "value y is"+y);
/*float dx = x-cx;
float dy = y-cy;*/
getRotationAngle(x,y);
float dx = x-cx;
float dy = y-cy;
double a=Math.atan2(dy,dx);
Log.i("test",":: "+Math.toDegrees(a));
this.degree = Math.toDegrees(a);
this.invalidate();
}
return true;
} protected void onDraw(Canvas canvas) {
super .onDraw(canvas);
boolean changed = true;
int availableWidth = getRight() - getLeft();
int availableHeight = getBottom() - getTop();
int x = availableWidth / 2;
int y = availableHeight / 2;
cx = x;
cy = y;
final Drawable dial = mDial;
int w = dial.getIntrinsicWidth() + 100;
int h = dial.getIntrinsicHeight() + 100;
boolean scaled = false;
if (availableWidth < w || availableHeight < h) {
scaled = true;
float scale = Math.min((float) availableWidth / (float) w, (float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}
if (changed)
{
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
dial.draw(canvas);
canvas.save();
float hour = 4 / 12.0f * 360.0f;
canvas.rotate((float) degree, x, y);
final Drawable hourHand = HourHand;
if (changed) {
w = hourHand.getIntrinsicWidth() + 30;
h = hourHand.getIntrinsicHeight() + 30;
hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
hourHand.draw(canvas);
canvas.restore();
canvas.save();
float minute = Minutes / 60.0f * 360.0f;
/*if (bearing == 0)
{
canvas.rotate(minute, x, y);
}
else
{
canvas.rotate((float)bearing, x, y);
}*/
final Drawable minuteHand = MinuteHand;
if (changed) {
w = minuteHand.getIntrinsicWidth() + 30;
h = minuteHand.getIntrinsicHeight() + 30;
minuteHand.setBounds(x - w, y - h, x + w, y + h);
}
minuteHand.draw(canvas);
canvas.restore();
if (scaled) {
canvas.restore();
}
}
当我触摸视图中的任何地方时,我的钟表开始旋转,而不应该。有帮助吗?