本文介绍了Android的 - 移动在圈子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有东西的麻烦,
我需要一个对象(傍桨)移动只在沿屏幕循环线。
因为如果你将有一个恒定的Y轴值,它只会沿着X轴移动,如u乌尔拖动手指放在上面,而是将其限制在一个圆形的路线一样。

I'm having trouble with something,I need to make an object (Pong paddle) to move only in a circular route along the screen.same thing as if you would have a constant y axis value and it would only move along the x axis as u drag ur finger on it, but to restrict it to a circular route.

任何见解?
我看到这个东西

和它只有在搞清楚如何不断在圈内移动的东西可以帮助(尽管它已经是闪存,这个想法是一样的)

and it only helps in figuring out how to move something constantly at a circle (eventhough it's Flash, the idea is the same)

感谢

推荐答案

下面是code块我用一个手指来旋转ImageView的。

Here is a block of code I use to rotate an imageview by one finger.

private float mCenterX, mCenterY;
private float direction = 0;
private float sX, sY;
private float startDirection=0;
private void touchStart(float x, float y) {
    mCenterX = this.getWidth() / 2;
    mCenterY = this.getHeight() / 2;
    sX = x;
    sY = y;
}

private void touchMove(float x, float y) {
    // this calculate the angle the image rotate
    float angle = (float) angleBetween2Lines(mCenterX, mCenterY, sX, sY, x,
            y);
    direction = (float) Math.toDegrees(angle) * -1 + startDirection;
    this.invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.rotate(direction, mCenterX, mCenterY);
    super.onDraw(canvas);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
         // record the start position of finger
        touchStart(x, y);
        break;
    case MotionEvent.ACTION_MOVE:
         // update image angle
        touchMove(x, y);
        break;
    case MotionEvent.ACTION_UP:
        startDirection = direction;
        break;
    }

    return true;
}

public double angleBetween2Lines(float centerX, float centerY, float x1,
        float y1, float x2, float y2) {
    double angle1 = Math.atan2(y1 - centerY, x1 - centerX);
    double angle2 = Math.atan2(y2 - centerY, x2 - centerX);
    return angle1 - angle2;
}

希望这有助于

编辑:基本上,上述code的作用是使用户能旋转的图像,并且该图像的中心不会改变。 angleBetween2Line()是计算的程度手指了一圈,其中心是图像的中心已经转移。

Basically, what the above code does is enable user to rotate an image, and the center of the image will not change. angleBetween2Line() is to calculate the degree that finger has moved in a circle whose center is the image's center.

这篇关于Android的 - 移动在圈子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 19:59