【问题标题】:After rotating a linearlayout, the onclick events do not change position after the rotation旋转线性布局后,onclick 事件在旋转后不会改变位置
【发布时间】:2013-09-05 06:49:48
【问题描述】:

我有一个垂直的线性布局,在线性布局中有三个可点击的图像视图。当我使用简单的动画将线性布局旋转 90 度时,问题就出现了。图像视图已正确旋转,但图像视图的 onclick 事件不会与线性布局一起旋转,并且与动画之前一样保持在原始位置。

下面是我的主要java代码

       westplayer = (LinearLayout) findViewById(R.id.linearLayout_west);

        // Create an animation instance
    Animation an = new RotateAnimation(0.0f, 180.0f, 32, 180);

    // Set the animation's parameters
    an.setDuration(0);               // duration in ms
    an.setRepeatCount(0);                // -1 = infinite repeated
    an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation 

        // Apply animation to linearlayout
    westplayer.setAnimation(an); 

上面的代码处理动画部分。下面的代码跟随动画,并用于更新布局位置,但不适用于我。

        // Update Layout 
          int top=westplayer.getTop();
      int bottom=westplayer.getBottom();
      int left=westplayer.getLeft();
      int right=westplayer.getRight();
      westplayer.layout(left, top , right, bottom );

xml如下:

     <LinearLayout
    android:id="@+id/linearLayout_west"
    android:layout_width="42dp"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout_north"
    android:duplicateParentState="false"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageViewW1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"

         />

    <ImageView
        android:id="@+id/imageViewW2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"
        android:layout_marginTop="-15dp" 
        />

    <ImageView
        android:id="@+id/imageViewW3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"
        android:layout_marginTop="-15dp" 
       />

</LinearLayout>

我从this 获得了更新布局代码,还找到了另一个我也尝试过this 的解决方案,但仍然没有积极的结果。我需要它来为 API 10 工作。非常感谢任何帮助

【问题讨论】:

  • 这个图像视图是线性布局的子级吗?
  • 是的,它是线性布局的子,我已经添加了上面的xml代码。

标签: android android-layout rotation android-linearlayout android-view


【解决方案1】:

是的,您得到了预期的结果。

由于android中的动画只尝试在视图的位图上应用一个变换矩阵,它不会改变视图的位置和大小。

所以在你的动画之后,虽然你setFillAfter(true) 让视图看起来旋转了,但实际上视图仍然在同一个地方,一点也不移动。

【讨论】:

  • 感谢您的回复。我认为更新布局代码会在动画之后重新发布视图,但似乎没有这样做。那么如何让图像视图的视图也与位图一起旋转?有什么想法吗?
  • @user2738708 抱歉,我真的不知道是否有办法以旋转方式布局视图。
  • 感谢您尝试回答我的问题,如果其他人有解决方案,请帮助我。
【解决方案2】:

您面临的问题是因为您使用了补间动画。视图将旋转,只有可见部分会移动。但是 ImageView 的位置将保持与布局中定义的相同。这就是可点击区域保持不变的原因。

要重新定义位置,根据动画,您必须使用Property Animation,如ObjectAnimatorValueAnimator。这样,在 xml 中定义的属性将被更新。现在可点击区域随着视图移动。

【讨论】:

  • 是的,你说得对。属性动画可以解决我的问题,但由于我之前没有提到,我需要我为 api 10 工作,我认为 api 10 不支持属性动画。我的最低版本是姜饼 2.3。感谢您的帮助。
  • 我尝试了 google play 上提供的 Nineoldandroid 应用程序,它与您提供的链接中的代码具有相同的实现(我的假设)。该应用程序显示了各种动画测试,但我感兴趣的是 viewpropertyanimator 演示。演示中的按钮应该在动画之后可以点击。我在果冻豆设备上尝试了该应用程序,它可以工作,但不适用于姜饼设备。请确认我的信息并感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多