2016-10-28 60 views
11

我添加圖片動態查看我的線性佈局,查看動畫在Android的摺疊/展開視圖中的LinearLayout

我想實現

enter image description here

下面是示例代碼,我做了什麼

MainActivity

package ksp.com.animationssample; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

public class MainActivity extends AppCompatActivity { 

    private Button btn_expand; 
    private Button btn_collapse; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn_expand = (Button) findViewById(R.id.btn_expand); 
     btn_collapse = (Button) findViewById(R.id.btn_collapse); 
     LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     final LinearLayout layout = (LinearLayout) findViewById(R.id.imageLayout); 


     for (int i = 0; i < 3; i++) { 
      final ImageView image = new ImageView(MainActivity.this); 
      image.setLayoutParams(vp); 
      if (i == 0) 
       image.setImageDrawable(getResources().getDrawable(R.drawable.item_image1)); 
      else image.setImageDrawable(getResources().getDrawable(R.drawable.item_image2)); 
      if (i == 2) 
       image.setVisibility(View.VISIBLE); 
      else 
       image.setVisibility(View.GONE); 
      layout.addView(image); 
     } 


     btn_expand.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       expandOrCollapse(layout.getChildAt(2), true, layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight()); 
       expandOrCollapse(layout.getChildAt(1), true, layout.getChildAt(0).getHeight()); 
       expandOrCollapse(layout.getChildAt(0), true, 0); 


      } 
     }); 
     btn_collapse.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       expandOrCollapse(layout.getChildAt(0), false, 0); 
       expandOrCollapse(layout.getChildAt(1), false, layout.getChildAt(0).getHeight()); 
       expandOrCollapse(layout.getChildAt(2), false, layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight()); 
      } 
     }); 


    } 


    public void expandOrCollapse(final View v, boolean is_expand, final int animheight) { 
     TranslateAnimation anim = null; 
     if (is_expand) { 
      anim = new TranslateAnimation(0.0f, 0.0f, -animheight, 0.0f); 
      v.setVisibility(View.VISIBLE); 
      Animation.AnimationListener expandedlistener = new Animation.AnimationListener() { 
       @Override 
       public void onAnimationStart(Animation animation) { 
       } 

       @Override 
       public void onAnimationRepeat(Animation animation) { 
       } 

       @Override 
       public void onAnimationEnd(Animation animation) { 


       } 
      }; 

      anim.setAnimationListener(expandedlistener); 
     } else { 
      anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -animheight); 
      Animation.AnimationListener collapselistener = new Animation.AnimationListener() { 
       @Override 
       public void onAnimationStart(Animation animation) { 
       } 

       @Override 
       public void onAnimationRepeat(Animation animation) { 
       } 

       @Override 
       public void onAnimationEnd(Animation animation) { 
        v.setVisibility(View.GONE); 
       } 
      }; 

      anim.setAnimationListener(collapselistener); 
     } 

     anim.setDuration(1500); 
     anim.setInterpolator(new AccelerateInterpolator(0.5f)); 
     v.startAnimation(anim); 
    } 
} 

activity_mainn.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:background="@android:color/holo_blue_dark" 
    tools:context="ksp.com.animationssample.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/btn_expand" 
     android:text="Expand" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btn_collapse" 
     android:text="Collopse"/> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/imageLayout" 
     android:gravity="center" 

     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 
    <!--android:animateLayoutChanges="true"--> 
</ScrollView> 


</RelativeLayout> 

當我點擊在第一時間展開按鈕沒有顯示從它的工作,第二次的動畫。

啓用可見項後點擊崩潰按鈕。

