android动画之补间动画


所谓的补间动画,就是补全两个不同图片中缺失的过程.
比说一个图片的尺寸是100 100 ,另一个是200200. 那么100到200这个放大的过程就可以用补间动画来实现.

xml的创建动画

准备

首先在res文件夹下创建anim动画文件夹
在这里插入图片描述
在anim文件夹创建使用的动画文件

旋转动画

1, 动画xml文件

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromDegrees="0"
    android:toDegrees="90"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator">
    >

    <!--通用属性说明
     duration     动画持续时间
     fromDegrees  角度 (从左上角开始)

     fillAfter    用于确定是否保持动画结束时的值,如果设置为true,控件动画结束时,将保持动画
     interpolator 设定插值器,其实就是指定的动作效果,比如弹跳效果等。
     -->

</rotate>

2,代码

 btn0.setOnClickListener((v) -> {
 //loadAnimation 的两个参数,1是上下文,2是动画xml文件
            RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotate_img);
            //可以给任意控件加入动画,此处img是ImageView.
            img.startAnimation(rotateAnimation);
        });

平移动画

1, xml文件

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromYDelta="0"
    android:toYDelta="200%"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator">

    <!-- 
    fromYDelta Y轴的移动方向
    % 代表自己的大小的倍数
    -->
</translate>

2, 代码

TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.translate_img);
            img.startAnimation(translateAnimation);

透明度(渐变)

1, xml文件

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="0.1"
    android:toAlpha="1"
    android:fillAfter="true">
    
    <!-- 
        fromAlpha 透明度变化 1是完全不透明
    -->
    
    
</alpha>

2, 代码

AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha_img);
            img.startAnimation(alphaAnimation);

缩放

1, xml文件

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

    >
    
    <!-- 相同属性省略
    pivotX 缩放起点 x 轴
    fromXScale x轴上的缩放
    toXScale 结束后x轴上的缩放
    -->

</scale>

2, 代码

btn3.setOnClickListener((v) -> {
            ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_img);
            img.startAnimation(scaleAnimation);
        });

java代码中创建动画

在java中创建动画的意思就是直接new 动画的对象,根据构造填上需要的参数即可.
大多属性都相同,一个能用.另外一个也能用.比如:耗时,重复次数等.

旋转动画

			//
			RotateAnimation rotateAnimation = new RotateAnimation(0, 90, 0, 0);
            rotateAnimation.setDuration(1000);
            translateAnimation.setRepeatCount(1);//重复次数
            rotateAnimation.setFillAfter(true);
            rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
                //开始
                @Override
                public void onAnimationStart(Animation animation) {

                }

                //结束
                @Override
                public void onAnimationEnd(Animation animation) {

                }

                //重复
                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            img.startAnimation(rotateAnimation);

平移动画

  TranslateAnimation translateAnimation = new TranslateAnimation(0, img.getMaxWidth(), 0, img.getMaxHeight());
            translateAnimation.setDuration(1000);
            translateAnimation.setFillAfter(true);
            translateAnimation.setRepeatCount(1);//重复次数
            translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            img.startAnimation(translateAnimation);

透明度

AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1f);
            alphaAnimation.setDuration(1000);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            img.startAnimation(alphaAnimation);

缩放

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
            scaleAnimation.setDuration(2000);
            scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            img.startAnimation(scaleAnimation);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

馮贰爺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值