如何在android中旋转整个相对布局

时间:2011-09-09 11:20:28

标签: android rotation android-animation

我正在开发Android应用程序。当用户在布局中触摸并拖动图像时,我需要旋转整个布局。我正在使用相对布局。在这里我有一个图像视图和拖动按钮和一个图像作为相对布局的背景。现在我需要在拖动按钮移动时旋转整个相对布局。

尝试旋转动画时,在触摸事件未感应后立即运行。我添加了我的代码,如下所示

               anim = new RotateAnimation(0, -15f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f);

        anim.setInterpolator(new LinearInterpolator());

        anim.setDuration(700);
        anim.setFillAfter(true);

        anim.setAnimationListener(MainActivity.this);


         rotationctrl.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
        innerlayout.startAnimation(anim);
        }
        else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
                }

                return true;
            }
        });

这里的问题是什么。在哪里我犯了错误..请帮助我。

1 个答案:

答案 0 :(得分:1)

Android动画仅在向其添加动画时移动UI布局的像素。要在动画后实际更新布局,您应该实现一个动画侦听器并手动更新布局,否则旧布局仍将保留,只有像素移位。

因此,如果您不手动更新布局,则图像及其响应区域将处于旧位置,即使它不可见。