4.1 问题
应用程序要让视图对象运动起来,实现变化或其他特效。
4.2 解决方案
(API Level12)
ObjectAnimator实例,例如ViewPropertyAnimator,可以用来操作View对象的属性,例如视图的位置或旋转。ViewPropertyAnimation是通过View.animate()获得的,然后根据动画的特征进行修改。通过这个API进行的修改会影响到View对象本身的真实属性。
4.3 实现机制
ViewPropertyAnimation是对视图内容制作动画的最便利方法。此API的工作方式就像生成器一样,所有对不同属性修改的调用都可以连接起来组成一个动画。在当前线程的Looper的相同迭代中,对ViewPropertyAnimation的所有调用都会汇集到一个动画中。以下两个代码演示了一个简单的视图过渡Activity。
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click to Toggle" />
<View
android:id="@+id/theView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AAA" />
</LinearLayout>
使用了ViewPropertyAnimation的Activity
public class AnimateActivity extends Activity implements View.OnClickListener {
private View mViewToAnimate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.toggleButton);