Animator动画

请先从网上下载nineOldAndroids这个jar包.

import org.iaiai.alpha.core.activity.BaseActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

@ContentView(R.layout.main)
public class StartActivity extends BaseActivity {
	
	@ViewInject(R.id.moveLayout)
	private View moveLayout;
	
	@ViewInject(R.id.rootLayout)
	private View rootLayout;
	
	@ViewInject(R.id.hiddenLayout)
	private View hiddenLayout;
	
	@ViewInject(R.id.hiddenTextView)
	private TextView hiddenTextView;
	
	private int mActivePointerId,mLastMotionY;

	@OnClick(R.id.colorLayout)
	public void onColorLayoutOnClick(View view){
		ValueAnimator colorAnim = ObjectAnimator.ofInt(moveLayout, "backgroundColor", /*Blue*/0xff0000ff, /*Red*/0xffff0000);
		colorAnim.setDuration(3000);
		colorAnim.setEvaluator(new ArgbEvaluator());
		colorAnim.setRepeatCount(ValueAnimator.INFINITE);
		colorAnim.setRepeatMode(ValueAnimator.REVERSE);
		colorAnim.start();
	}
	
	@OnClick(R.id.moveLayoutLayout)
	public void onMoveLayoutLayoutOnClick(View view){
		AnimatorSet animationSet = new AnimatorSet();
		
		ObjectAnimator animator1 = ObjectAnimator.ofFloat(moveLayout, "y", 0, 100);
		animator1.setInterpolator(new AccelerateInterpolator());	//在动画开始的地方速率改变比较慢,然后开始加速
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(moveLayout, "scaleX", 1, 0);
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(moveLayout, "scaleY", 1, 0);
		
		ObjectAnimator animator4 = ObjectAnimator.ofFloat(moveLayout, "y", 100, 0);
		animator4.setInterpolator(new DecelerateInterpolator());	//在动画开始的地方快然后慢
		ObjectAnimator animator5 = ObjectAnimator.ofFloat(moveLayout, "scaleX", 0, 1);
		ObjectAnimator animator6 = ObjectAnimator.ofFloat(moveLayout, "scaleY", 0, 1);
		animationSet.playTogether(animator1,animator2,animator3);
		animationSet.play(animator4).after(animator1);
		animationSet.play(animator5).after(animator1);
		animationSet.play(animator6).after(animator1);
		
		animationSet.setDuration(500);
		animationSet.start();
	}
	
	@OnClick(R.id.flipVerticalLayout)
	public void onFlipVerticalOnClick(View view){
		ObjectAnimator.ofFloat(view, "rotationY", 0.0F, 360.0F).setDuration(500).start();
	}
	
	@OnClick(R.id.flipHorizontalLayout)
	public void onFlipHorizontalOnClick(View view){
		ObjectAnimator.ofFloat(view, "rotationX", 0.0F, 360.0F).setDuration(500).start();
	}
	
