如何确定旋转图像的最终位置或角度

5
我有一个正在Fling旋转的轮子的ImageView。当旋转完成时,如何检测轮子的最终位置呢?基本上,类似于幸运转盘的轮子,结果取决于轮子停止的位置。
有没有一种方法可以检测Fling/旋转何时完成,然后获取最终角度?我想将该角度与圆形的4个象限之一相关联,并设置该结果。谢谢。下面是我的一些代码 //////////////////////////Gesture Detect /////////////////
private class MyWheelOnTouchListener implements OnTouchListener {

 private double startAngle;

 @Override
 public boolean onTouch(View v, MotionEvent event) {

     switch (event.getAction()) {

         case MotionEvent.ACTION_DOWN:
             rotateAnim(); // test
            // reset the touched quadrants
             for (int i = 0; i < quadrantTouched.length; i++) {
                 quadrantTouched[i] = false;
             }

             allowRotating = false;

             startAngle = getAngle(event.getX(), event.getY());

             break;

         case MotionEvent.ACTION_MOVE:
             double currentAngle = getAngle(event.getX(), event.getY());
            rotateDialer((float) (startAngle - currentAngle));
             startAngle = currentAngle;
             break;

         case MotionEvent.ACTION_UP:
             allowRotating = true;
             break;
     }
     // set the touched quadrant to true
     quadrantTouched[getQuadrant(event.getX() - (wheelWidth / 2), wheelHeight - event.getY() - (wheelHeight / 2))] = true;

     wheeldetector.onTouchEvent(event);

     return true;
 }
}

/**
* Simple implementation of a {@link SimpleOnGestureListener} for detecting a fling event.
*/
private class MyWheelGestureDetector extends SimpleOnGestureListener {
    private double endAngle;
 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     // get the quadrant of the start and the end of the fling
     int q1 = getQuadrant(e1.getX() - (wheelWidth / 2), wheelHeight - e1.getY() - (wheelHeight / 2));
     int q2 = getQuadrant(e2.getX() - (wheelWidth / 2), wheelHeight - e2.getY() - (wheelHeight / 2));

     // the inversed rotations
     if ((q1 == 2 && q2 == 2 && Math.abs(velocityX) < Math.abs(velocityY))
             || (q1 == 3 && q2 == 3)
             || (q1 == 1 && q2 == 3)
             || (q1 == 4 && q2 == 4 && Math.abs(velocityX) > Math.abs(velocityY))
             || ((q1 == 2 && q2 == 3) || (q1 == 3 && q2 == 2))
             || ((q1 == 3 && q2 == 4) || (q1 == 4 && q2 == 3))
             || (q1 == 2 && q2 == 4 && quadrantTouched[3])
             || (q1 == 4 && q2 == 2 && quadrantTouched[3])) {

         wheel.post(new FlingWheelRunnable(-1 * (velocityX + velocityY)));
     } else {
         // the normal rotation
         wheel.post(new FlingWheelRunnable(velocityX + velocityY));
     }
     endAngle = getAngle(e1.getX(), e2.getY());


     return true;
 }
}

/**
* A {@link Runnable} for animating the the dialer's fling.
*/
private class FlingWheelRunnable implements Runnable {

 private float velocity;

 public FlingWheelRunnable(float velocity) {
     this.velocity = velocity;
 }

 @Override
 public void run() {
     if (Math.abs(velocity) > 5) { // original = 5
         rotateDialer(velocity / 100); // original = 75
         velocity /= 1.0666F; // original = 1.0666F

        wheel.getRotation()); <-- maybe something like this, but not working??
         // post this instance again
         wheel.post(this);
     }

 }
}

/**
 * @return The angle of the unit circle with the image view's center
 */
private double getAngle(double xTouch, double yTouch) {
    double x = xTouch - (wheelWidth / 2d);
    double y = wheelHeight - yTouch - (wheelHeight / 2d);

    switch (getQuadrant(x, y)) {
        case 1:
            return Math.atan(y / x) * 180 / Math.PI;
        case 2:
            return 180 - Math.atan(y / x) * 180 / Math.PI;
        case 3:
            return 180 + (-1 * Math.atan(y / (x)) * 180 / Math.PI);
        case 4:
            return 360 + Math.atan(y / (x)) * 180 / Math.PI;
        default:
            return 0;
    }
}

/**
 * @return The selected quadrant.
 */
private static int getQuadrant(double x, double y) {
    if (x >= 0) {
        return y >= 0 ? 1 : 4;
    } else {
        return y >= 0 ? 2 : 3;
    }
}


/**
 * Rotate the wheel.
 *
 * @param degrees The degrees, the dialer should get rotated.
 */
private void rotateDialer(float degrees) {
    matrix.postRotate(degrees, wheelWidth / 2, wheelHeight / 2);
    wheel.setImageMatrix(matrix);




}

我也有这个问题。你找到解决方法了吗? - fnkbz
嗨,最后我用一种更简单的方法解决了。我只是使用范围为0至360的Math.random()函数添加了一个随机角度,然后再加上大约1800(5次动画旋转),接着使用switch语句根据生成的随机角度来决定结果。 - huskyd97
3个回答

1
我不确定这是否与您当前的实现兼容,但您是否尝试检测最终轮的颜色?您只需要使用BitmapFactory来加载它:
Bitmap slice = BitmapFactory.decodeFile(); 
// just go through the documentation 
// to find the easiest, for you, way to load it

那只需要拿出一个像素:
int slicePixel = slice.getPixel(0, 0);

最后检测颜色:

int red = Color.red(slicePixel);
int blue = Color.blue(slicePixel);
int green = Color.green(slicePixel);

希望这对你有所帮助。

1
你好,这是一个有趣的方法。我甚至没有意识到你可以用这种方式来检测颜色!请参考上面的注释以查看我的最终实现。感谢你的回复。 - huskyd97
这是一种非常有趣的做法,做得很好!确保将其发布为答案并接受它。 :) - Sipty

0

我实际上将该函数放在了我的rotateDialer函数中。我使用了以下代码:

    float[] vert = new float[9];
    matrix.getValues(vert) ;
    double bleh = Math.round(Math.atan2(vert[matrix.MSKEW_X], vert[matrix.MSCALE_X]) * (180 / Math.PI));

但是后来我必须进行其他算术运算,因为角度不匹配我链接到的数据的角度,但如果您只是将其记录到控制台中,您应该能够将其与自己的匹配。

来源于这里


0

我最终采用了一种更简单的方法。我只是使用了 Math.random 函数添加了一个随机角度,范围为 0-360 度。然后再加上大约 1800 度(5 次动画旋转)。接着,我使用了 switch 语句来根据生成的随机角度决定结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接