Animation制作动画组件效果
包括:旋转,淡入浅出,移动,透明。
源代码如下:
package com.lgq.animation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1;
Button button2;
Button button3;
Button button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4= (Button) findViewById(R.id.button4);
}
public void button1(View view)
{
//创建一个AnimationSet对象,参数为Boolean型,
//true表示使用Animation的interpolator,false则是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间
alphaAnimation.setDuration(500);
//将alphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法执行动画
button1.startAnimation(animationSet);
}
public void button2(View view)
{
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
sa.setDuration(1000);
animationSet.addAnimation(sa);
button2.startAnimation(animationSet);
}
public void button3(View view)
{
AnimationSet animationSet = new AnimationSet(true);
//参数1:从哪个旋转角度开始
//参数2:转到什么角度
//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(1000);
animationSet.addAnimation(ra);
button3.startAnimation(animationSet);
}
public void button4(View view)
{
AnimationSet animationSet = new AnimationSet(true);
//参数1~2:x轴的开始位置
//参数3~4:x轴的结束位置
//参数5~6:y轴的开始位置
//参数7~8:y轴的结束位置
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f);
ta.setDuration(1000);
animationSet.addAnimation(ta);
button4.startAnimation(animationSet);
}
}
自定义的动画,实现左右摇晃的代码:
package com.lgq.animation; import android.view.animation.Animation; import android.view.animation.Transformation; /** * Created by Administrator on 2015/9/7. */ public class MyAnimation extends Animation{ @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*20)*20,0); super.applyTransformation(interpolatedTime, t); } }
* 主程序代码:
public void button5(View view) { MyAnimation ma = new MyAnimation(); button5.startAnimation(ma); }