<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="start" />
<ImageView
android:id="@+id/id_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/start"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
package com.example.xx;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView mBallView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBallView = (ImageView) findViewById(R.id.id_ball);
Button startBtn = (Button) findViewById(R.id.start);
startBtn.setOnClickListener(this);
}
public void rotateyAnimRun() {
// // 通过静态方法构建一个ObjectAnimator对象
// // 设置作用对象、属性名称、数值集合
// ObjectAnimator.ofFloat(mBallView, "translationX", 0.0F, 500F, 300.0F)
// // 设置执行时间(1000ms)
// .setDuration(2000)
// // 开始动画
// .start();
// ObjectAnimator.ofFloat(mBallView, "rotationX", 0.0F, 180.0F, 95.0F)
// .setDuration(4000).start();
// // 修改view的y属性, 从当前位置移动到300.0f
// ObjectAnimator yBouncer = ObjectAnimator.ofFloat(mBallView, "y",
// 1500, 300.0f);
// yBouncer.setDuration(1500);
// // 设置插值器(用于调节动画执行过程的速度)
// yBouncer.setInterpolator(new BounceInterpolator());
// // 设置重复次数(缺省为0,表示不重复执行)
// yBouncer.setRepeatCount(1);
// // 设置重复模式(RESTART或REVERSE),重复次数大于0或INFINITE生效
// yBouncer.setRepeatMode(ValueAnimator.REVERSE);
// // 设置动画开始的延时时间(200ms)
// yBouncer.setStartDelay(200);
// // 开始动画
// yBouncer.start();
// 通过静态方法构建一个ValueAnimator对象
// 设置数值集合
ValueAnimator animator = ValueAnimator.ofFloat(0f, 200.0f);
// 设置作用对象
animator.setTarget(mBallView);
// 设置执行时间(1000ms)
animator.setDuration(1000);
// 添加动画更新监听
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 获取当前值
Float mValue = (Float) animation.getAnimatedValue();
// 设置横向偏移量
mBallView.setTranslationX(mValue);
// 设置纵向偏移量
mBallView.setTranslationY(mValue);
}
});
// 开始动画
animator.start();
// // 获取view左边位置
// int left = mBallView.getLeft();
// // 获取view右边位置
// int right = mBallView.getRight();
// // 将view左边增加10像素
// PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", left,
// left + 10);
// // 将view右边减少10像素
// PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right",
// right, right - 10);
// // 在X轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f
// PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX",
// 1f, 0f, 1f);
// // 在Y轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f
// PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY",
// 1f, 0f, 1f);
// // 将PropertyValuesHolder交付给ObjectAnimator进行构建
// ObjectAnimator customAnim = ObjectAnimator.ofPropertyValuesHolder(
// mBallView, pvhLeft, pvhRight, pvhScaleX, pvhScaleY);
// // 设置执行时间(1000ms)
// customAnim.setDuration(1000);
// // 开始动画
// customAnim.start();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
rotateyAnimRun();
break;
}
}
}