一、补间动画

1.alpha 透明度

2.rotate 旋转

3.scale 缩放

4.translate 平移

二、实现步骤

image.png

三、整体代码

1.动画透明度alpha.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:fromAlpha="0"
  5. android:toAlpha="1"
  6. android:duration="2000"
  7. />
  8. </set>

2.动画旋转rotate.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <rotate android:fromDegrees="0"
  4. android:toDegrees="360"
  5. android:pivotX="50%"
  6. android:pivotY="50%"
  7. android:duration="2000"/>
  8. </set>

3.动画缩放scale.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <scale
  4. android:fromXScale="1"
  5. android:fromYScale="1"
  6. android:toXScale="2"
  7. android:toYScale="2"
  8. android:pivotX="50%"
  9. android:pivotY="50%"
  10. android:duration="2000"
  11. />
  12. </set>

4.动画平移translate.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:fromXDelta="0"
  5. android:fromYDelta="0"
  6. android:toXDelta="300"
  7. android:toYDelta="400"
  8. android:duration="2000"
  9. />
  10. </set>

5.布局activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:id="@+id/iv_image"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:adjustViewBounds="true"
  11. android:maxWidth="300dp"
  12. android:maxHeight="300dp"
  13. android:src="@drawable/jin"
  14. />
  15. </RelativeLayout>

6.MainActivity文件代码

  1. package com.example.mytweenedanimation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.animation.Animation;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.ImageView;
  8. import android.widget.RelativeLayout;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. final ImageView iv_image = findViewById(R.id.iv_image);
  15. iv_image.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. //通过加载xml动画设置文件来创建一个Animation对象
  19. //1.绑定透明度布局
  20. //Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
  21. //2.绑定旋转布局
  22. //Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
  23. //3.绑定缩放布局
  24. //Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
  25. //4.绑定平移布局
  26. Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
  27. //启动动画
  28. iv_image.startAnimation(animation);
  29. }
  30. });
  31. }
  32. }

7.效果图:

(1)透明度alpha效果

image.png

(2)旋转rotate效果

image.png

(3)缩放scale效果image.png

(4)平移translate效果

image.png