Android: Hiệu ứng Animation Alpha
Giải phóng thời gian, khai phóng năng lực
Video hướng dẫn:
Bài viết hướng dẫn:
File activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:layout_width="180dp" android:layout_height="180dp" app:srcCompat="@drawable/logov1study" android:id="@+id/imageViewLogo" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintVertical_bias="0.145"/> <Button android:text="Start Alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStartAlpha" app:layout_constraintTop_toBottomOf="@+id/imageViewLogo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.489" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:textSize="24sp" android:textStyle="bold" android:textColor="#4CAF50"/> </androidx.constraintlayout.widget.ConstraintLayout>
File resource anim_alpha.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="3000" android:repeatMode="reverse" android:repeatCount="1"></alpha> </set>
File MainActivity.java:
package v1study.com.animationalphav1study; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { ImageView imageViewLogo; Button buttonStartAlpha; Animation animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getViews(); buttonStartAlpha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.anim_alpha); start(animation); } }); } private void getViews() { imageViewLogo=findViewById(R.id.imageViewLogo); buttonStartAlpha=findViewById(R.id.buttonStartAlpha); } void start(Animation animation){ imageViewLogo.startAnimation(animation); } }
Giải phóng thời gian, khai phóng năng lực