20. 补间动画

20. 补间动画

给动画设置一个初始值以及一个结束值,加上一个动画时间,Android系统自动补全。

20.1 补间动画分类
  • alpha 透明度
  • rotate 旋转
  • scale 缩放
  • translate 平移
20.2 布局文件

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        android:layout_centerInParent="true"
        android:src="@drawable/test"
        android:maxWidth="300dp"
        android:maxHeight="300dp"
        android:adjustViewBounds="true"
        />

</RelativeLayout>

20.3 透明度变化

创建anim文件夹

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000"
        />

</set>

从透明到不透明

使用

ImageView imageView = findViewById(R.id.iv);

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //通过加载xml动画设置文件来创建一个Animation对象
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);

        //启动
        imageView.startAnimation(animation);
    }
});

在这里插入图片描述

20.4 旋转

在这里插入图片描述

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

pivot设置中心轴

0°到360°

在这里插入图片描述

运行

在这里插入图片描述

20.5 缩放动画

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="0.5"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        />
</set>

缩放为原来的一半

在这里插入图片描述

运行

在这里插入图片描述

20.6 平移

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="400"
        android:toYDelta="400"
        android:duration="2000"
        />
</set>

在这里插入图片描述

运行

在这里插入图片描述

posted @ 2022-09-19 07:50  随遇而安==  阅读(59)  评论(0编辑  收藏  举报