Android 帧动画 的实现

1、帧动画

在这里插入图片描述

2、文件结构

在这里插入图片描述

1)activity_main 定义一个 img ,两个 按钮
2)frameanimation.xml 文件 加载的自定义的 动画文件
3)ManiActivity 文件 主要功能实现地方

3、activity_main.xml 文件

<?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:orientation="vertical"
    tools:context="com.example.lum.myapplication.MainActivity">

<ImageView
    android:id="@+id/img_id"
    android:layout_width="100dp"
    android:layout_height="100dp" />

    <Button
        android:id="@+id/start_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"/>


    <Button
        android:id="@+id/stop_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"/>
</LinearLayout>

4、frameanimation.xml 文件 自定义的 动态文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/test" android:duration="500"/>
    <item android:drawable="@drawable/timg" android:duration="500"/>
    <item android:drawable="@drawable/test" android:duration="500"/>
    <item android:drawable="@drawable/timg" android:duration="500"/>
    <item android:drawable="@drawable/test" android:duration="500"/>
    <item android:drawable="@drawable/timg" android:duration="500"/>
    <item android:drawable="@drawable/test" android:duration="500"/>
    <item android:drawable="@drawable/timg" android:duration="500"/>
    <item android:drawable="@drawable/test" android:duration="500"/>
    <item android:drawable="@drawable/timg" android:duration="500"/>


</animation-list>

5、ManiActivity 文件

package com.example.lum.myapplication;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG = "MainActivity: ";
    private AnimationDrawable animationDrawable;
    private Button buttonStart,buttonStop;

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

        buttonStart = (Button) findViewById(R.id.start_id);
        buttonStop = (Button) findViewById(R.id.stop_id);
        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);

        //获取显示动画 view
        ImageView image = (ImageView) findViewById(R.id.img_id);
        //设置图片背景为动画
        image.setBackgroundResource(R.anim.frameanimation);
        //获取 刚设置的 背景Drawable 转化为 AnimationDrawable 对象
        animationDrawable = (AnimationDrawable) image.getBackground();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_id:
                animationDrawable.start();
                break;
            case R.id.stop_id:
                animationDrawable.stop();
                break;
                default:
                    break;
        }
    }
}

当 ImageView 的背景 被设置为Frame Animation 动画 后,可以将ImageView 的背景

文章参考:
《Android 典型技术模块开发详解》

本人郑重声明,本博客所编文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/84553279