码迷,mamicode.com
首页 > 移动开发 > 详细

Android 一般动画animation和属性动画animator

时间:2015-01-28 09:50:24      阅读:1927      评论:0      收藏:0      [点我收藏+]

标签:android   java   

package com.example.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
	}
    
	public void click(View view){
		Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
	} 
	
	public void move(View view ){
		float fromXDelta=0;
		float toXDelta=0;
		float fromYDelta=0;
		float toYDelta=200;
		TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
		/**
		 * time
		 */
		animation.setDuration(1000);
		animation.setFillAfter(true);
		ImageView imageView=(ImageView)findViewById(R.id.imageView);
		imageView.startAnimation(animation);
	}
     
}

这是一般动画,再看属性动画

区别:一般动画变换后只是图片移动了,view的位置不变,然而属性动画view随着图片移动。

	public void move(View view ){

		ImageView imageView=(ImageView)findViewById(R.id.imageView);
		ObjectAnimator.ofFloat(imageView, "translationY", 0f,200f).setDuration(1000).start();
	}

多种动画同时进行

	public void move(View view ){

		ImageView imageView=(ImageView)findViewById(R.id.imageView);
		ObjectAnimator.ofFloat(imageView, "translationY", 0f,200f).setDuration(1000).start();
		ObjectAnimator.ofFloat(imageView, "translationX", 0f,200f).setDuration(1000).start();
		ObjectAnimator.ofFloat(imageView, "rotation", 0,360f).setDuration(1000).start();
	}
Google提供了一种更节省系统资源的多种动画同时播放

	public void move(View view ){

		ImageView imageView=(ImageView)findViewById(R.id.imageView);

		PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation", 0,360f);
		PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX", 0f,200f);
		PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY", 0f,200f);
		ObjectAnimator.ofPropertyValuesHolder(imageView, p1,p2,p3).setDuration(1000).start();
	}
同时Google提供了animatorset,允许多种不同动画按照用户要求播放,如使用set.palyTpgether()   set.playSequentially()

	public void move(View view ){

		ImageView imageView=(ImageView)findViewById(R.id.imageView);


		ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView, "rotation", 0,360f);
		ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView, "translationX", 0,200f);
		ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView, "translationY", 0,200f);
	
		AnimatorSet set=new AnimatorSet();
		//set.playTogether(animator1,animator2,animator3);
		set.playSequentially(animator1,animator2,animator3);
		set.setDuration(1000);
		set.start();
	}





Android 一般动画animation和属性动画animator

标签:android   java   

原文地址:http://blog.csdn.net/u010710760/article/details/43204677

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!