	@OnClick(R.id.hiddenTextView)
	public void onHiddenTextViewOnClick(View view){
		AnimatorSet animationSet = new AnimatorSet();
		
		ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(hiddenLayout, "translationY", 0.0F, 250F);
		translationAnimator.setDuration(2000);
		
		ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(hiddenLayout, "alpha", 1, 0);
		alphaAnimator.setDuration(500);
		
		animationSet.playTogether(translationAnimator,alphaAnimator);
		animationSet.addListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
			}
			@Override
			public void onAnimationRepeat(Animator animation) {
			}
			@Override
			public void onAnimationEnd(Animator animation) {
				hiddenTextView.setVisibility(View.GONE);
			}
			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
		animationSet.start();
		
//		AnimationSet animationSet = new AnimationSet(true);
//		
//		//透明度
//		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//		alphaAnimation.setDuration(1000);
//		animationSet.addAnimation(alphaAnimation);
//		
//		// 放缩
//		ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0, 1,0, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
//		scaleAnimation.setDuration(1000);
//		animationSet.addAnimation(scaleAnimation);
//		
//		// 旋转
//		RotateAnimation rotateAnimation = new RotateAnimation(0, 180,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
//		rotateAnimation.setDuration(1000);
//		animationSet.addAnimation(rotateAnimation);
//		
//		// 平移
//		TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT, 0f,Animation.RELATIVE_TO_PARENT, 1.0f);
//		translateAnimation.setDuration(1000);
//		animationSet.addAnimation(translateAnimation);
//		
//		//以下两个属性设置动画之后显示在停止位置上
//		animationSet.setFillEnabled(true);
//		animationSet.setFillAfter(true);
//		animationSet.setAnimationListener(new AnimationListener() {
//			@Override
//			public void onAnimationStart(Animation animation) {
//			}
//			@Override
//			public void onAnimationRepeat(Animation animation) {
//			}
//			@Override
//			public void onAnimationEnd(Animation animation) {
//				hiddenTextView.setVisibility(View.GONE);
//			}
//		});
//		
//		hiddenLayout.startAnimation(animationSet);
	}
	
	@OnClick(R.id.showTextView)
	public void onShowTextViewOnClick(View view){
		AnimationSet animationSet = new AnimationSet(true);
		
		//透明度
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(1000);
		animationSet.addAnimation(alphaAnimation);
		
		// 放缩
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0,1, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(1000);
		animationSet.addAnimation(scaleAnimation);
		
		// 旋转
		RotateAnimation rotateAnimation = new RotateAnimation(180, 0,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
		rotateAnimation.setDuration(1000);
		animationSet.addAnimation(rotateAnimation);
		
		// 平移
		TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT, 1.0f,Animation.RELATIVE_TO_PARENT, 0f);
		translateAnimation.setDuration(1000);
		animationSet.addAnimation(translateAnimation);
		
		//以下两个属性设置动画之后显示在停止位置上
		animationSet.setFillEnabled(true);
		animationSet.setFillAfter(true);
		animationSet.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				hiddenTextView.setVisibility(View.VISIBLE);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
			}
		});
		
		hiddenLayout.startAnimation(animationSet);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		moveLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent ev) {
				final int action = ev.getAction();
				switch (action & MotionEvent.ACTION_MASK) {
		            case MotionEvent.ACTION_MOVE: {
		            	move(v,ev);
		            	break;
		            }
		            case MotionEvent.ACTION_DOWN: {
		            	mActivePointerId = ev.getPointerId(0);
		            	mLastMotionY = (int) ev.getY(mActivePointerId);
		            	break;
		            }
		            case MotionEvent.ACTION_CANCEL:
		            case MotionEvent.ACTION_UP:
		            	mActivePointerId = -1;
		            	break;
				}
				
				return true;
			}
		});
	}
	
	private void move(View v, MotionEvent ev){
		RelativeLayout.LayoutParams params = (LayoutParams) v.getLayoutParams();
		
		int dy = (int) ev.getY(mActivePointerId) - mLastMotionY;
		int t = params.topMargin+dy;
		
//		/***********这种方式移动会有个问题,当再有其它组件移动时会把原来用此方式移动的所有组件还原到原来的初始位置,也就是用layout没有改变真正的坐标*****************/
//		int dy = (int) ev.getY(mActivePointerId) - mLastMotionY;
//        int l = 0;
//        int t = v.getTop()+dy;
//        int r = v.getRight();
//        int b = v.getBottom() + dy;
//        
////    	v.layout(l, t, r, b);
//    	Log.i("***","l:"+l+"___t:"+t+"___r:"+r+"___b:"+b);	//这种移动
//    	/*****************************/
    	
    	if(t<0){
        	params.topMargin = 0;
        	v.setLayoutParams(params);
    		return;
    	}
    	if(t>(rootLayout.getHeight()-v.getHeight())){
        	params.topMargin = rootLayout.getHeight()-v.getHeight();
        	v.setLayoutParams(params);
    		return;
    	}
    	params.topMargin = t;
    	v.setLayoutParams(params);
	}

}

猜你喜欢

转载自iaiai.iteye.com/blog/2153056
今日推荐