4.动画视图

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);
      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值