2014-02-05 36 views
0

我尝试使用ObjectAnimator为LinearLayout内的按钮设置动画效果,当我点击布局时什么也没有发生。如何使用ObjectAnimator为LinearLayout内的按钮设置动画效果

但是,如果我更改LinearLayout中的ImageViews的按钮动画开始没有问题。

继承人是XML:

<LinearLayout   
    android:id="@+id/images" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/img1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1.0"   
     android:src="@drawable/image1" 
     android:visibility="visible" 
     /> 

    <ImageView 
     android:id="@+id/img2" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1.0" 
     android:src="@drawable/image2" 
     android:visibility="gone" /> 

</LinearLayout> 

,这是代码:

final LinearLayout images = (LinearLayout) findViewById(R.id.images); 
    images.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) {    
      final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(images, "rotationY", -90f, 0f); 
      goneToVisible.setDuration(1000); 
      goneToVisible.start(); 
     } 
    }); 

寄托都工作正常,但是当我通过按钮来更改的ImageView:

<LinearLayout   
    android:id="@+id/images" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/img1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1.0"   
     android:background="@drawable/image1" 
     android:visibility="visible" 
     />   
    <Button 
     android:id="@+id/img2" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1.0" 
     android:background="@drawable/image2" 
     android:visibility="gone" />   
</LinearLayout> 

final LinearLayout images = (LinearLayout) findViewById(R.id.images); 
    images.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //flip(image1, image2, animLength.getProgress()); 
      final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(images, "rotationY", -90f, 0f); 
      goneToVisible.setDuration(1000); 
      goneToVisible.start(); 
     } 
    }); 

动画不能正常工作。

如果从LinearLayout中取出按钮,它可以工作,但不在里面。我需要使用Buttons代替ImageView。

我该如何解决这个问题?

谢谢。

回答

0

与您ImageView工作的第一个代码,因为ImageView默认情况下不点击,所以当你在LinearLayou单击事件将为LinearLayout和布局click事件的动画响应点击将工作

第二个代码与Button不适合你,因为Button可点击,所以当你点击布局和button已经是你的布局的高度,所以当点击布局点击事件将响应Button而不是布局,你不'没有收听Button点击事件没有动画发生,所以你可以放这在你的XML布局

android:clickable="false" 

这将使事件布局直接回复,或使动画上Button本身一样,

final Button btn = (Button) findViewById(R.id.img1); 
     final LinearLayout images = (LinearLayout) findViewById(R.id.images); 
     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //flip(image1, image2, animLength.getProgress()); 
       final ObjectAnimator goneToVisible = ObjectAnimator.ofFloat(btn, "rotationY", -90f, 0f); 
       goneToVisible.setDuration(1000); 
       goneToVisible.start(); 
      } 
     }); 

喂我回来

+0

这将是更有益OP和未来的访问者来描述你所做的改变,以及*他们为什么工作,而不是仅仅提供代码。 – codeMagic

+0

嗨。如果你点击按钮,它的工作正常,但如果你点击容器,它不适用于内部的按钮,但它有效,如果你有ImageVies而不是按钮。最终的LinearLayout图像=(LinearLayout)findViewById(R.id.images); images.setOnClickListener(new View.OnClickListener(){。我想单击容器以启动按钮的动画。谢谢 –

+0

@codeMagic感谢您的评论我将更详细地解释此问题 –

相关问题