Android基础——Animation动画

本文深入探讨了Android中的帧动画,它是通过逐帧显示不同图像来创建动画效果的技术。适合初学者了解和掌握。

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

帧动画:一页一页的动画

/**
 * 资源文件创建帧动画
 * 1, 在res/drawable 文件中创建一个xml 个是的文件, 根节点为<animation-list></animation-list>
 * 2, 需要在根节点中, 添加每一帧图片
 *      <item  android:drawable="图片的id" android:duration="持续的时间 (毫秒)"/>
 * 3, 在布局页面中, ImageView 控件中引入 :  <ImageView  android:background = "帧动画资源的id"/>
 * 4, 在Activity 中, 得到动画资源管理器对象
 *      AnimationDrawable animationDrawable = (AnimationDrawable)mIv.getBackground();
 * 5, 调用      animationDrawable.start() ; 开启动画
 * 6, 调用      animationDrawable.stop(); 停止动画
 */
public class FreamAnimationActivity01 extends AppCompatActivity {

    //1, 得到动画管理器对象
    private AnimationDrawable animationDrawable;
    private ImageView mIv;

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

        mIv = (ImageView) findViewById(R.id.iv_id);
        //动画资源是通过页面中 android:background 属性设置, 所以只能通过ImageView 来获取
        animationDrawable = (AnimationDrawable) mIv.getBackground();
    }

    //程序一旦进入, 则开始播放动画 - --- 窗口焦点改变时调用即可  -- 扩展
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!animationDrawable.isRunning()) animationDrawable.start();
    }

    //开始播放动画
    public void start(View view) {
        // 判断:  如果动画没有播放, 则开始播放
        if (!animationDrawable.isRunning())
        {
            animationDrawable.start();
        }

    }

    //停止播放动画
    public void stop(View view) {

        animationDrawable.stop();
    }
}
//资源文件中创建

<?xml version="1.0" encoding="utf-8"?>
<!--1, 需要在res/drawable 文件夹中, 创建xml文件, 并且提供一个根节点
        animation-list  为节点

        android:drawable=""     当前显示的图片
        android:duration=""     持续时间, 停留时间  毫秒为单位
        -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@mipmap/anim1" android:duration="100"/>
    <item android:drawable="@mipmap/anim2" android:duration="100"/>
    <item android:drawable="@mipmap/anim3" android:duration="100"/>
    <item android:drawable="@mipmap/anim4" android:duration="100"/>
    <item android:drawable="@mipmap/anim5" android:duration="100"/>
    <item android:drawable="@mipmap/anim6" android:duration="100"/>
    <item android:drawable="@mipmap/anim7" android:duration="100"/>
    <item android:drawable="@mipmap/anim8" android:duration="100"/>
    <item android:drawable="@mipmap/anim9" android:duration="100"/>
    <item android:drawable="@mipmap/anim10" android:duration="100"/>
    <item android:drawable="@mipmap/anim11" android:duration="100"/>
    <item android:drawable="@mipmap/anim12" android:duration="100"/>

</animation-list>
//代码中创建

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import bw.com.bw_day02_animation.R;

/**
 *  代码的方式创建帧动画
 *  1, 初始化动画管理器对象
 *      AnimationDrawable drawable = new AnimationDrawable();
 *  2, 向动画管理器中添加图片资源
 *      drawable.addFrame(Drawable类型的图片, 持续时间 毫秒);
 *  3, 设置图片是否执行一次
 *      drawable.setOneShot(false); // true 执行一次; false 执行多次
 *  4, 将动画资源, 设置到图片中
 *      mIv.setImageDrawable(drawable);
 *  5, 调用方法 start() 开启动画  --- drawable.isRunning()  判断动画是否正在执行
 *      drawable.start();
 *  6, 调用方法 stop() 停止动画
 *      drawable.stop();
 */
public class FrameAnimationActivity02 extends AppCompatActivity {

    private ImageView mIv;

    private AnimationDrawable animationDrawable;

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

        mIv = (ImageView) findViewById(R.id.iv_id);

        //1, 初始化动画资源管理器对象
         animationDrawable = new AnimationDrawable();

        //2, 向动画资源中, 添加图片
        //Drawable类型的图片 ,  持续时间
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0001),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0002),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0003),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0004),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0005),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0006),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0007),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0008),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0009),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0010),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0011),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0012),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0013),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0014),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0015),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0016),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0017),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0018),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0019),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0020),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0021),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0022),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0023),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0024),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0025),100);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0026),100);

        //3, 设置动画是否执行一次 -- true :  只执行一次;    false  执行多次
        animationDrawable.setOneShot(false);

        //4, 将动画资源设置到图片控件中
        mIv.setImageDrawable(animationDrawable);
    }

    //4, 开启动画
    public void start(View view) {
        if(!animationDrawable.isRunning())
        {
            animationDrawable.start();
        }
    }

    //5, 结束动画
    public void stop(View view) {
        animationDrawable.stop();
    }
}

//补间动画

/**
 * 资源文件创建的补间动画
 * 1, res 的文件夹中 , 创建 anim文件夹
 * 2, 在res/anim 文件夹中, 创建一个xml 个是的动画, 根节点<alpha , scale, rotate,translate, set>
 * 3, 在Activity 中, 启动动画
 *     mIv.startAnimation(AnimationUtils.loadAnimation(上下文对象, 动画的资源id));
 */
public class TweenActivity01 extends AppCompatActivity {

    private ImageView mIv;

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

