最近项目中到处都用到了动画 所以来总结下吧 众所周知 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:有人想到了暂停 这里本人还没有想出 如果有大牛以前解决过还望指点一二