个人对andriod frame动画的总结

本文介绍了Android中的帧动画,包括使用AnimationList在XML中定义帧动画的方式,以及通过animation-list在drawable文件夹中声明。讨论了帧动画的属性如oneshot、duration,并提供了官方示例代码中如何控制动画的开始和停止。此外,提到了帧动画的另一种写法和封装,并链接到相关文章。同时,文章提及了属性动画的animatorset和playTogether概念。

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

          最近项目中到处都用到了动画 所以来总结下吧 众所周知 andriod中有tween动画和帧动画 也就是frame动画 

这两者使用的频率都非常之高 这里我介绍一个非常好的学习动画的项目叫做baseAnimati    s.play(anim4).after(anim3);

on 这个apk已经出到了1.3版 开发者总结了现在市面上各种可以用到的动画效果 

有的很炫 推荐各位看看

 接着说上面的 frame动画 现在的frame动画一般来说有两种方式可以实现

第一种的核心就是一个animationList  这个追其原点要看官方的文档 

Drawable Animation

Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. TheAnimationDrawable class is the basis for Drawable animations.

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it's more simply accomplished with a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

The XML file consists of an <animation-list> element as the root node and a series of child<item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here's an example XML file for a Drawable animation


然而我们发现 官方把这类动画却统称为drawable动画 我们来看看官方的声明(PS 本人的文档可能不是目前谷歌最新的可能会有些许不同)


这里 声明了三类 一类称之为属性动画 一类称之为视图动画 一类是drawable动画 

这可能和我们最初的两种认识有些大相径庭 来看看其他两种 

属性动画中却主声明了三个animator (本人称之为动画演播者)

ValueAnimator ObjectAnimator AnimatorSet 

先看我们属性的最后一个 注意不是animationset而是 animatorset 我们看下这个animatorset

Provides a mechanism to group animations together so that they run in relation to one another. You can set animations to play together, sequentially, or after a specified delay.

这里 有一个关键词是playtogether 这个词不是一起玩 而是播放到一起 

而我们看这个


这里有一个builder 这个builder 我们点进去 

  AnimatorSet s = new AnimatorSet();
     s.play(anim1).with(anim2);
     s.play(anim2).before(anim3);
     s.play(anim4).after(anim3);

这里的确是找到了animationset的雏形 它可以设置同时播放什么 也可以设置之后播放什么 

我发现 再讲下去就跑题了 有关话题咱下次再讲。


关于帧动画官方让我们声明一个animation-list 注意这个animation-list是在drawable文件夹里


<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true"> 
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 
</animation-list>

这里最常用的三个属性 oneshot 就是说是否循环播放 true表示不循环 (一次播放) false表示循环 

引用的drawable 传递的就是其每帧的图片 duration就是动画周期 

我们再看看官方的demo

<pre name="code" class="java">AnimationDrawable rocketAnimation;  
public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);   
setContentView(R.layout.main);    
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);   
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);   
rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
}  
public boolean onTouchEvent(MotionEvent event) { 
  if (event.getAction() == MotionEvent.ACTION_DOWN) {    
 rocketAnimation.start();   
  return true;   
}   
return super.onTouchEvent(event); 
}


 

这里代码里 我们发现得到animationDrawable 就可以调用start方法和stop方法就能控制整个动画的起停

然而这里的帧动画有第二种写法

主要的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/ib_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
	    android:text="开始" />
    
    <Button
        android:layout_below="@id/ib_start"
        android:id="@+id/ib_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
	    android:text="停止" />
    
    	
    	<ImageView 
    	    android:layout_toRightOf="@id/ib_start"
    	    android:id="@+id/iv"
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    android:src="@drawable/ani"
    	    />
    
</RelativeLayout>

这里的ani就是在res中声明过的动画文件ani

package com.akira.animdemo2;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView iv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		iv = (ImageView) findViewById(R.id.iv);
		Button bt_start = (Button) findViewById(R.id.ib_start);
		Button bt_stop = (Button) findViewById(R.id.ib_start);
		bt_start.setOnClickListener(this);
		bt_start.setOnClickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_start:
			startAnimation();
			break;
		case R.id.ib_stop:
			stopAnimation();
			break;
			
		default:
			break;
		}
		
	}
	/**
	 * 动画停止
	 */
	private void stopAnimation() {
		AnimationDrawable aniDraw = (AnimationDrawable) iv.getDrawable();//这里要声明成animationDrawable
		aniDraw.stop();
	}
	/**
	 * 动画开始
	 */
	private void startAnimation() {
		iv.setImageResource(R.drawable.ani);
		AnimationDrawable aniDraw = (AnimationDrawable) iv.getDrawable();//这里要声明成animationDrawable
		aniDraw.start();
	}

}

这里调用animationDrawable的开启和结束就好了 

第三种就是 上一篇文章andriod帧动画的个人封装

http://blog.youkuaiyun.com/mtgodd/article/details/38874537


ps:有人想到了暂停 这里本人还没有想出 如果有大牛以前解决过还望指点一二 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值