NineOldAndroidsDemos 学习(1) Toggles

本文介绍如何通过Animator实现视图动画效果,包括平移、缩放、旋转及透明度变化等操作。提供了具体的代码实例,展示了如何为按钮点击事件绑定不同的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值