您的位置:首页 > 移动开发 > Android开发

关于Android中的的动画

2015-11-02 20:56 351 查看
动画有以下几种:

补间动画

帧动画

属性动画 API 11 Android3.0及以上

布局动画

补间动画特点:

补间动画改变的并不是视图的本身,改变的仅仅是视图的影像。

使用补间动画时,应该让动画的变化方向是视图本身所处的位置

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="200"
android:toXDelta="0"
android:duration="1000"
/>

</set>


属性动画

属性动画改变的是视图本身真正的属性

用xml文件创建属性动画:

res/animator:
<objectAnimator></>


指定要改变的属性名称 propertyName

起始值 ValueFrom

终止值 ValueTo

动画时长 Duration

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="200"
android:duration="1000"
>
</objectAnimator>


用代码创建属性动画:

ObjectAnimator.ofFlot(目标组件,

属性名称,起始值,终止值

);

设定时长 setDuration

启动动画 start

ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "rotation", 0,200);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "x", 0,200);
final ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv, "y", 0,200);
animator.setDuration(1000);
animator2.setDuration(1000);
animator3.setDuration(1000);
animator.start();
animator2.start();


多个属性动画是并行执行的。如果希望控制

多个属性动画的执行顺序,可以使用

1)监听器

animator2.addListener(new AnimatorListener() {

@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
animator3.start();
}

@Override
public void onAnimationCancel(Animator animation) {

}
});


2)属性动画集合 AnimatorSet

AnimatorSet set = new AnimatorSet();
set.play(animator).with(animator2).before(animator3);
set.start();


帧动画

用xml文件创建帧动画:

res/drawable:

<animation-list></>


将一帧一帧的内容做为item放入,要指定这一帧要显示的图像和这一帧的长度

使用时,要获得AnimationDrawable对象,

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/voice_right"
android:duration="200"
/>
<item
android:drawable="@drawable/voice_right1"
android:duration="200"
/>
<item
android:drawable="@drawable/voice_right2"
android:duration="200"
/>
<item
android:drawable="@drawable/voice_right3"
android:duration="200"
/>

</animation-list>


调用start方法就可以启动帧动画了。

调用stop方法停止帧动画。

public void playFrameAnim(View v) {
ImageView iv2 = (ImageView) findViewById(R.id.iv2);
Drawable drawable = iv2.getDrawable();
AnimationDrawable anim = (AnimationDrawable) drawable;
if (isplaying == false) {
anim.start();
isplaying = true;
} else {
anim.stop();
ispla
af3b
ying = false;
}
}


Activity完整代码如下:

package com.example.animdemo;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
private ImageView iv;
private boolean isplaying;

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

iv = (ImageView) findViewById(R.id.iv);
}

public void showMessage(View v) {
Toast.makeText(this, "我是ImageView", 0).show();
}

public void anim(View v) {
// Animation animation = AnimationUtils.loadAnimation(this,
// R.anim.trans_demo);
// animation.setFillAfter(true);
// iv.setAnimation(animation);

// Animator animator = AnimatorInflater.loadAnimator(this,
// R.animator.object_tran);
// animator.setTarget(iv);
// animator.start();

ObjectAnimator animator = ObjectAnimator
.ofFloat(iv, "rotation", 0, 200);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "x", 0, 200);
final ObjectAnimator animator3 = ObjectAnimator
.ofFloat(iv, "y", 0, 200);
animator.setDuration(1000);
animator2.setDuration(1000);
animator3.setDuration(1000);
//      animator.start();
//      animator2.start();
//      animator2.addListener(new AnimatorListener() {
//
//          @Override
//          public void onAnimationStart(Animator animation) {
//
//          }
//
//          @Override
//          public void onAnimationRepeat(Animator animation) {
//
//          }
//
//          @Override
//          public void onAnimationEnd(Animator animation) {
//              animator3.start();
//          }
//
//          @Override
//          public void onAnimationCancel(Animator animation) {
//
//          }
//      });
AnimatorSet set = new AnimatorSet();
set.play(animator).with(animator2).before(animator3);
set.start();
}

public void playFrameAnim(View v) {
ImageView iv2 = (ImageView) findViewById(R.id.iv2);
Drawable drawable = iv2.getDrawable();
AnimationDrawable anim = (AnimationDrawable) drawable;
if (isplaying == false) {
anim.start();
isplaying = true;
} else {
anim.stop();
isplaying = false;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 动画 布局