Android动画介绍——补间动画(2)

本文介绍了如何在Android中使用AnimationSet来组合透明度、移动、旋转和缩放等动画效果,通过XML和代码示例展示了如何创建并控制这些动画的执行,包括设置时长、重复次数和模式。
摘要由CSDN通过智能技术生成

接上一篇,这里我们介绍AnimationSet,也就是组合渐变。

如下是我们用到的类:

android.view.animation.AnimationSet

上一篇我们介绍了透明动画效果(AlphsAnimation)、移动动画效果(TranslateAnimation)、旋转动画效果(RotateAnimation)、缩放动画效果(ScaleAnimation);那么可不可以将这几种动画效果混在一块用呢?可以的,这个时候我们就用到了AnimationSet,它可以将一种或多种动画效果放在一起运行。

AnimationSet也继承Animation,常见方法如下:

方法描述

AnimationSet(boolean shareInterpolator)

构造函数

void addAnimation(Animation a)

添加定义好的动画效果

void setDuration(long durationMillis)

设置播放时长(毫秒)

void setRepeatCount(int repeatCount)

设置重放次数

void setRepeatMode(int repeatMode)

设置重放模式(reverse表示倒序回放,restart表示从头播放)

void setInterpolator(Interpolator i)

设置插值器

void setFillAfter(boolean fillAfter)

控件动画结束时是否保持动画最后的状态

void setFillBefore(boolean fillBefore)

控件动画结束时是否还原到开始动画前的状态

void setStartOffset(long startOffset)

调用start函数之后等待开始运行的时间,单位为毫秒

void setAnimationListener(AnimationListener listener)

动画的事件监听

这里有一个差值器(interpolator)的概念,我们后续再介绍。

我们同样采用xml load和在Activity中直接设置两种方式实现。

首先新建animation.xml文件,内容如下:

<?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" />


    <rotate
        android:fromDegrees="0"
        android:toDegrees="720"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:duration="2000" />


    <scale
        android:fromXScale="1"
        android:toXScale="10"
        android:fromYScale="1"
        android:toYScale="10"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:duration="2000" />
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="320"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:duration="2000" />
</set>

然后在上一篇Domo的基础上修改Activity,代码如下:

package com.example.animationsetdomo;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {


    private ImageView imageView;
    private int count = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);


        imageView = findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count == 0){
                    imageView.startAnimation(animation);
                    count = 1;
                }else if(count == 1){
                    AnimationScale();
                    count = 0;
                }
            }
        });
    }


    void AnimationScale(){


        //构造方法的入参如果是“ture”,则代表使用默认的interpolator,如果是“false”则代表使用自己定义interpolator
        AnimationSet animationSet = new AnimationSet(true);


        //透明度从0到1
        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);


        //旋转2圈
        RotateAnimation rotateAnimation = new RotateAnimation(0,720,
                Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);


        //放大2倍
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f,10,1f,10,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setRepeatCount(1);
        scaleAnimation.setRepeatMode(Animation.REVERSE);


        //平移
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.ABSOLUTE,0, Animation.RELATIVE_TO_PARENT,0.305f,
                Animation.ABSOLUTE,0, Animation.RELATIVE_TO_PARENT,0.305f);
        translateAnimation.setRepeatCount(1);
        translateAnimation.setRepeatMode(Animation.REVERSE);


        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);


        animationSet.setDuration(2000);
        animationSet.setRepeatMode(Animation.REVERSE);


        animationSet.setFillAfter(false);
        animationSet.setFillBefore(true);


        animationSet.cancel();
        animationSet.reset();


        imageView.startAnimation(animationSet);
    }
}

Domo代码:

Android动画介绍-补间动画(2)资源-CSDN文库 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值