2016-10-28 18 views
11

のビューを展開し、ビューアニメーション/私はイメージが私のリニアレイアウトに動的に表示追加のLinearLayout

私はここで

enter image description here

を達成したいサンプル・コードは、私が

を行っているものです

メインアクティビティ

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> 

私はが初めてで展開ボタンをクリックしは、それが働いている2回目以降のアニメーションが表示されていません。

enable コラプスボタンをクリックすると表示されます。次何をすべきか

: を、私はそれが、その後崩壊後、それは私が上記の画像では言及のようにトップビューとして表示されるように持って、アニメーションを折りたたむ選択したアニメーションを表示するために持っている任意の表示項目を選択しました。現在のところ、ビューの数をハードコードしており、アニメーションの静的な高さで各ビューの静的アニメーションを書いています。つまり、(expandOrCollapse(view, height_of_view)

これはしばらく検索しますが、運はありません。

私はVie expand/collapsesmooth expand/collapseに従っていますが、それらはリニアレイアウトですべてのビューを展開するのに役立ちません。

私たちはこれを行うことができますリストビューはリサイクラビューですか?

私はGitのハブへの私のサンプルを追加した時間節約のために、あなたはそれAnimation Sample Github

は、私にしてください提案してみてください。

+0

問題を明確にしてください。折りたたんだあと、下のクレジットカードを表示したままにしますか?それとも別の問題がありますか? –

+0

2番目のカードをクリックすると、崩壊後にトップに表示されます。 – Kathi

+1

CardStackViewは良いライブラリです。一度訪れてみてください。https://github.com/loopeer/CardStackView –

答えて

1

に試してみてください、あなたのターゲットビューを隠す(今あなたが折りたたまれたアニメーション終了後にすべてのビューのためGONE可視性を設定)し、これをしない:

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

'v.bringToFront()'メソッドをお寄せいただきありがとうございます。私はその変更でリポジトリを更新する予定ですが、ビューが動的である場合、つまりサーバーからのカウントを処理するにはどうすればいいですか。ここで私は別々に各ビューのアニメーションを書いたのですが、それは正しい方法ではありませんか?このタイプのアニメーションを複数のビューで処理するより良い方法を提案できますか?また、リスト内の選択ビューのアニメーションが選択されている必要があります。どうすればそれに対処できますか?あなたの助けを感謝します – Kathi

+0

私は、ループのアニメーションを適用することができますね。選択したアニメーションでは、どのようなアニメーションが表示されますか? 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)); 'その作品のようです。このコードは動的に追加されたレイアウトに対してどのように機能しますか? forループを使用できますか? – Kathi

+0

はいループを使用することができます –

+0

コードを調べた後、自分のコードをプロジェクトに配置しましたが、私の見解はちょうど点滅して消えてしまっています。このコードがどのように機能するかを少し助けてください。私は線形レイアウトのすべてのビューに適用しました。また、どのようにアニメーションの高さを知っていますか? – Kathi

関連する問題