5

Tôi đã tìm cách đơn giản nhất để tạo ảnh động một đối tượng với ConstraintLayout từ điểm A đến điểm B, với khả năng thay đổi thời lượng và tốc độ tăng tốc của nó. Ví dụ: di chuyển bố cục/chế độ xem từ dưới màn hình xuống đến vị trí dự định với các ràng buộc được đặt trên màn hình. Tôi đã không thể tìm thấy cách để làm điều đó cho các đối tượng với ConstraintLayout. Bất cứ ai cũng có thể chỉ cho tôi đi đúng hướng? Cảm ơn.Làm cách nào để tạo hiệu ứng ConstraintLayout từ dưới lên trên cùng của màn hình?

Trả lời

6

Có quan điểm này xml như nội dung:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/constraint_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Button" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.0" /> 

</android.support.constraint.ConstraintLayout> 

Và điều này trong hoạt động:

class SecondActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.item) 

     val constraintLayout = findViewById(R.id.constraint_layout) as ConstraintLayout 
     val button = findViewById(R.id.button) 

     button.setOnClickListener { 
      val constraintSet = ConstraintSet() 
      constraintSet.clone(constraintLayout) 
      constraintSet.setVerticalBias(R.id.button, 1.0f) 
      constraintSet.setHorizontalBias(R.id.button, 1.0f) 

      val transition = AutoTransition() 
      transition.duration = 1500 
      transition.interpolator = AccelerateDecelerateInterpolator() 

      TransitionManager.beginDelayedTransition(constraintLayout, transition) 
      constraintSet.applyTo(constraintLayout) 
     } 
    } 
} 

sẽ dẫn đến kết quả này:

enter image description here

Xem this articlethis presentation để biết thêm chi tiết.

Các vấn đề liên quan