停止图像在旋转360度后的旋转

7

我想让图片以中心点为轴心旋转一圈,但是我无法在期望的位置停止旋转,虽然我可以进行旋转,但我希望能够在 360'(1圈) 后停止旋转。

public class RotateRoundActivity extends Activity implements OnTouchListener
{

    private ImageView dialer;
    //private float y=0;
    private float x=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //  double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());

        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x=event.getX();
              //  y=event.getY();
                updateRotation(rotation);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       

        return true;
    }

旋转方法@

    private void updateRotation(double rot){
        float newRot=new Float(rot);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        Matrix matrix=new Matrix();
        matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
        Log.i("demo===>", "matrix==>" + matrix);
     //   Log.i("demo===", "y===>" + y);
        Log.i("demo===", "x===>" + x);

        if(x>250){
            Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            dialer.setImageBitmap(reDrawnBitmap);
        }
        else{
            Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            dialer.setImageBitmap(reDrawnBitmap);
        }
    }

}

您的建议是可欣赏的。


你想要旋转的方向是什么?顺时针还是逆时针? - Chintan Raghwani
无论是顺时针还是逆时针方向。 - Maulik
2个回答

3

您需要保存以前的 rot 值。并且在 updateRotation 方法中添加检查,如果 previousRot 在 360 度左侧,而 rot 在 360 度右侧,则表示我们已经旋转了一圈,并且需要停止旋转。

顺时针情况下的示例代码:

if (previousRot >= 300 && previousRot <= 360 && rot >= 0 && rot <= 60) {
    rot = 359.99; // or here can be 360'
}

对于逆时针情况,几乎是相同的,只是值被交换了。

if (previousRot >= 0 && previousRot <= 60 && rot >= 300 && rot <= 360) {
    rot = 0;
}

这段代码将停止旋转。从开始时,顺时针情况下previousRot应为0,逆时针情况下应为359.99。


另一种方法是添加一个变量来存储总共旋转的角度。从开始时,traveledAngle必须等于0。如果你是顺时针旋转,你必须增加rotpreviousRot之间的差值。当逆时针旋转时,要减去同样的值。

traveledAngle += rot - previousRot;

当“traveledAngle”大于360度时,需要停止顺时针旋转;当它小于0时,需要停止逆时针旋转。

我如何获得大于360度的角度?它将从1度增加到360度,超过360度后将变为1度。 - Maulik
没错!对于顺时针方向,previousRot 应该是 350-360°,而 rot 应该约为 0-10°。但这些数字仅供参考,实际情况可能是 300-360° 和 0-60°。主要思路是使用 previousRot 值。 - vasart
好的。但是如果我在旋转一次后处于1'位置,而我的previousRot值为360',那么我该如何停止旋转呢?另外,逆时针旋转的逻辑应该是什么? - Maulik
通过使用 traveledAngle,添加了另一种解决问题的方法。 - vasart
我喜欢你对traveledAngle和rot angle的概念和逻辑。在你的评论之后,我已经尝试解决停止旋转的问题两天了。现在我几乎找到了解决这个问题的方法。谢谢。 - Maulik
我还没有得到确切的解决方案,但是我非常接近了。因此,我接受了你的回答。 - Maulik

2
我使用了你的演示并添加了一些逻辑,更新后的演示如下:
public class RotateRoundActivity extends Activity implements OnTouchListener {
    float rot1=0.0F, rot2=0.0F;
    boolean clockwise, rotationDone = false, halfrotated = false;
    int rotcall=0;

    private ImageView dialer;
    //private float y=0;
    private int x=0;
    //private int y=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //  double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x=(int) event.getX();
                //y=(int) event.getY();
                updateRotation(rotation);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       

        return true;
    }

    private void updateRotation(double rot){
        float newRot = new Float(rot);

        rotcall++;
        if(rotcall == 1)
            rot1 = new Float(rot);
        if(rotcall == 2)
            rot2 = new Float(rot);
        if(rot1 != 0.0F && rot2 != 0.0F)
            if(rot1 < rot2)
                clockwise = true;
            else
                clockwise = false;
        System.out.println("Rotate :: "+newRot);

        if(clockwise && rot1>=0 ) {
            if(newRot < 0)
                halfrotated = true;
            if(halfrotated && newRot > 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(clockwise && rot1<0) {
            if(newRot > 0)
                halfrotated = true;
            if(halfrotated && newRot < 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(!clockwise && rot1<0) {
            if(newRot > 0)
                halfrotated = true;
            if(halfrotated && newRot < 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(!clockwise && rot1>=0) {
            if(newRot < 0)
                halfrotated = true;
            if(halfrotated && newRot > 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }

        System.out.println("Rotation Done :: "+rotationDone);

        if(!rotationDone) {
            //BitmapDrawable bitmapDrawable = (BitmapDrawable) dialer.getDrawable();
            //Bitmap bitmap = bitmapDrawable.getBitmap();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.  YOUR_DRBL  );
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postRotate(newRot, width, height);
            System.out.println("x===>" + x);
            //System.out.println("y===>" + y);

            //if (x > 250) {
                Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                dialer.setImageBitmap(reDrawnBitmap);
            /*} else {
                Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        width, height, matrix, true);
                dialer.setImageBitmap(reDrawnBitmap);
            }*/
        }
    }

}

有两个副作用。1.]从左半部分顺时针开始旋转OR 2.]从右半部分逆时针开始旋转。在这两种情况下,它会将图像旋转1.5次。这是我的逻辑目前的限制。如果我更新了它,我会通知你。 - Chintan Raghwani
@Maulik,如果你试用我的演示时遇到任何问题,请在此处评论通知我。 - Chintan Raghwani

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