Android动画之逐帧动画(Frame Animation)基础学习

所属分类: 软件编程 / Android 阅读数: 36
收藏 0 赞 0 分享

前言

在Android中,动画Animation的实现有两种方式:Tween Animation(补间动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。

下面我们就来学习下Android中逐帧动画的基础知识。

原理 : 人眼的"视觉暂留"

方式 :

     1.在java代码中 ( new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);)

         sdk好像要求最低版本必须>=21

     2.在XML文件中定义动画资源

效果图如下

代码

1.准备图片资源

将图片资源放在drawable-hdpi目录下

2.在drawable目录下新建animation-list类型文件

anim_frame.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/a" android:duration="100"/>
 <item android:drawable="@drawable/b" android:duration="100"/>
 <item android:drawable="@drawable/c" android:duration="100"/>
</animation-list>

3.在布局文件中,添加ImageView,将其background属性设置为动画资源xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button
 android:id="@+id/btn_start"
 android:text="开始跳舞"
 android:textSize="25sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
 <Button
 android:id="@+id/btn_stop"
 android:text="结束跳舞"
 android:textSize="25sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

 <ImageView
 android:id="@+id/image"
 android:background="@drawable/anim_frame"
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>
</LinearLayout>

4.在java中,获取动画资源,调用start( )开启动画,stop( )停止动画

package com.lyp.frameanim;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
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 implements View.OnClickListener{
 private Button mBtnStart;
 private Button mBtnStop;
 private ImageView mImage;
 private AnimationDrawable mAnim;

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

// new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);

 initView();
 mAnim = (AnimationDrawable) mImage.getBackground();
 }

 private void initView() {
 mBtnStart= (Button) findViewById(R.id.btn_start);
 mBtnStop= (Button) findViewById(R.id.btn_stop);
 mImage= (ImageView) findViewById(R.id.image);

 mBtnStart.setOnClickListener(this);
 mBtnStop.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_start:
  mAnim.start();
  break;
  case R.id.btn_stop:
  mAnim.stop();
  break;
 }
 }
}

以上就是这篇文章的全部内容了,希望能对各位Android开发者们有所帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多