【Android高级】安卓各种动画使用方法详解

时间:2021-11-09 22:36:29

一、概述

Android提供了几种动画类型:View AnimationDrawable AnimationProperty Animation

ViewAnimation相当简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性。

Drawable Animation就是所谓的帧动画,一般作为Drawable的背景使用。

PropertyAnimation产生的原因,你希望View有一个颜色的切换动画;你希望可以使用3D旋转动画;你希望当动画停止时,View的位置就是当前的位置;这些View Animation都无法做到。

二、使用方法

(1View Animation(一个组件的动画)

     a.采用定义xml动画,可以在一个set资源里面定义多种效果,使用这种方法时要注意里面的坐标的使用,是以当前组件的左上边为原点。可以使用X%p这种定义与父布局的关系。在代码中加载时用AnimationUtils.loadAnimation(context, id);的方法得到动画并用组件的huoJianImageView.setAnimation();方法设置动画,启动动画时可以用得到的animation启动,也可以用组件的start的方法。

     b.采用java代码生成动画,TranslateAnimationAlphaAnimation。生成动画以后的设置方法如前。

 2Drawable Animation(连续变化的动画)

     Drawable里面定义animation-list然后作为一个组件的背景,在java代码中得到

AnimationDrawable animationDrawable = (AnimationDrawable) bullImageView.getBackground();

     然后启动 animationDrawable.start()

     一般使用这种方法需要的注意的是控制该动画的最后一帧播放完毕,则组件消失。

     可自定义一个ImageView代码如下所示:

 

package com.animal.bull;

import java.lang.reflect.Field;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class MyImage extends ImageView {
AnimationDrawable animationDrawable;
int totalFrame;
Field field;

public void setAnimationDrawable(AnimationDrawable animationDrawable) {
this.animationDrawable = animationDrawable;
totalFrame = animationDrawable.getNumberOfFrames();
}

public MyImage(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
try {
if (animationDrawable != null) {
field = AnimationDrawable.class.getDeclaredField("mCurFrame");
field.setAccessible(true);
int r = field.getInt(animationDrawable);
if (r == totalFrame - 1) {
setVisibility(View.INVISIBLE);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

super.onDraw(canvas);
}
}

 3Property Animation(属性动画)

     a.不采用xmlhttp://blog.csdn.net/lmj623565791/article/details/38067475

       1ObjectAnimator

             ObjectAnimator.ofFloat(imageView, "rotationX",0.0F, 360.0F).setDuration(500) .start();

       2ValueAnimator

public void paowuxian() {
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0),new PointF(w, h));
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
// fraction = t / duration
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
// Log.e(TAG, fraction * 3 + "");
// x方向200px/s ,则y方向0.5 * 10 * t
PointF point = new PointF();
point.x = (w-imageView.getWidth())/(3) * fraction * 3;
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
return point;
}
});

valueAnimator.start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF point = (PointF) animation.getAnimatedValue();
imageView.setX(point.x);
imageView.setY(point.y);

}
});
}
     b. 采用 xml。(http://blog.csdn.net/lmj623565791/article/details/38092093

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together" >

<objectAnimator
android:duration="3000"
android:propertyName="y"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueTo="400"
android:valueType="intType" >
</objectAnimator>
<objectAnimator
android:duration="3000"
android:propertyName="x"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueTo="200"
android:valueType="intType" >
</objectAnimator>

</set>

class move {
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;

imageView.layout(x, imageView.getTop(),
x + imageView.getMeasuredWidth(), imageView.getBottom());
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
imageView.layout(imageView.getLeft(), y, imageView.getRight(), y
+ imageView.getMeasuredHeight());
}

int x;
int y;
}

 AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater
.loadAnimator(getApplicationContext(),
R.animator.ani_sx);
animatorSet.setTarget(mo);
animatorSet.start();

 布局动画:

public class LayoutAniAct extends Activity implements OnCheckedChangeListener {

CheckBox born_It;
CheckBox born_Pa;
CheckBox die_It;
CheckBox die_Pa;
GridLayout gridView;
Button button;
LayoutTransition layoutTransition;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutani);
born_It = (CheckBox) findViewById(R.id.born_It);
born_Pa = (CheckBox) findViewById(R.id.born_Pa);
die_It = (CheckBox) findViewById(R.id.die_It);
die_Pa = (CheckBox) findViewById(R.id.die_Pa);
gridView = (GridLayout) findViewById(R.id.gridView1);
button = (Button) findViewById(R.id.button1);
layoutTransition = new LayoutTransition();
gridView.setLayoutTransition(layoutTransition);
die_It.setOnCheckedChangeListener(this);
die_Pa.setOnCheckedChangeListener(this);
born_It.setOnCheckedChangeListener(this);
born_Pa.setOnCheckedChangeListener(this);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Button button = new Button(getApplicationContext());
button.setText((++n) + "");
gridView.addView(button, Math.min(1, gridView.getChildCount()));
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
gridView.removeView(button);
}
});
}
});
}

int n = 0;

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
layoutTransition = new LayoutTransition();
layoutTransition.setAnimator(
LayoutTransition.APPEARING,
born_It.isChecked() ? layoutTransition
.getAnimator(LayoutTransition.APPEARING) : null);
layoutTransition.setAnimator(
LayoutTransition.CHANGE_APPEARING,
born_Pa.isChecked() ? layoutTransition
.getAnimator(LayoutTransition.CHANGE_APPEARING) : null);
layoutTransition.setAnimator(
LayoutTransition.DISAPPEARING,
die_It.isChecked() ? layoutTransition
.getAnimator(LayoutTransition.DISAPPEARING) : null);
layoutTransition.setAnimator(
LayoutTransition.CHANGE_DISAPPEARING,
die_Pa.isChecked() ? layoutTransition
.getAnimator(LayoutTransition.CHANGE_DISAPPEARING)
: null);
gridView.setLayoutTransition(layoutTransition);

}
}