下一步做什麼: 當我選擇它來顯示選擇動畫,然後合攏動畫播放,倒塌後它就像我在圖片上面提到顯示爲頂視圖中的任何視圖項目。目前我硬編碼的意見,併爲每個視圖靜態動畫編寫靜態動畫,即(expandOrCollapse(view, height_of_view)

我搜索了一段時間,但沒有運氣。

我遵循Vie expand/collapsesmooth expand/collapse,但他們不能幫助我擴展線性佈局中的所有視圖。

我們可以做這個列表視圖是回收視圖還是什麼?

爲節省時間我已經加入我的樣品git的樞紐,你可以嘗試一下Animation Sample Github

建議我請。

+0

只是爲了清楚這個問題 - 你想要底部的信用卡在摺疊後仍然可見?還是有其他問題? –

+0

假設當我點擊第二張牌時,它必須在崩潰後出現頂部 – Kathi

+1

CardStackView是一個很好的庫,考慮訪問一次,https://github.com/loopeer/CardStackView –

回答

1

儘量不要隱藏你的目標視圖(現在你設置可見走了倒塌的動畫結束後,所有的意見),並做到這一點:

targetView.bringToFront(); 
targetView.invalidate(); 
targetView.getParent().requestLayout(); 
+0

感謝'v.bringToFront()'方法我將更新我的回購與該更改,但我們如何處理,如果視圖是動態的,即從服務器計數。在這裏,我爲每個視圖單獨編寫了動畫,這不是正確的方式嗎?你能建議我更好的方式來處理這種類型的動畫多視圖?您也可以看到列表中應該有選擇視圖的選擇動畫。我們該如何處理?感謝您的幫助 – Kathi

+0

我想,您可以將動畫應用於for-loop。對於選擇動畫 - 你說什麼樣的動畫?在onClick回調中,我們獲得目標視圖,並可以應用任何類型的動畫,甚至可以在xml佈局中設置動畫。我想你沒有太多的項目要動畫,所以它足以使用FrameLayout作爲容器(而不是ListView) –

+0

@ kathi可以附上學習目的的代碼:)。真的我不能如何處理這個問題:( – udayatom

5

我已經單獨的類吧。檢查它是否適用於您:

public class DropDownAnim extends Animation { 
    private final int sourceHeight; 
    private final int targetHeight; 
    private final View view; 
    private final boolean down; 

    public DropDownAnim(View view, int sourceHeight, int targetHeight, boolean down) { 
     this.view = view; 
     this.sourceHeight = sourceHeight; 
     this.targetHeight = targetHeight; 
     this.down = down; 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newHeight; 
     if (down) { 
      newHeight = sourceHeight + (int) (targetHeight * interpolatedTime); 
     } else { 
      newHeight = sourceHeight + targetHeight - (int) (targetHeight * interpolatedTime);//(int) (targetHeight * (1 - interpolatedTime)); 
     } 
     view.getLayoutParams().height = newHeight; 
     view.requestLayout(); 
     view.setVisibility(down ? View.VISIBLE : View.GONE); 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
} 

雖然與此工作。爲了擴大

public void expand(final View v) { 
     final int sourceHeight = v.getLayoutParams().height; 
     final int targetHeight = getResources().getDimensionPixelSize(R.dimen.notification_height);//v.getMeasuredHeight(); 
     DropDownAnim a = new DropDownAnim(v,targetHeight,true); 
     a.setDuration((int) (targetHeight/v.getContext().getResources().getDisplayMetrics().density)); 
     a.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // Your code on end of animation 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     v.setVisibility(View.INVISIBLE); 
     v.startAnimation(a); 
    } 

對於崩潰:

public void collapse(final View v) { 
     final int sourceHeight = v.getLayoutParams().height; 
     final int targetHeight = v.getMeasuredHeight(); 
     DropDownAnim a = new DropDownAnim(v, targetHeight, false); 
     a.setDuration((int) (targetHeight/v.getContext().getResources().getDisplayMetrics().density)); 
     a.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // Your code on end of animation 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     v.startAnimation(a); 
    } 

更新:
sourceHeight加入防止視圖閃爍。

+0

感謝您的全面解決方法,我將這些方法應用於我像'expand(layout.getChildAt(2)); expand(layout.getChildAt(1)); expand(layout.getChildAt(0)); '似乎是它的作品。此代碼如何適用於動態添加的佈局?我們可以用循環嗎? – Kathi

+0

是的,你可以使用循環 –

+0

經過對你的代碼的研究之後,我把你的代碼放到了我的項目中,但是我的觀點只是閃爍而去。你能幫助更多的代碼如何工作。我在線性佈局中申請了每個視圖。另外它是如何知道動畫高度的? – Kathi