Java 在android中以圆周运动移动imageview

Java 在android中以圆周运动移动imageview,java,android,Java,Android,我想基于x轴和y轴触摸事件的值以圆周运动平移视图 我只使用了一个轴,如下面的gif所示 网站trumpdonald.org有我想要的确切的运动。 您可以在屏幕周围自由移动鼠标,喇叭跟随鼠标,但它保持在圆形路径中 public boolean onTouch(View v, MotionEvent event) { rawScreenX = event.getRawX(); rawScreenY = event.getRawY();

我想基于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;
        }
你可以试试这个

//@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功能以检测动作\u移动

@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;
    }
你可以试试这个

//@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功能以检测动作\u移动

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

imageView.animate().rotation(180).start()?我不想旋转图像,我希望它遵循一个循环路径ImageView.animate().rotation(180.start()?我不想旋转图像,我希望它沿着圆形路径移动谢谢,但我需要视图沿着我的手指在屏幕上移动,但要沿着圆形路径移动。就像我在问题中链接到的网站:trumpdonald.org我无法在动画中使用你的onTouch功能。播放动画时,我必须单击图像。但当我点击图片时,onTouch不起作用。你能帮我吗?谢谢,但我需要视图沿着我的手指在屏幕上移动,但要以圆形路径移动。就像我在问题中链接到的网站:trumpdonald.org我无法在动画中使用你的onTouch功能。播放动画时,我必须单击图像。但当我点击图片时,onTouch不起作用。你能帮助我吗?