先呈上我测试动画的界面:
布局文件main.xml为:
<?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" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:src="@drawable/naruto" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="click me to show" /> </LinearLayout>
接下来自定义动画,新建一个类CustomAnimation继承自Animation,需要重写applyTransformation和initialize方法,具体解释就在代码中给出了,所以呢,在这里就不重复说了吧O(∩_∩)O。自定义类CustomAnimation代码如下:
package com.android.animation; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.AnticipateOvershootInterpolator; import android.view.animation.Transformation; public class CustomAnimation extends Animation { int centerX, centerY; public CustomAnimation() { } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // TODO Auto-generated method stub super.applyTransformation(interpolatedTime, t); Matrix matrix = t.getMatrix(); //通过interpolatedTime设置矩阵缩放 matrix.setScale(interpolatedTime, interpolatedTime); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } //初始化动画 @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { // TODO Auto-generated method stub super.initialize(width, height, parentWidth, parentHeight); // 初始化中点坐标 centerX = width / 2; centerY = height / 2; //动画持续的时间 setDuration(2000); //设置动画结束后是否保留在终止的位置 setFillAfter(true); /** * 设置动画的加速度曲线,传入参数Interpolator的简介子类有: * AnticipateOvershootInterpolator 改变开始向后然后向前射出超出屏幕 * AccelerateDecelerateInterpolator 变化的速度开始和结束缓慢但中间加速穿过 * AccelerateInterpolator 变化的速度开始慢慢,然后加速 * AnticipateInterpolator 改变开始向后然后向前射出 * BounceInterpolator 变化到最后时反弹 * CycleInterpolator 重复动画指定数量的周期CycleInterpolator(float cycles)需传入周期数 * DecelerateInterpolator 开始变化的速度很快,然后开始减速 * LinearInterpolator 线性变化 * OvershootInterpolator 射出向前,超过屏幕然后回来 * * pS->文字功底欠佳,描述的不是很准确哈,见谅,见谅o(╯□╰)o具体效果还是要自己动手试试 */ setInterpolator(new AnticipateOvershootInterpolator()); } }
最后在主Activity中使用自定义的Animation:
package com.android.animation; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class CustomAnimationActivity extends Activity { /** Called when the activity is first created. */ private CustomAnimation animation; private ImageView image; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //实例化 animation = new CustomAnimation(); image = (ImageView)findViewById(R.id.image); button = (Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub //为图片设置动画效果 image.setAnimation(animation); } }); } }