Android开发之View动画

本文详细介绍了Android中的四种视图动画:平移、缩放、旋转及透明度变化,并提供了具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android动画主要分为3种
  • View动画
  • 帧动画
  • 属性动画
何为View动画?

View动画主要是对View对象进行变换所达到的动画效果,如平移、缩放、旋转和透明度等,下面写个简单案例。

动画文件

首先在res目录下新建一个anim文件夹,然后新建4个动画文件,如下:

然后在Activity布局中放入一张图片:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/football"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/football" />
</RelativeLayout>
复制代码
Activity代码
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.football);

    }
复制代码
1、平移动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="3000" 
        android:fromXDelta="0"  //x的起始值
        android:fromYDelta="0"  //y的起始值
        android:toXDelta="400" //x的结束值
        android:toYDelta="400" /> //y的结束值

</set>
复制代码

android:fromXDelta:x的起始值 android:toXDelta:x的结束值 android:fromYDelta:y的起始值 android:toYDelta:y的结束值

核心代码

private void translateAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translateanim);
        img.startAnimation(animation);
    }
复制代码

测试运行

2、缩放动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="3000"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>
复制代码

android:fromXScale:水平方向缩放的起始值 android:toXScale:水平方向缩放的结束值 android:fromYScale:垂直方向缩放的起始值 android:toYScale:垂直方向缩放的结束值

核心代码

private void scaleAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanim);
        img.startAnimation(animation);
    }
复制代码

测试运行

3、旋转动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:toDegrees="360" />


</set>
复制代码

android:fromDegrees:旋转开始的角度 android:toDegrees:旋转结束的角度

核心代码

private void roteteAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotateanim);
        img.startAnimation(animation);
    }
复制代码

测试运行

4、透明度动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>
复制代码

android:fromAlpha:起始透明度 android:toAlpha:结束透明度

核心代码

private void alphaAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alphaanim);
        img.startAnimation(animation);
    }
复制代码

测试运行

注意点

1、动画集合 <set xmlns:android="http://schemas.android.com/apk/res/android"> 中可以设置一些属性值,重要属性说明:

  • android:interpolator:动画集合插值器,主要影响动画的速度,默认为加速减速插值器,还有线性插值器、减速插值器等等
  • android:shareInterpolator:动画集合中的动画是否与几何共享同一个插值器
  • android:duration:动画集合执行时间
  • android:fillAfter:动画结束以后View是否停在结束位置,默认是false不停留,但是该属性需要设置在动画集合中才有效果,设在单独的动画中是无效的

2、View动画并没有真正改变View的位置,也就是说就算你看到了动画最终停留在了某个位置,它的真身还是在原来的位置,有点像神话小说的元神出窍,所以使用的时候要特别注意,如给Button设置点击事件,就会发现新位置的Button并不会出发click事件,原始位置却能响应,不知道原因的同学肯定入坑~~

转载于:https://juejin.im/post/5a3113996fb9a0452936c1af

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值