Android图形动画学习之逐帧动画

                                          Android动画学习之逐帧动画
        逐帧动画
        使用图像序列来实现逐帧动画的过程。
        ☆☆☆同定义补帧动画类似,我们通过XML文件的方式定义逐帧动画。
        1.在res文件夹中的drawable文件夹中,放入动画帧。
        2.在drawable文件夹下建立逐帧动画的XML文件,来定义动画帧。
        3.将建立的动画XML文件,作为图像加载到控件中。
        4.使用AnimationDrawable创建对象来获取动画资源。

        ☆☆☆使用Android Studio实现逐帧动画。
        1.打开Android Studio,新建工程后,在res文件夹中drawable文件夹,放入图片,我放入4张图片。
        在这里插入图片描述
        在这里插入图片描述
        2.在res文件夹中drawable文件夹建立XML文件,编写代码来设置动画。
        在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/p1" android:duration="500"/>
    <item android:drawable="@drawable/p2" android:duration="500"/>
    <item android:drawable="@drawable/p3" android:duration="500"/>
    <item android:drawable="@drawable/p4" android:duration="500"/>
</animation-list>

        3.在activity_main.xml中,建立1个Button,1个ImageView,将建立的动画XML文件,作为图像加载到ImageView控件中。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="lession.example.com.androidlession4_10.MainActivity">
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/button"
        android:layout_alignParentStart="true"
        android:text="逐帧动画演示"
        android:textSize="20sp"
        android:textColor="@android:color/holo_red_dark" />
        
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/image_list"
        android:id="@+id/imageView"
        android:layout_below="@+id/button"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="28dp" />
</RelativeLayout>

        4.在MainActivity.java中,编写相关代码。

package lession.example.com.androidlession4_10;

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

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt = (Button) findViewById(R.id.button);
        final ImageView iv = (ImageView) findViewById(R.id.imageView);
        final AnimationDrawable mAd = (AnimationDrawable) iv.getBackground();
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAd.start();
            }
        });
    }
}

        运行结果:

                        在这里插入图片描述

        这就是逐帧动画的使用,如果转载以及CV操作,请务必注明出处,谢谢!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值