- package com.demo;
- 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 MyAnimation extends Animation {
- private int parentWidth;
- private int parentHeight;
- private float objectWidth;
- private float objectHeight;
- private Camera camera;
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t) {
- // TODO Auto-generated method stub
- super.applyTransformation(interpolatedTime, t);
- Matrix matrix=t.getMatrix();
- camera.save();
- camera.getMatrix(matrix); //必须写在preTranslate postTranslate方法之前否则达不到想要的效果
- matrix.preTranslate(0,0); //动画起始点,默认左上角
- matrix.postTranslate((parentWidth-objectWidth)*interpolatedTime, (parentHeight-objectHeight)*interpolatedTime);
- /*
- matrix.preTranslate(parentWidth/2,parentHeight/2);
- matrix.postTranslate((parentWidth/2-objectWidth)*interpolatedTime, (parentHeight/2-objectHeight)*interpolatedTime);
- */
- camera.restore();
- }
- @Override
- public void initialize(int width, int height, int parentWidth, //width,height表目标组件的尺寸,
- int parentHeight) { //parentWidth,parentHeight表目的组件的父组件的尺寸
- // TODO Auto-generated method stub
- super.initialize(width, height, parentWidth, parentHeight);
- setDuration(5000);
- setFillAfter(false); //设置是否保存动画后的位置,false表示动画后恢复到初始位置
- objectHeight=height;
- objectWidth=width;
- this.parentWidth=parentWidth;
- this.parentHeight=parentHeight;
- camera=new Camera();
- setInterpolator(new LinearInterpolator()); //设置动画播放速率
- }
- }
转载于:https://blog.51cto.com/randino/841044