动画集内包含自定义的一个动画,和一个缩放动画。
?
这个是页面
package com.nico; import android.app.Activity; import android.os.Bundle; import android.view.animation.AnimationSet; import android.view.animation.ScaleAnimation; import android.widget.ImageView; public class AnimationActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView img = (ImageView) findViewById(R.id.img); AnimationSet ani = new AnimationSet(false); ani.addAnimation(new MoveAnimation()); ScaleAnimation scale = new ScaleAnimation(1.5f, 1.0f, 1.5f, 1.0f); scale.setDuration(5000); scale.setFillAfter(true); ani.addAnimation(scale); ani.setDuration(5000); ani.setFillAfter(true); // ani.start(); img.startAnimation(ani); ani.start(); } }
?这个是自定义动画:
package com.nico; import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.Transformation; public class MoveAnimation extends Animation { private int halfWidth; private int halfHeight; private Camera camera; @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); setDuration(5000); setFillAfter(true); halfWidth = width / 2; halfHeight = height / 2; setInterpolator(new LinearInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { camera = new Camera(); camera.save(); final Matrix matrix = t.getMatrix(); System.out.println("------------------->" + interpolatedTime); camera.translate((100.0f * interpolatedTime) , (150.0f * interpolatedTime - 150) , 0.0f); camera.rotateY(45 * (interpolatedTime)); camera.getMatrix(matrix); // matrix.preScale(interpolatedTime, interpolatedTime); // matrix.preRotate(interpolatedTime * 360); matrix.preTranslate(-halfWidth, -halfHeight); matrix.postTranslate(halfWidth, halfHeight); camera.restore(); } }
?