飞行的不知名小鸟

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ancientear/article/details/82760535

本实例实现时,主要用到了逐帧动画和平移补间动画。
实现过程
(1)在新建项目的res目录中,在drawable的目录创建一个fly.xml资源文件,在该文件中定义一个小鸟飞行动作的图画,该动画由两帧组成,也就是由两个预先定义好的图片组成,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable = "@drawable/bard1" android:duration = "60"/>
    <item android:drawable = "@drawable/bard2" android:duration = "60"/>

</animation-list>

(2)在res中,创建一个名为anim的目录,并创建名称为move.xml的XML资源文件,在该文件中定义一个实现小鸟飞行的补间动画,该动画为在水平方向上向右向上平移300像素,持续时间为3秒钟。

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

(3)修改新建项目的res/layout目录下的布局文件main.xml,将默认添加的TextView组件删除,然后在默认添加的线性布局管理器中添加一个ImageView组件,并设置该组件的背景逐帧动画资源fly:

<?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"
    android:id="@+id/ll"
    android:orientation="vertical"
    tools:context="com.example.shenfan.bard.MainActivity">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fly"/>

</LinearLayout>

(4)打开默认创建的MainActivity,在onCreat()方法中,首先获取布局文件中的线性布局管理器及ImageView组件,然后获取动画资源,最后,为线性布局添加单击监听事件,该监听事件中,播放动画,从而实现小鸟飞行的效果:

package com.example.shenfan.bard;

import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.animation.Animation;
public class MainActivity extends AppCompatActivity {

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

        LinearLayout ll = (LinearLayout)findViewById(R.id.ll);//获取布局文件中添加的线性布局管理器
        final ImageView iv = (ImageView)findViewById(R.id.imageView1);
        final Animation move = AnimationUtils.loadAnimation(this,R.anim.move);
        final AnimationDrawable anim = (AnimationDrawable)iv.getDrawable();//获取AnimationDrawable对象

        //为线性布局管理器添加单击事件监听器
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anim.start();//开始播放动画
                iv.setAnimation(move);
            }
        });
    }
}

运行结果如下图:
在这里插入图片描述

可下载:
https://download.csdn.net/download/ancientear/10674541

猜你喜欢

转载自blog.csdn.net/ancientear/article/details/82760535
今日推荐