NineOldAndroidsDemos(http://nineoldandroids.com/) 是一个让3.0一下的应用也可以使用Animator的开源项目,
所以接触android也有不少时间了,但是使用animator却很少,所以借此机会学习一下animator的使用.
首先是Toggles
这个是最简单的animator的使用
很简单 只要使用ObjectAnimator/ofFloat就可以对某个view进行动画,比animation是不是更快一些?
public class Toggles extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toggles);
final View target = findViewById(R.id.target);
final int duration = 2 * 1000;
findViewById(R.id.tx).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "translationX", 0, 50, -50, 0).setDuration(duration).start();
}
});
findViewById(R.id.ty).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "translationY", 0, 50, -50, 0).setDuration(duration).start();
}
});
findViewById(R.id.sx).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "scaleX", 1, 2, 1).setDuration(duration).start();
}
});
findViewById(R.id.sy).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "scaleY", 1, 2, 1).setDuration(duration).start();
}
});
findViewById(R.id.a).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "alpha", 1, 0, 1).setDuration(duration).start();
}
});
findViewById(R.id.rx).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "rotationX", 0, 180, 0).setDuration(duration).start();
}
});
findViewById(R.id.ry).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "rotationY", 0, 180, 0).setDuration(duration).start();
}
});
findViewById(R.id.rz).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator.ofFloat(target, "rotation", 0, 180, 0).setDuration(duration).start();
}
});
findViewById(R.id.pivot_zero_zero).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHelper.setPivotX(target, 0);
ViewHelper.setPivotY(target, 0);
}
});
findViewById(R.id.pivot_center).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHelper.setPivotX(target, target.getWidth() / 2f);
ViewHelper.setPivotY(target, target.getHeight() / 2f);
}
});
findViewById(R.id.pivot_width_height).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHelper.setPivotX(target, target.getWidth());
ViewHelper.setPivotY(target, target.getHeight());
}
});
}
}
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/tx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TX"/>
<Button
android:id="@+id/ty"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TY"/>
<Button
android:id="@+id/sx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SX"/>
<Button
android:id="@+id/sy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SY"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Alpha"/>
<Button
android:id="@+id/rx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RX"/>
<Button
android:id="@+id/ry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RY"/>
<Button
android:id="@+id/rz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RZ"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<Button
android:id="@+id/pivot_zero_zero"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot (0,0)"/>
<Button
android:id="@+id/pivot_center"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot Center"/>
<Button
android:id="@+id/pivot_width_height"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Pivot (w,h)"/>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center">
<Button
android:id="@+id/target"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Sample Text"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>