android帧动画详解

本文详细介绍了Android帧动画的使用,包括两种实现方法,并通过代码示例进行解析,旨在帮助开发者轻松掌握帧动画的实现技巧。

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

学习使我快乐使我开心
安卓的动画大体分为三部分 请看图:
在这里插入图片描述

由上图可以知道安卓中动画主要分为这三大类 分别为视图动画(View Animation) ,帧动画(Drawable Animation),属性动画(PropertyAnimation)
根据上图我们就可以知道这章我要讲什么了 哈哈 那就是帧动画
柿子要挑软的捏 难题嘛我们就先从简单的着手
帧动画:图片按照顺序一帧一帧组合而成的动画
废话不多说直接看代码:
文件存放路径: main/res/drawable

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

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false" >
    
<!--android:oneshot="false"true表示只播放一次,false则相反-->

   <item
     android:drawable="@drawable/first"
     android:duration="500"/>
   <item
     android:drawable="@drawable/second"
     android:duration="500"/>
  <item
     android:drawable="@drawable/third"
     android:duration="500"/>
  <item
     android:drawable="@drawable/fourth"
     android:duration="500"/>

</animation-list>

第一种方法

布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity中:

  imageView = findViewById(R.id.imgview);
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.zhendonghua);
        imageView.setImageDrawable(animationDrawable);
        animationDrawable.start();//开启动画
        animationDrawable.stop();//关闭动画

第二种

布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgview"
        android:background="@drawable/zhendonghua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity中:

 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        //判断是否在运行
        if(!animationDrawable.isRunning()){
            //开启帧动画
            animationDrawable.start();
        }

好啦 关于帧布局就到这里了 因为帧布局很简单所以就不更多的演示了 我会在后面更新中将安卓的动画慢慢学习更新玩的
喜欢的话请给我来个小赞吧 你的支持是我最大的动力!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安东尼肉店

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

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

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

打赏作者

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

抵扣说明:

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

余额充值