        mIv = (ImageView) findViewById(R.id.iv_id);
    }

    //点击按钮, 切换动画
    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.but_01:
                //渐变
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.alpha_anim));
                break;
            case R.id.but_02:
                //缩放
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.scale_anim));
                break;
            case R.id.but_03:
                //旋转
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.rotate_anim));
                break;
            case R.id.but_04:
                //位移
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.translate_anim));
                break;
            case R.id.but_05:
                //集合
                mIv.startAnimation(AnimationUtils.loadAnimation(this,R.anim.set_anim));
                break;

        }
    }
}

//资源文件中

<?xml version="1.0" encoding="utf-8"?>
<!--透明度的变化  渐变

    0.0  不可见,  1.0  可见

    android:fromAlpha=""    开始的透明度
    android:toAlpha=""      结束的透明度

    通用属性:
    android:duration=""     持续时间  毫秒
    android:fillAfter=""   是否保存结束时的状态
    android:repeatMode="reverse"  设置重复的模式
        reverse     反向执行
        restart     再次执行
    android:repeatCount = "2"   重复的次数   最终执行的次数 = 重复次数 + 执行 1

-->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="3000"
    android:fillAfter="true"
    android:repeatMode="reverse"
    android:repeatCount = "2"
    >
</alpha>

<?xml version="1.0" encoding="utf-8"?>
<!--旋转
      android:fromDegrees=""  开始角度
    android:toDegrees=""       结束的角度
    android:pivotX=""           X轴的中心的
    android:pivotY=""           Y轴的中心的
-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromDegrees="0"
    android:toDegrees="720"
    android:pivotX="50%"
    android:pivotY="50%"

    android:duration="3000"
    >

</rotate>

<?xml version="1.0" encoding="utf-8"?>

<!--缩放
    android:fromXScale=""       X轴起始比例
    android:toXScale=""         X轴的结束比例

    android:fromYScale=""       Y轴起始比例
    android:toYScale=""         Y轴的结束比例

     android:pivotX=""          X轴的中心的
    android:pivotY=""           Y轴的中心的
-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="1"
    android:toXScale="3"
    android:fromYScale="1"
    android:toYScale="3"
    android:pivotX="50%"
    android:pivotY="50%"

    android:duration="3000"
    >

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

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


    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="800"
        android:toYDelta="800"
        android:duration = "3000"
        />

</set>
<?xml version="1.0" encoding="utf-8"?>

<!--位移

    android:fromXDelta=""       X轴的起始位置
    android:toXDelta=""         X轴的结束位置

    android:fromYDelta=""       Y轴的起始位置
    android:toYDelta=""         Y轴的结束位置
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  插值器
    `                       @android:anim/bounce_interpolator  弹跳


-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXDelta="0"
    android:toXDelta="0"

    android:fromYDelta="0"
    android:toYDelta="1000"

    android:duration = "3000"
    android:interpolator="@android:anim/bounce_interpolator"
    >

</translate>

//代码中创建

import android.support.v7.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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import bw.com.bw_day02_animation.R;

/**
 * 代码创建补间动画
 *
 * 1, 声明每一种动画资源, 设置参数信息
 * 2, 设置持续时间
 * 3, 为控件启动动画
 *      mIv.startAnimation(animation);
 *
 */
public class TweenActivity02 extends AppCompatActivity {

    private ImageView mIv;

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

        mIv = (ImageView) findViewById(R.id.iv_id);
    }


    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.but_01:
                //渐变
                //开始的透明度 , 结束的透明度
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);//创建动画资源
                alphaAnimation.setDuration(3000);  //设置持续时间
                alphaAnimation.setFillAfter(true);//保持结束时的状态
                mIv.startAnimation(alphaAnimation);//为图片启动动画

                break;

            case R.id.but_02:
                //缩放
                /**
                 * X轴的起始比例
                 * X轴的结束比例
                 * Y轴的起始比例
                 * Y轴的结束比例
                 * X轴中心点的参考
                 * X轴的中心位置
                 * Y轴中心点的参考
                 * Y轴的中心位置
                 */
               // ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f);//左上角为参考点
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f,3f,1f,3f,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f
                        );//以X和Y 的中心的为参考

                scaleAnimation.setDuration(3000);

                mIv.startAnimation(scaleAnimation);

                break;

            case R.id.but_03:
                //旋转
                RotateAnimation rotateAnimation = new RotateAnimation(0,720,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f
                        );

                rotateAnimation.setDuration(3000);
                mIv.startAnimation(rotateAnimation);
                break;


            case R.id.but_04:
                //位移
                TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800);
                translateAnimation.setDuration(3000);
                mIv.startAnimation(translateAnimation);
                break;

            case R.id.but_05:
                //集合
                //1, 实例化动画集合的对象
                //true  代表使用结合的插值器,  false  代表使用动画自己的插值器
                AnimationSet animationSet = new AnimationSet(false);

                //2, 创建每一个动画
                TranslateAnimation tAnimation = new TranslateAnimation(0,300,0,300);
                AlphaAnimation aAnimation = new AlphaAnimation(1.0f,0.0f);

                //3, 把每个动画, 添加到集合中
                animationSet.addAnimation(tAnimation);
                animationSet.addAnimation(aAnimation);

                //4, 为集合动画, 设置属性
                animationSet.setDuration(3000);
                animationSet.setFillAfter(true);

                //5, 为图片启动动画
                mIv.startAnimation(animationSet);

                break;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值