如果Android提供的四种动画 效果 和混合动画效果 不能够 满足需求的话,可以使用自定义动画效果 ;
新建一个类CustomAnimation 使其继承自 android.view.animation.Animation
package com.rock.lo6customanimation; import android.view.animation.Animation; import android.view.animation.Transformation; public class CustomAnimation2 extends Animation { private float controlWith; private float controlHeight; //自定义动画执行applyTransformation之前会先执行initialize方法 //在initialize方法中能获取调用者的长度和宽度,以及父级容器的长度和宽度 @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { // TODO Auto-generated method stub super.initialize(width, height, parentWidth, parentHeight); controlWith=width; controlHeight=height; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { //设置透明效果 ,并且使期透明效果 根据interpolatedTime的值进行变化 //t.setAlpha(interpolatedTime); //根据矩阵设置旋转效果 //三个参数degress:旋转的度数,px:X轴开始位置,py:Y轴开始位置 //t.getMatrix().setRotate(interpolatedTime*360, controlWith/2, controlHeight/2); //使用按钮进行左右摇头(Y轴不动,使按钮进行左右来回移动) t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*50)*50, 0); super.applyTransformation(interpolatedTime, t); } }
<Button android:id="@+id/btnCustom2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/btnCustomAnim" android:layout_centerVertical="true" android:layout_marginRight="35dp" android:text="@string/btnCustom2" />
findViewById(R.id.btnCustom2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CustomAnimation2 custom2=new CustomAnimation2(); custom2.setDuration(2000); v.startAnimation(custom2); } });