android动画


 Animation: 不管怎么变换,只有原始位置会响应事件,新位置不会响应事件
         *
属性动画: 变换后的位置可以响应事件,原来的位置不再响应事件


//自定义Animtion
				MyAnimation animation=new MyAnimation();
				v.startAnimation(animation);

package com.example.androidtest;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;

public class MyAnimation extends Animation {
 	
	private int centerX;
	private int centerY;
	//用来修改动画变换矩阵
	private Camera camera;
	
	private final int  rotateY=300;
	
	//可以在这初始化
	@Override
	public void initialize(int width, int height, int parentWidth, int parentHeight) {
		// TODO Auto-generated method stub
		super.initialize(width, height, parentWidth, parentHeight);
		
		setDuration(3000);
		
		setFillAfter(true);
		
		setInterpolator(new LinearInterpolator());
		
		centerX=parentWidth/2;
		
		centerY=parentHeight/2;
		
		camera=new Camera();
	}
	
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		// TODO Auto-generated method stub
		Matrix matrix=t.getMatrix();

		camera.save();
		
		camera.rotateY(interpolatedTime*rotateY);
		//把旋转变换作用到矩阵上
		camera.getMatrix(matrix);
		
		camera.restore();
		
		//下面两行代码改变旋转时的默认旋转中心
		matrix.preTranslate(centerX, centerY);
		matrix.postTranslate(-centerX, -centerY);
		

	}

}


	CircleView civ=(CircleView) findViewById(R.id.civ);
		//属性动画方式1
		PropertyValuesHolder pvh1=PropertyValuesHolder.ofFloat("translationX", 300);
		PropertyValuesHolder pvh2=PropertyValuesHolder.ofFloat("scaleX", 1,0,1f);
		PropertyValuesHolder pvh3=PropertyValuesHolder.ofFloat("scaleY", 1,0,1);
		
		final ObjectAnimator anim=ObjectAnimator.ofPropertyValuesHolder(civ, pvh1,pvh2,pvh3);
		anim.setDuration(3000).start();
		
		anim.addListener(new AnimatorListenerAdapter() {
			
			 @Override
			public void onAnimationEnd(Animator animation) {
				
				 
			}
			
		});
//
		//属性动画方式2
		CircleView civ2=(CircleView) findViewById(R.id.civ2);
		// 3.0之后才有animate()方法
		civ2.animate().alpha(0.5f).rotationY(360).translationX(100)
			.setDuration(3000).scaleX(0.5f).scaleY(0.5f)
			.withEndAction(new Runnable() {
				
				@Override
				public void run() {
					
				}
			}).start();

	
				
		
		/*
		 * 布局动画
		 *   
		 *   在给ViewGroup中addView时添加的一个动画过度效果(每一个子控件都会执行,可以选择执行顺序)。
		 *   最简单的方式是在布局文件中的父布局中添加如下代码 
		 *   android:animateLayoutChanges="true"   
		 *   不过这种方式是安卓默认的效果,无法改变
		 *   
		 *   可以通过如下方式来改变效果
		 */
		final ViewGroup viewGroup=(ViewGroup) findViewById(R.id.container);
		ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1); //x方向从0到1   y方向从0到1
		sa.setDuration(5000);
		
		//0.5   每个子控件执行动画的延时
		final LayoutAnimationController lac=new LayoutAnimationController(sa,0.5f);
		/*
		 * LayoutAnimationController.ORDER_NORMAL  顺序执行
		 * LayoutAnimationController.ORDER_RANDOM  随机执行
		 * LayoutAnimationController.ORDER_REVERSE  反序执行
		 * 
		 */
		lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
		
		//调用该方法后会执行动画(每个子控件都会执行)
		viewGroup.setLayoutAnimation(lac);
		
		
		civ.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ImageView child=new ImageView(getApplicationContext());
				child.setImageResource(R.drawable.pic);
				// TODO Auto-generated method stub
				viewGroup.addView(child);
				//
				viewGroup.setLayoutAnimation(lac);

			}
		});
		


你可能感兴趣的:(android)