Android-Animation动画学习

Android动画主要分为 补间动画逐帧动画

动画的实现方式
1.代码实现
2.xml文件实现

所有演示都基于一张图片

补间动画

补间动画 之 Alpha:透明度渐变动画

(1)编写Alpha渐变动画文件

duration:持续时长,以ms为单位
fromAlpha:开始时的渐变度
toAlpha:结束时的渐变度
若需无限循环,需要设置repeatCount=”infinite”

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        >
    </alpha>
</set>

(2)加载动画

 //代码创建
 AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(2000);
//加载xml文件
 Animation alphaAnimation= AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);
 //调用动画
image_Example.startAnimation(alphaAnimation);

效果如下:
alpha

补间动画 之 Alpha:缩放动画

(1)编写xml文件
fromXScale:在X轴上的开始点
toXScale:在X轴上的结束点
fromYScale:在Y轴上的开始点
toYScale:在Y轴上的结束点
pivotX:变换起点X坐标
pivotY:变换起点Y坐标

<ml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="3000"
        android:fromXScale="0"
        android:toXScale="1.0"
        android:fromYScale="0"
        android:toYScale="1.0"
        android:pivotX="100%"
        android:pivotY="100%">
    </scale>
</set>

(2)加载动画

//代码生成动画
 ScaleAnimation scaleAnimation=new ScaleAnimation(0f,1f,0f,1f);
 scaleAnimation.setDuration(2000);
 //调用资源文件生成动画
 Animation animation1=AnimationUtils.loadAnimation(this,R.anim.scale);
//加载动画
image_Example.startAnimation(scaleAnimation);

(3)效果如下
这里写图片描述

补间动画 之 Alpha:旋转动画

(1)编写xml文件
上面有的这里不再重复,只叙述新的
android:fromDegrees:起始旋转角度
android:toDegrees:一共旋转多少度
repeatMode:重复模式,从有restart和reserve两种。就是一种正着来,一种反着来。
repeatCount:重复次数

<?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"
        android:pivotY="50%"
        android:pivotX="50%"
        android:repeatMode="reverse"
        android:repeatCount="3">

    </rotate>
</set>

(2)加载动画

//代码生成动画
 RotateAnimation rotateAnimation=new RotateAnimation(0,360);
                rotateAnimation.setDuration(3000);
                rotateAnimation.setRepeatCount(2);
                rotateAnimation.setRepeatMode(Animation.REVERSE);
 //调用资源文件生成动画
 Animation rotateAnimation=AnimationUtils.loadAnimation(this,R.anim.rotate);
//加载动画
image_Example.startAnimation(rotateAnimation);

(3)效果如下
这里写图片描述

补间动画 之 Alpha:平移动画

(1)编写xml文件
上面有的这里不再重复,只叙述新的
android:fromXDelta:起始X轴平移坐标
android:toXDelta:起始X轴终点坐标
android:fromYDelta:起始Y轴的平移坐标
android:toYDelta:起始Y轴的终点坐标

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

    </translate>
</set>

(2)加载动画

//代码生成动画
  TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);
               translateAnimation.setDuration(3000);
 //调用资源文件生成动画
  Animation animation3=AnimationUtils.loadAnimation(this,R.anim.translate);
//加载动画
image_Example.startAnimation(translateAnimation);

(3)效果如下
这里写图片描述


逐帧动画

下次在介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
animation-list是Android中的一个Drawable资源类型,用于定义逐帧动画。它可以让你定义一系列不同的Drawable资源,并通过设置每个Drawable资源在动画中的时长和顺序来创建一个动画序列。每个Drawable资源可以是一个静态图像、一个逐帧动画或一个过渡动画。你可以将animation-list应用于任何View或ViewGroup的背景,以创建动态的背景效果。 要使用animation-list,你需要在res/drawable文件夹下创建一个XML文件,并在其中定义Drawable资源。以下是一个简单的animation-list示例: ```xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> </animation-list> ``` 在这个例子中,我们定义了一个animation-list,其中包含三个Drawable资源:frame1、frame2和frame3。我们设置了每个Drawable资源在动画序列中的时长为100毫秒,并将oneshot属性设置为true,表示动画只会播放一次。要在代码中使用这个animation-list,可以通过以下方式: ```java ImageView imageView = findViewById(R.id.image_view); imageView.setBackgroundResource(R.drawable.my_animation); AnimationDrawable anim = (AnimationDrawable) imageView.getBackground(); anim.start(); ``` 在这个例子中,我们首先获取了一个ImageView,并将其背景设置为animation-list资源my_animation。然后我们获取ImageView的背景,并将其转换为AnimationDrawable对象。最后,我们调用start()方法开始动画

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值