<pre name="code" class="plain">
1.
MainActivity.java
public class MainActivity extends Activity {
private ImageView iv;
private ObjectAnimator oa1;
private ObjectAnimator oa2;
private ObjectAnimator oa3;
private ObjectAnimator oa4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点到我了", 0).show();
}
});
}
public void translate(View v){
// TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 0);
// ta.setDuration(2000);
// ta.setFillAfter(true);
// iv.startAnimation(ta);
oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100);
oa1.setDuration(2000);
oa1.setRepeatCount(1);
oa1.setRepeatMode(ValueAnimator.REVERSE);
oa1.start();
}
public void scale(View v){
oa2 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 2, 1.6f);
oa2.setDuration(2000);
//oa2.setRepeatCount(1);
//oa2.setRepeatMode(ValueAnimator.REVERSE);
oa2.start();
}
public void alpha(View v){
oa3 = ObjectAnimator.ofFloat(iv, "alpha", 1, 0.2f, 1, 0.8f);
oa3.setDuration(2000);
//oa3.setRepeatCount(1);
//oa3.setRepeatMode(ValueAnimator.REVERSE);
oa3.start();
}
public void rotate(View v){
oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
oa4.setDuration(2000);
oa4.setRepeatCount(1);
oa4.setRepeatMode(ValueAnimator.REVERSE);
oa4.start();
}
public void fly(View v){
AnimatorSet set = new AnimatorSet();
//挨个飞
// set.playSequentially(oa1, oa2, oa3, oa4);
//一起飞
set.playTogether(oa1, oa2, oa3, oa4);
set.start();
}
//用xml文件定义属性动画,xml文件的内容在下方
public void xml(View v){
Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator);
//设置作用于那个组件
at.setTarget(iv);
at.start();
}
}
2.layout中的activity_main.xml
<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=".MainActivity" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"
android:onClick="translate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:onClick="scale"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明"
android:onClick="alpha"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:onClick="rotate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一起飞"
android:onClick="fly"/>
</LinearLayout>
<Button
android:layout_below="@id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xml定义的属性动画"
android:onClick="xml"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerInParent="true"
/>
</RelativeLayout>
3.用xml文件定义属性动画--------------------------------------------------
【用xml文件定义属性动画,需要在res目录下建立一个xml文件】
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:propertyName="translationX"
android:duration="200"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="-100"
android:valueTo="100"
>
</objectAnimator>
</set>