0

我使用約束佈局作爲根與3子視圖:幻燈片向上/向下SurfaceView在ConstraintLayout

  • 的TextView
  • 按鈕
  • SurfaceView(View.Visibility =已刪除)

我想當我點擊Button時,SurfaceView出現(View.Visibility = Visible)from:

Y = - SurfaceView.height

到:

Y = 0

,當我點擊第二個時間上的按鈕,SurfaceView消失(View.Visibility =飄)從:

Y = 0

到:

Y = - SurfaceView.height

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <TextView 
     android:id="@+id/title" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Animation" 
     android:textSize="30sp" 
     android:gravity="center" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 

    <Button 
     android:id="@+id/display_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Dsiplay" 
     android:textSize="30sp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/title" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 


    <SurfaceView 
     android:id="@+id/animation_view" 
     android:layout_width="0dp" 
     android:layout_height="150dp" 
     android:visibility="gone" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 



</android.support.constraint.ConstraintLayout> 

MainActivity:

public class MainActivity extends Activity { 


    boolean isVisible = false; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


     findViewById(R.id.display_btn).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       isVisible = !isVisible; 

       if(isVisible){ 

        /* 
        * Start Animation slide to down 
        * */ 

        findViewById(R.id.animation_view).animate() 
          .translationY(0) 
          .setListener(new AnimatorListenerAdapter() { 

           @Override 
           public void onAnimationStart(Animator animation) { 

            super.onAnimationStart(animation); 


            findViewById(R.id.animation_view).setY(-findViewById(R.id.animation_view).getHeight()); 

            findViewById(R.id.animation_view).setVisibility(View.VISIBLE); 

           } 

          }); 

       } 
       else { 

        /* 
        * Start Animation slide to up 
        * */ 


        findViewById(R.id.animation_view).animate() 
          .translationY(-findViewById(R.id.animation_view).getHeight()) 
          .setListener(new AnimatorListenerAdapter() { 
           @Override 
           public void onAnimationStart(Animator animation) { 

            super.onAnimationStart(animation); 

           } 

           @Override 
           public void onAnimationEnd(Animator animation) { 

            super.onAnimationEnd(animation); 

            findViewById(R.id.animation_view).setVisibility(View.GONE); 

           } 

          }); 

       } 

      } 

     }); 



    } 
} 

當我點擊,第一次它不工作(滑下)。

+0

'tranlationY()'移動視圖上下在屏幕上。如果你想動畫消失,你應該看看'scaleX()'和'scaleY'。 – Cheticamp

+0

不,我只是想將視圖向下移動,但第一次(向下動畫)它只顯示得如此之快以至於它沒有動畫,我沒有看到任何移動。 –

回答

0

您的SurfaceView沒有正確的初始條件。這些條件在完成動畫後自行修正,這就是後續動畫工作的原因。添加以下到SurfaceView XML:

android:translationY="-150dp" 

你也可以做到這一點的代碼,但你需要提前做的比onClick()處理。

0

如果它可以幫助某人。

ConstraintLayout

<android.support.constraint.ConstraintLayout> 


    ... 

    <SurfaceView 
     android:id="@+id/animation_view" 
     android:layout_width="0dp" 
     android:layout_height="150dp" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:visibility="gone" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     android:translationY="-150dp" /> 




</android.support.constraint.ConstraintLayout> 

MainActivity

public class MainActivity extends Activity { 


    float translationY; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


     translationY = findViewById(R.id.animation_view).getTranslationY(); 

     findViewById(R.id.display_btn).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 


       if(findViewById(R.id.animation_view).getVisibility() == View.GONE){ 

        /* 
        * Start Animation slide to down 
        * */ 

        findViewById(R.id.animation_view).animate() 
          .translationY(0) 
          .setListener(new AnimatorListenerAdapter() { 

           @Override 
           public void onAnimationStart(Animator animation) { 

            super.onAnimationStart(animation); 

            findViewById(R.id.animation_view).setVisibility(View.VISIBLE); 

           } 

          }); 

       } 
       else if(findViewById(R.id.animation_view).getVisibility() == View.VISIBLE){ 

        /* 
        * Start Animation slide to up 
        * */ 


        findViewById(R.id.animation_view).animate() 
          .translationY(translationY) 
          .setListener(new AnimatorListenerAdapter() { 
           @Override 
           public void onAnimationStart(Animator animation) { 

            super.onAnimationStart(animation); 

           } 

           @Override 
           public void onAnimationEnd(Animator animation) { 

            super.onAnimationEnd(animation); 

            findViewById(R.id.animation_view).setVisibility(View.GONE); 

           } 

          }); 

       } 

      } 

     }); 



    } 
} 
相關問題