NineOldAndroidsDemos 学习(2) ViewPropertyAnimator

这一节实现的功能其实和前面的Toggle差不多,只不过使用了另一种 方式实现,操作起来发现更加简单

其实在android 3.0+以后 是可以直接使用viewPropertryAnimator的,

方式如下

   imageView.animate().setDuration(2000);
   imageView.animate().alpha(0);

但是在2.3以下就会报 函数找不到的错误了,所以NineOldAndroidDemos提供了兼容方式,虽然不是重写view,但是使用起来和4.0的挺像的.

代码:

package com.jakewharton.nineoldandroids.sample;

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class VPADemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vpademo);

        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        final Button fadeOut = (Button) findViewById(R.id.fadeOut);
        final Button fadeIn = (Button) findViewById(R.id.fadeIn);
        final Button moveOver = (Button) findViewById(R.id.moveOver);
        final Button moveBack = (Button) findViewById(R.id.moveBack);
        final Button rotate = (Button) findViewById(R.id.rotate);
        final Button animatingButton = (Button) findViewById(R.id.animatingButton);

        // Set long default duration for the animator, for the purposes of this demo
        animate(animatingButton).setDuration(2000);

        fadeOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //渐渐消失
                animate(animatingButton).alpha(0);
            }
        });

        fadeIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //渐渐展现
                animate(animatingButton).alpha(1);
            }
        });

        moveOver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //移动
                int xValue = container.getWidth() - animatingButton.getWidth();
                int yValue = container.getHeight() - animatingButton.getHeight();
                animate(animatingButton).x(xValue).y(yValue);
            }
        });

        moveBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animate(animatingButton).x(0).y(0);
            }
        });

        rotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //旋转
                animate(animatingButton).rotationYBy(720);
            }
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/fadeOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fade Out"/>
    <Button
        android:id="@+id/fadeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fade In"/>
    <Button
        android:id="@+id/moveOver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Move Over"/>
    <Button
        android:id="@+id/moveBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Move Back"/>
    <Button
        android:id="@+id/rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rotate"/>
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:id="@+id/container">
	    <Button
	        android:id="@+id/animatingButton"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="Animating Button"/>
    </LinearLayout>
</LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值