gpt4 book ai didi

java - 在android中以圆周运动移动imageview

转载 作者:行者123 更新时间:2023-11-29 19:23:13 25 4
gpt4 key购买 nike

我想根据 x 轴和 y 轴触摸事件的值以圆周运动方式平移 View 。

我只使用一个轴就可以工作,如下面的 gif 所示。

enter image description here

网站 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;
}

最佳答案

你可以试试这个..

//@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;
}

关于java - 在android中以圆周运动移动imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41947448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com