2017-04-04 42 views
0

我试图使用TranslateAnimation在点击项目时将个人档案图片移动到顶部。使用TranslateAnimation添加项目(来自ListView)不会取出他的父项

我找到了正确的位置(x,y),但个人资料图片没有出自他的项目区域。

我试图使用bringToFront()方法,但它不起作用。

enter image description here

这里我的问题

enter image description here

代码以动画我看来

public void animateContactAdding(View view) { 
     View profileImage = view.findViewById(R.id.linear_layout_image_profile); 

     int positionViewContact[] = {0, 0}; 
     profileImage.getLocationOnScreen(positionViewContact); 

     int positionHeader[] = {0, 0}; 
     mRelativeLayoutContactHeader.getLocationOnScreen(positionHeader); 

     float distanceBtwViews = (positionViewContact[1] - positionHeader[1]) * -1; 

     TranslateAnimation anim = new TranslateAnimation(0, 0, 0, distanceBtwViews); 
     anim.setDuration(400); 

     anim.setAnimationListener(new TranslateAnimation.AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
      } 
     }); 

     profileImage.startAnimation(anim); 
    } 

项目

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight"> 

    <LinearLayout 
     android:id="@+id/linear_layout_image_profile" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/circle_image_placeholder" 
      android:layout_width="?android:attr/listPreferredItemHeight" 
      android:layout_height="?android:attr/listPreferredItemHeight" 
      android:padding="10dp" /> 

     <ImageView 
      android:id="@+id/circle_image_profile" 
      android:layout_width="?android:attr/listPreferredItemHeight" 
      android:layout_height="?android:attr/listPreferredItemHeight" 
      android:padding="10dp" /> 

    </LinearLayout> 

    <TextView 
     android:id="@android:id/text2" 
     android:layout_width="match_parent" 
     android:layout_height="26dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_toEndOf="@+id/linear_layout_image_profile" 
     android:ellipsize="marquee" 
     android:lines="1" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@android:id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@android:id/text2" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:layout_toEndOf="@+id/linear_layout_image_profile" 
     android:ellipsize="marquee" 
     android:gravity="center_vertical" 
     android:lines="1" 
     android:textAppearance="@style/TextAppearance.RalewaySemiBold" 
     android:textSize="17sp" /> 

</RelativeLayout> 

回答

0

可能您正在为整个布局设置动画。尝试应用动画只在ImageView,看看会发生什么:

public void animateContactAdding(View view) { 
     int positionViewContact[] = {0, 0}; 
     profileImage.getLocationOnScreen(positionViewContact); 

     int positionHeader[] = {0, 0}; 
     mRelativeLayoutContactHeader.getLocationOnScreen(positionHeader); 

     float distanceBtwViews = (positionViewContact[1] - positionHeader[1]) * -1; 

     TranslateAnimation anim = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0.0F, Animation.RELATIVE_TO_SELF, 0.0F, 
      Animation.RELATIVE_TO_SELF, 2.0F, Animation.RELATIVE_TO_SELF, 0.0F 
    ); 
     anim.setDuration(2000); 
     anim.setFillAfter(true); 
     anim.setAnimationListener(new TranslateAnimation.AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
      } 
     }); 

     view.startAnimation(anim); 
    } 
+0

我的代码已经这样做了。我只是动画没有文字的个人资料图片。 – Johnny

+0

你在布局中应用动画吗?看看这一行:profileImage = view.findViewById(R.id.linear_layout_image_profile); –

+0

哦,好的。是的,我的项目包含一个'LinearLayout',它包含两个'ImageView'。我直接在'ImageView'上尝试,看起来完全一样: -/ – Johnny