资源文件
<LinearLayout
android:id="@+id/ll_root"
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移动画"
android:onClick="lineAnimator"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放动画"
android:onClick="ScaleAnimator"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度动画"
android:onClick="alphaAnimator"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转动画"
android:onClick="circleAnimator"/>
<View
android:layout_width="match_parent"
android:layout_height="50dp"/>
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/pic"
android:layout_gravity="center"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
LinearLayout ll_root;
ImageView pic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_root= (LinearLayout) findViewById(R.id.ll_root);
pic= (ImageView) findViewById(R.id.pic);
}
private void changeLocation(View view,int x,int y){
int left=x;
int top=y;
int right=x+view.getWidth();
int bottom=view.getHeight()+y;
view.layout(left,top,right,bottom);
}
/**
* 位移动画
* @param view
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void lineAnimator(View view) {
int width=ll_root.getWidth();
int height=(int)pic.getY();
ValueAnimator animator=ValueAnimator.ofInt(height ,height+pic.getHeight());
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int y = (int) animation.getAnimatedValue();
int x = (int) pic.getX();
changeLocation(pic, x, y);
}
});
// animator.setInterpolator(new LinearInterpolator());
animator.start();
}
/**
* 缩放动画
* @param view
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void ScaleAnimator(View view) {
AnimatorSet animatorSet=new AnimatorSet();
ValueAnimator animatorsmall=ValueAnimator.ofFloat(1f,0);
animatorsmall.setDuration(1000);
animatorsmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale= (float) animation.getAnimatedValue();
pic.setScaleX(scale);
// pic.setScaleY(scale);
}
});
ValueAnimator animatorlarge=ValueAnimator.ofFloat(0, 1.2f, 1.0f);
animatorlarge.setDuration(1000);
animatorlarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale = (float) animation.getAnimatedValue();
pic.setScaleX(scale);
// pic.setScaleY(scale);
}
});
animatorSet.play(animatorlarge).after(animatorsmall);
animatorSet.start();
}
/**
* 透明度动画
* @param view
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void alphaAnimator(View view) {
ValueAnimator animator=ValueAnimator.ofFloat(1f,0.5f,1f);
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
pic.setAlpha((Float) animation.getAnimatedValue());
}
});
animator.start();
}
/**
* 圆形旋转动画
* @param view
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void circleAnimator(View view) {
ValueAnimator animator=ValueAnimator.ofFloat(0f, (float)(2* PI));//这边的转过的角度应使用弧度[0,2π]
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int x = (int) (100 * cos((float) animation.getAnimatedValue()) + 200);
int y = (int) (100 * sin((float) animation.getAnimatedValue()) + 300);
changeLocation(pic, x, y);
}
});
animator.setInterpolator(new LinearInterpolator());//匀速
animator.start();
}
public void moveCircle(int x,int y,int cx,int cy,int r){
}
}
android:interpolator 属性可以控制动画的变化速度:
<!--动画开始时速度比较慢,在中间的时加速-->
<set android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<!--动画开始时速度比较慢,然后开始加速-->
<set android:interpolator="@android:anim/accelerate_interpolator">
<!--动画循环播放特定的次数,以正弦曲线改变速度-->
<set android:interpolator="@android:anim/cycle_interpolator">
<!--动画开始时速度比较慢,然后开始减速-->
<set android:interpolator="@android:anim/decelerate_interpolator">
<!--均匀速度-->
<set android:interpolator="@android:anim/linear_interpolator">