这一节实现的功能其实和前面的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>