TVOffAnimation.java
package com.iaiai.activity; import android.graphics.Matrix; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Animation; import android.view.animation.Transformation; /** * * <p> * Title: TVOffAnimation.java * </p> * <p> * E-Mail: [email protected] * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2012-10-8 下午1:33:44 * </p> * * @author 丸子 * @version 0.0.1 */ public class TVOffAnimation extends Animation { private int halfWidth; private int halfHeight; @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); setDuration(500); setFillAfter(true); // 保存View的中心点 halfWidth = width / 2; halfHeight = height / 2; setInterpolator(new AccelerateDecelerateInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final Matrix matrix = t.getMatrix(); if (interpolatedTime < 0.5) { matrix.preScale(1 + 0.625f * interpolatedTime, 1 - interpolatedTime / 0.8f + 0.003f, halfWidth, halfHeight); } else { matrix.preScale(7.5f * (1 - interpolatedTime), 0.003f, halfWidth, halfHeight); } } }
MainActivity.java
package com.iaiai.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; /** * * <p> * Title: MainActivity.java * </p> * <p> * E-Mail: [email protected] * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2012-10-8 下午1:33:44 * </p> * * @author 丸子 * @version 0.0.1 */ public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { findViewById(R.id.root).startAnimation(new TVOffAnimation()); } }); } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="动画" /> </LinearLayout>