2018/10/31

[Android] Animation on UI View

Android 裡面有一些動畫的套件可以用,例如: 淡入出,移動...等。
這篇先以淡入、淡出的動畫來示範
最主要的程式碼是這些:
  1. Animation fadeIn = new AlphaAnimation(0, 1);
  2. fadeIn.setInterpolator(new AccelerateInterpolator());
  3. fadeIn.setStartOffset(100);
  4. fadeIn.setDuration(1000);
  5. fadeIn.setAnimationListener(new Animation.AnimationListener()
  6. {
  7. public void onAnimationEnd(Animation animation)
  8. {
  9. ivImage.setVisibility(View.VISIBLE);
  10. }
  11. public void onAnimationRepeat(Animation animation) {}
  12. public void onAnimationStart(Animation animation) {}
  13. });
  14. ivImage.startAnimation(fadeIn);

利用這個套件,寫了一個簡單的圖片淡入、淡出的範例程式
MainActivity.java:
  1. public class MainActivity extends AppCompatActivity{
  2.  
  3. ImageView ivImage;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. ivImage = (ImageView)findViewById(R.id.id_ivImage);
  11. }
  12.  
  13. public void onClick(View v){
  14. switch (v.getId()){
  15. case R.id.id_btn01:
  16. mImageAnimation(ivImage, 100, 1000, true);
  17. break;
  18.  
  19. case R.id.id_btn02:
  20. mImageAnimation(ivImage, 100, 1000, false);
  21. break;
  22. }
  23. }
  24.  
  25. private static void mImageAnimation(final View img, long offset, long duration, boolean isFadeIn){
  26. if(isFadeIn){
  27. Animation fadeIn = new AlphaAnimation(0, 1);
  28. fadeIn.setInterpolator(new AccelerateInterpolator());
  29. fadeIn.setStartOffset(offset);
  30. fadeIn.setDuration(duration);
  31. fadeIn.setAnimationListener(new Animation.AnimationListener()
  32. {
  33. public void onAnimationEnd(Animation animation)
  34. {
  35. img.setVisibility(View.VISIBLE);
  36. }
  37. public void onAnimationRepeat(Animation animation) {}
  38. public void onAnimationStart(Animation animation) {}
  39. });
  40. img.startAnimation(fadeIn);
  41. }
  42. else {
  43. Animation fadeOut = new AlphaAnimation(1, 0);
  44. fadeOut.setInterpolator(new AccelerateInterpolator());
  45. fadeOut.setStartOffset(offset);
  46. fadeOut.setDuration(duration);
  47. fadeOut.setAnimationListener(new Animation.AnimationListener()
  48. {
  49. public void onAnimationEnd(Animation animation)
  50. {
  51. img.setVisibility(View.INVISIBLE);
  52. }
  53. public void onAnimationRepeat(Animation animation) {}
  54. public void onAnimationStart(Animation animation) {}
  55. });
  56. img.startAnimation(fadeOut);
  57. }
  58. }
  59. }

activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.chris.myapplication.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/textView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:layout_marginTop="8dp"
  16. android:text="MainActivity"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20.  
  21. <Button
  22. android:id="@+id/id_btn01"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginEnd="8dp"
  26. android:layout_marginStart="8dp"
  27. android:layout_marginTop="8dp"
  28. android:onClick="onClick"
  29. android:text="Fade in"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toBottomOf="@+id/textView" />
  33.  
  34. <Button
  35. android:id="@+id/id_btn02"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginEnd="8dp"
  39. android:layout_marginStart="8dp"
  40. android:layout_marginTop="8dp"
  41. android:onClick="onClick"
  42. android:text="Fade out"
  43. app:layout_constraintEnd_toEndOf="parent"
  44. app:layout_constraintStart_toStartOf="parent"
  45. app:layout_constraintTop_toBottomOf="@+id/id_btn01" />
  46.  
  47. <ImageView
  48. android:id="@+id/id_ivImage"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginEnd="8dp"
  52. android:layout_marginStart="8dp"
  53. android:layout_marginTop="8dp"
  54. app:layout_constraintEnd_toEndOf="parent"
  55. app:layout_constraintStart_toStartOf="parent"
  56. app:layout_constraintTop_toBottomOf="@+id/id_btn02"
  57. app:srcCompat="@drawable/img" />
  58.  
  59. </android.support.constraint.ConstraintLayout>

其實 Android 中還有其他動畫套件,例如: RotateAnimation、ScaleAnimation、TranslateAnimation
有空可以玩玩看。

END

沒有留言:

張貼留言