2017-04-04 46 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