2017-10-06 8 views
1

最初は高さが0の親ボトムに揃えられたビューが与えられ、アニメーションを適用して高さがWRAP_CONTENTになるようにします。私はこれを達成するために、レイアウトの移行を使用しています:LayoutTransitionの高さが0になると、アニメーションなしでビューが直ちに消えてしまいます

viewGroupviewToAnimateの親であると animateLayoutChange=true

を持っており、ロジックがある

viewGroup.layoutTransition.enableTransitionType(LayoutTransition.CHANGING) 

val params = viewToAnimate.layoutParams 

if (expand && params.height == 0) { 
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT 
    viewToAnimate.layoutParams = params 
} else if (collapse && params.height != 0) { 
    params.height = 0 
    viewToAnimate.layoutParams = params 
} 

ビューがあるとき、これは素晴らしい作品拡張された;ビューは底からその高さにうまく滑ります。しかし、高さが0に設定されている場合、ビューは単に消えて、スライドしません。viewToAnimateは、TextViewsの相対レイアウトで、何も複雑ではありません。任意の提案をいただければ幸いです。 https://www.dropbox.com/s/1pq7yh0bqx8ghif/2017_10_06_23_17_11.mp4?dl=0

ビューアニメーション化する:効果を示すビデオ(ただ消えて、テキストが滑り落ちない気づく)

<RelativeLayout> 

    ..some other stuff... 

    <RelativeLayout 
      android:id="@+id/viewToAnimate" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:gravity="center" 
      android:layout_alignParentBottom="true" 
      android:background="@color/white"> 

      <TextView 
       android:id="@+id/sampleText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sample Text" 
       android:textSize="50sp"/> 

    </RelativeLayout> 

</RelativeLayout> 
+0

「ConstraintLayout」または「RelativeLayout」と2つのボタン(表示/非表示)と下部の「TextView」のいずれかを使用してこれを再現することはできません。再現するのに必要なすべてのコードを投稿することができます(さらに、テストしているAPIレベルなどの情報)? –

+0

@ BenP。展開と折りたたみの両方のアニメーションがあなたのために起こりますか?私のレイアウトを投稿させてください – ono

+0

@BenPビデオとレイアウトを確認 – ono

答えて

1

を "wrap_content" としてviewToAnimateレイアウト高さを維持し、 textViewの高さは「0dp」です。下の例を参照してください。

<RelativeLayout 
    android:animateLayoutChanges="true" 
    android:id="@+id/viewToAnimate" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_alignParentBottom="true" 
    android:background="@android:color/white"> 

    <TextView 
     android:id="@+id/sampleText" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:text="Sample Text" 
     android:textSize="50sp"/> 

</RelativeLayout> 


final RelativeLayout viewToAnimate = (RelativeLayout) findViewById(R.id.viewToAnimate); 
    viewToAnimate.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); 

      TextView sampleTV = (TextView)findViewById(R.id.sampleText); 
      ViewGroup.LayoutParams params = sampleTV.getLayoutParams(); 
      if (params.height == 0) { 
       params.height = ViewGroup.LayoutParams.WRAP_CONTENT; 
      }else { 
       params.height = 0; 
      } 

      sampleTV.setLayoutParams(params); 

お試しください。

1

はちょうどあなたのXMLにこれを追加し、それが仕事を取るだろう

 TextView sampleTV = (TextView) findViewById(R.id.sampleText); 

    //initially translate the view to bottom 
    sampleTV.setTranslationY(sampleTV.getHeight()); 

    // for sliding up 
    sampleTV.animate().translationY(0).setDuration(100).setInterpolator(new AccelerateDecelerateInterpolator()).start(); 

    // for sliding down 
    sampleTV.animate().translationY(sampleTV.getHeight()).setDuration(100).setInterpolator(new AccelerateDecelerateInterpolator()).start(); 
1

のようなオブジェクトのアニメーターを使用してみてください。私は、ビューは、ビューが画面外であることを意味している場合、私はその後、ときに「見えない」visibility属性を設定してあるの内外ビューをアニメーション化するために何その

<RelativeLayout> 

     ..some other stuff... 

     <RelativeLayout 
       android:id="@+id/viewToAnimate" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
     *******android:animateLayoutChanges="true"******** 
       android:gravity="center" 
       android:layout_alignParentBottom="true" 
       android:background="@color/white"> 

       <TextView 
        android:id="@+id/sampleText" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Sample Text" 
        android:textSize="50sp"/> 

     </RelativeLayout> 

    </RelativeLayout> 
1

よう

android:animateLayoutChanges="true" 

アニメーションを開始するときは「Visible」に設定し、アニメーションを終了するときは「Invisible」に設定します。

高さを変更する代わりに、視認性を設定します。 XMLの使用での視認性を設定する

if (expand) { 
    viewToAnimate.setVisibility(View.VISIBLE); 
} else if (collapse) { 
    viewToAnimate.setVisibility(View.INVISIBLE); 
} 

android:visibility="invisible" 

は、この情報がお役に立てば幸いです。

明確化:私はアニメーションする内容、レイアウト/ビューの可視性を設定するsetAnimationListenerを使用するよりもアニメーションを取得するためにカスタムアニメーションとAnimationUtils.loadAnimationを使用してい

例:ビューにスライドさせるための

Animation animation = AnimationUtils.loadAnimation(this, R.anim.custom_animation_in_or_out_of_view); 
animation.setAnimationListener(new Animation.AnimationListener(){ 
      @Override public void onAnimationStart(Animation animation) { 
       //should always be visible on animation starting 
       viewToAnimate.setVisibility(View.INVISIBLE); 
      } 
      @Override public void onAnimationRepeat(Animation animation) {} 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       //set your viewToAnimate to the corresponding visibility... 
      } 
     }); 
viewToAnimate.startAnimation(animation); 

カスタムアニメーションXML:

<?xml version="1.0" encoding="utf-8"?> 
<translate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromYDelta="0%p" 
    android:toYDelta="100%p" 
    android:duration="1000" /> 

及びビューのうちスライドは、単に 'fromYDelta' と 'toYDelta' の値を切り替えます。

関連する問題