属性动画和补间动画的区别是,补间动画只是表面上实现了平移,旋转,渐变,缩放,实际上属性值不变;
属性动画实现平移,旋转,渐变,缩放后,属性值变了
Myjstojava ---- PropertyAnimation
属性动画实现平移,旋转,渐变,缩放后,属性值变了
下面就是测试的例子
代码:
package com.atguigu.propertyanimation;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* 测试属性动画的基本使用
* 属性动画和补间动画的区别是,补间动画只是表面上实现了平移,旋转,渐变,缩放,实际上属性值不变;
* 属性动画实现平移,旋转,渐变,缩放后,属性值变了
* 下面就是测试的例子
*/
public class MainActivity extends Activity {
private ImageView iv_animation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_animation = (ImageView) findViewById(R.id.iv_animation);
iv_animation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
}
});
}
/**
* 补间(视图)动画
* @param v
*/
public void testTweenAnimation(View v) {
TranslateAnimation animation = new TranslateAnimation(0, iv_animation.getWidth(), 0, iv_animation.getHeight());
animation.setDuration(3000);
animation.setFillAfter(true);
iv_animation.startAnimation(animation);
}
private AnimatorSet animatorSet;
/**
* 测试属性动画
*/
public void testPropertyAnimation(View v) {
// x轴上移动
ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv_animation,"translationX",0,iv_animation.getWidth());
// y轴上移动
ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv_animation,"translationY",0,iv_animation.getHeight());
AnimatorSet set = new AnimatorSet();
// 两个动画一起播放
set.playTogether(animator3,animator4);
// 播放时间2秒
set.setDuration(2000);
// 开始播放
set.start();
// //另外一种写法
// iv_animation.animate()
// .translationXBy(iv_animation.getWidth())
// .translationYBy(iv_animation.getWidth())
// .setDuration(2000)
// .setInterpolator(new BounceInterpolator())
// .start();
// ObjectAnimator animator = ObjectAnimator.ofFloat(iv_animation, "translationX", 0,iv_animation.getWidth());
// ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv_animation, "translationY", 0,iv_animation.getHeight());
// AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.setDuration(2000);
// animatorSet.setInterpolator(new BounceInterpolator());
// //两个动画一起播放
// animatorSet.playTogether(animator, animator2);
// //开始播放
// animatorSet.start();
}
public void reset(View v) {
iv_animation.clearAnimation();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="testTweenAnimation"
android:text="测试补间(视图)动画" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="testPropertyAnimation"
android:text="测试属性动画" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="reset"
android:text="重置补间动画" />
<ImageView
android:id="@+id/iv_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/logo" />
</LinearLayout>
源码下载:
Myjstojava ---- PropertyAnimation
http://download.csdn.net/download/zhaihaohao1/10020298