android - 图像旋转 360 度后停止

标签 android rotational-matrices

我正在尝试从它的中心点旋转图像单轮,但我无法在所需位置停止,因为我可以旋转但我想在 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);
        }
    }

}

您的建议很重要。

最佳答案

您必须保存以前的rot 值。并在 updateRotation 方法中添加检查,如果 previousRot 位于 360' 度的左侧并且 rot 位于 360' 度的右侧,那么我们制作了1轮需要停止旋转。

顺时针示例代码

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时需要停止逆时针旋转。

关于android - 图像旋转 360 度后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341135/

相关文章:

android - Gradle Crittercism内置映射上传不起作用

android - 消除音量信封重新触发点击 - Android 上的 Jsyn

android - Azure 身份验证错误

java - 在 Android 应用游戏上玩游戏时使用背景音乐

python - 如何在 matplotlib 图表上制作动画

android - WiFi 直接 : How to Connect and interact with peers programmatically without user intervention?

python - 来自两个 3D 点的欧拉角和旋转矩阵

ios - 如何围绕 Y 轴翻转 CALayer?

c++ - 使用 GLM(旋转)为 OpenGL 创建变换矩阵

computer-vision - 如何计算一台相机相对于第二台相机的外参数?