我想基于x轴和y轴触摸事件的值,以圆周运动转换视图。
我只使用一个轴工作,如下面的gif所示。
网站trumpdonald.org有我正在寻找的确切动作。
您可以在屏幕上自由移动鼠标,小号跟随鼠标,但它保持在圆形路径中。
public boolean onTouch(View v, MotionEvent event) {
rawScreenX = event.getRawX();
rawScreenY = event.getRawY();
int radius = 500;
//change the range from screenwidth/height into 2*PI
float x = changeRange(0, screenWidth, 0,2*(float)Math.PI, rawScreenX);
float y = changeRange(0, screenHeight, 0,2*(float)Math.PI, rawScreenY);
double yTranslate = radius * Math.sin(x);
double xTranslate = radius * Math.cos(x);
imageView.setTranslationY((float) yTranslate);
imageView.setTranslationX((float) xTranslate);
return true;
}
答案 0 :(得分:2)
你可以试试这个..
//@image - imageView, @float - radius
Animation anim = new CircularRotateAnimation(image, 500);
//duration of animation
anim.setDuration(3000);
//start the animation
image.startAnimation(anim);
创建自定义动画类
public class CircularRotateAnimation extends Animation {
private View view; // view you want to animate
private float cx, cy; // center x,y position of circular path
private float prevX, prevY; // previous x,y position of image during animation
private float r; // radius of circle
private float prevDx, prevDy;
/**
* @param view - View that will be animated
* @param r - radius of circular path
*/
public CircularRotateAnimation(View view, float r){
this.view = view;
this.r = r;
}
@Override
public boolean willChangeBounds() {
return true;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
// calculate position of image center
int cxImage = width / 2;
int cyImage = height / 2;
cx = view.getLeft() + cxImage;
cy = view.getTop() + cyImage;
// set previous position to center
prevX = cx;
prevY = cy;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 0){
t.getMatrix().setTranslate(prevDx, prevDy);
return;
}
float angleDeg = (interpolatedTime * 360f + 90) % 360;
float angleRad = (float) Math.toRadians(angleDeg);
// r = radius, cx and cy = center point, a = angle (radians)
float x = (float) (cx + r * Math.cos(angleRad));
float y = (float) (cy + r * Math.sin(angleRad));
float dx = prevX - x;
float dy = prevY - y;
prevX = x;
prevY = y;
prevDx = dx;
prevDy = dy;
//applying the circular animation
t.getMatrix().setTranslate(dx, dy);
}
}
覆盖onTouch功能以检测action_move
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
//your calculation;
rawScreenX = event.getRawX();
rawScreenY = event.getRawY();
//modify CirculateRotateAnimation function accroding to your needs
Animation anim = new CircularRotateAnimation(image, YOUR CALCULATED RADIUS);
anim.setDuration(YOUR TIMING);
image.startAnimation(anim);
break;
}
}
return true;
}