添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接


说明:
值动画分:平移动画,旋转动画,缩放动画,透明动画,颜色渐变动画
1)animator.setTarget(mIVHandle); 表示把动画添加到要实现动画的view上面
2) translationX 是view源码里的setTranslationX的translationX部分

1.值动画Activity对象

public class PropertyAnimActivity extends Activity {
private ImageView mIVHandle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_properties_anim);
mIVHandle = (ImageView) findViewById(R.id.iv_hand);
}
//位移
public void clickTraslate(View view){
ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "translationX", 0, 200);
animator.setDuration(3000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
//缩放
public void clickScale(View view){
//mIVHandle.setPivotX(0.5f);
//mIVHandle.setPivotY(0.5f);
ViewHelper.setPivotX(mIVHandle, 0.5f);
ViewHelper.setPivotY(mIVHandle, 0.5f);
ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "scaleX", 0, 2);
animator.setDuration(3000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
//旋转
public void clickRotate(View view){
//ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "rotationX", 0, 360);
ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "rotation", 0, 360);
animator.setDuration(3000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
//透明
public void clickAlpha(View view){
ObjectAnimator animator = ObjectAnimator.ofFloat(mIVHandle, "alpha", 0, 1);
animator.setDuration(3000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
//集合
public void clickSet(View view){
AnimatorSet set = new AnimatorSet();
ObjectAnimator alpha = ObjectAnimator.ofFloat(mIVHandle, "alpha", 0, 1);
ObjectAnimator rotation = ObjectAnimator.ofFloat(mIVHandle, "rotation", 0, 360);
ObjectAnimator scale = ObjectAnimator.ofFloat(mIVHandle, "scaleX", 0, 2);
//1.按循序播放
set.playSequentially(alpha, rotation, scale);
//2.按先后播放
set.play(alpha).after(rotation).before(scale);
//3.同时播放
set.playTogether(alpha, rotation, scale);
set.setDuration(2000);
set.start();
}
//color
public void clickColor(View view){
//setBackgroundColor
ObjectAnimator animator = ObjectAnimator.ofObject(mIVHandle, "backgroundColor", new ArgbEvaluator(), Color.RED, Color.BLUE);
animator.setDuration(3000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
//xml
public void clickXml(View view){
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.translator);
animator.setTarget(mIVHandle);
animator.start();
//AnimatorInflater.loadAnimator(this, R.animator.scale).setTarget(mIVHandle).start();
}
}

2.布局文件activity_properties_anim.xml

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickTraslate"
android:text="位移" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickScale"
android:text="缩放" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickRotate"
android:text="旋转 " />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickAlpha"
android:text="透明" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickSet"
android:text="集合" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickColor"
android:text="color" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickXml"
android:text="xml" />
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/iv_hand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
</RelativeLayout>

</LinearLayout>

3.调用的平移动画xml文件translator.xml,在res文件夹下添加animator目录

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="translationX"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="100"
android:valueType="floatType" >

</objectAnimator>