Android 安卓动画 属性动画 - 移动动画

本是古典 何须时尚 2022-05-14 06:21 513阅读 0赞

引入

属性动画的出现,弥补了补间动画的不足之处,补间动画,只是改变了表面上的东西,但是其中属性并未改变,而属性动画相反,改变了表面上的东西,并且也更改了其属性。


类:ObjectAnimator

用于操作属性动画的类


动画 - 相关文章篇

帧动画

帧动画: https://blog.csdn.net/qq_40881680/article/details/82222684

补间动画

补间动画-平移动画: https://blog.csdn.net/qq_40881680/article/details/82255459

补间动画-缩放动画: https://blog.csdn.net/qq_40881680/article/details/82260914

补间动画-旋转动画: https://blog.csdn.net/qq_40881680/article/details/82261557

补间动画-透明/渐变动画: https://blog.csdn.net/qq_40881680/article/details/82261869

补间动画-组合动画(四个动画一起播放): https://blog.csdn.net/qq_40881680/article/details/82285987

属性动画

属性动画-渐变透明动画: https://blog.csdn.net/qq_40881680/article/details/82318363

属性动画-旋转动画: https://blog.csdn.net/qq_40881680/article/details/82354017

属性动画-缩放动画: https://blog.csdn.net/qq_40881680/article/details/82377850

属性动画-移动动画: https://blog.csdn.net/qq_40881680/article/details/82378391

属性动画-组合动画: https://blog.csdn.net/qq_40881680/article/details/82381258


布局文件 篇

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="0dp"
  12. android:layout_weight="1"
  13. android:background="#9c98ce"
  14. android:orientation="vertical"
  15. android:paddingLeft="20dp"
  16. android:paddingRight="20dp"
  17. android:paddingTop="10dp">
  18. <Button
  19. android:id="@+id/button"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:background="#5b7bda"
  23. android:text="点击演示动画"
  24. android:textColor="#fff" />
  25. </LinearLayout>
  26. <LinearLayout
  27. android:layout_width="match_parent"
  28. android:layout_height="0dp"
  29. android:layout_weight="4"
  30. android:orientation="vertical">
  31. <ImageView
  32. android:id="@+id/image"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_gravity="center"
  36. android:layout_marginTop="20dp"
  37. android:background="@mipmap/kuiba" />
  38. <TextView
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_gravity="center"
  42. android:text="《魁拔》"
  43. android:textSize="18sp" />
  44. </LinearLayout>
  45. </LinearLayout>

代码逻辑 篇

属性动画用到的是:ObjectAnimator

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. ObjectAnimator objectAnimator;
  3. private Button button;
  4. private ImageView image;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. initView();
  10. }
  11. private void initView() {
  12. button = (Button) findViewById(R.id.button);
  13. image = (ImageView) findViewById(R.id.image);
  14. button.setOnClickListener(this);
  15. image.setOnClickListener(this);
  16. }
  17. @Override
  18. public void onClick(View v) {
  19. switch (v.getId()) {
  20. case R.id.button:
  21. objectAnimator = ObjectAnimator.ofFloat(image,"translationX",0f,60f,0f);
  22. objectAnimator.setDuration(2000);
  23. objectAnimator.start();
  24. break;
  25. case R.id.image:
  26. Toast.makeText(this, "我是属性动画", Toast.LENGTH_SHORT).show();
  27. break;
  28. }
  29. }
  30. }

AndroidStudio快速实例化-插件安装与使用:https://blog.csdn.net/qq_40881680/article/details/82012180


objectAnimator = ObjectAnimator.ofFloat(image,”translationX”,60f);

沿着X轴向右移动60f,效果只有一次 (负数为向左)


objectAnimator = ObjectAnimator.ofFloat(image,”translationX”,0f,60f);

动画从0f,沿着X轴向右移动60f (负数为向左)


objectAnimator = ObjectAnimator.ofFloat(image,”translationX”,0f,60f,0f);

动画从0f,沿着X轴向右移动60f,之后再回到0f (负数为向左)


ObjectAnimator.ofFloat()括号中的参数:

第一个参数,要实现动画的控件id

第二个参数,要实现的动画属性,以下列出6种:


































propertyName

详细作用
alpha 实现渐变效果
rotation 实现旋转旋转效果
translationX 实现水平移动效果(左或右移动)
translationY 实现纵向移动效果(向上或者向下移动)
scaleX 实现轴X缩放效果(放大或者缩小)
scaleY 实现轴Y缩放效果(放大或者缩小)

后面的参数就不多做解释了,以上都有

Y轴同理相反,将第二个参数改为translationY即可


效果演示 篇

沿着X轴向右移动60f,效果只有一次 (负数为向左)

70


动画从0f,沿着X轴向右移动60f (负数为向左)

70 1


动画从0f,沿着X轴向右移动60f,之后再回到0f (负数为向左)

70 2

发表评论

表情:
评论列表 (有 0 条评论,513人围观)

还没有评论,来说两句吧...

相关阅读