Android四大动画详解

Android常用的四个动画类:

  1. AlphaAnimation 透明度动画
  2. ScaleAnimation 缩放动画
  3. TranslateAnimation 位移动画
  4. RotateAnimation 旋转动画

1.AlphaAnimation 透明度动画
代码:

public class MainActivity extends AppCompatActivity{
    ImageView iv;
    AlphaAnimation alphaAnimation;//透明度动画
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.iv);
        alphaAnimation = new AlphaAnimation(0,1);//0为全透明 1为不透明
        alphaAnimation.setDuration(5000);//动画持续时间
//        alphaAnimation.setRepeatCount(3);//重复播放次数
//        alphaAnimation.setFillAfter(true);//动画执行完是否停留在执行完的状态
//        alphaAnimation.setStartOffset(1000);//执行前的等待

    }
    public void myClick(View v) {
       switch (v.getId()){
           case  R.id.start:
               iv.startAnimation(alphaAnimation);
               break;
           case R.id.stop:
               alphaAnimation.cancel();
               break;
       }
    }
}

效果图:
这里写图片描述

2.ScaleAnimation 缩放动画
构造方法参数讲解:

ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
  • float fromX 动画起始时 X坐标上的伸缩尺寸
  • float toX 动画结束时 X坐标上的伸缩尺寸
  • float fromY 动画起始时Y坐标上的伸缩尺寸
  • float toY 动画结束时Y坐标上的伸缩尺寸
  • int pivotXType 动画在X轴相对于物件位置类型
  • float pivotXValue 动画相对于物件的X坐标的开始位置
  • int pivotYType 动画在Y轴相对于物件位置类型
  • float pivotYValue 动画相对于物件的Y坐标的开始位置

代码:

public class MainActivity extends AppCompatActivity{
    ImageView iv;
    ScaleAnimation saleAnimation;//透明度动画
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.iv);
        saleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        saleAnimation.setDuration(2000);//设置动画持续时间
        saleAnimation.setRepeatCount(2);//设置重复次数
        saleAnimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        saleAnimation.setStartOffset(2000);//执行前的等待时间
    }
    public void myClick(View v) {
       switch (v.getId()){
           case  R.id.start:
               iv.startAnimation(saleAnimation);//开始动画
               break;
           case R.id.stop:
               saleAnimation.cancel();//取消动画
               break;
       }
    }
}

效果图:
这里写图片描述

3. TranslateAnimation 位移动画
构造方法讲解:

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 
  • float fromXDelta 动画开始的点离当前View X坐标上的差值
  • float toXDelta 动画结束的点离当前View X坐标上的差值
  • float fromYDelta 动画开始的点离当前View Y坐标上的差值
  • float toYDelta 动画开始的点离当前View Y坐标上的差值

代码:

public class MainActivity extends AppCompatActivity{
    ImageView iv;
    TranslateAnimation translateAnimation;//透明度动画
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.iv);
        translateAnimation = new TranslateAnimation(0, 150,0, 0);
        translateAnimation.setDuration(2000);//设置动画持续时间
        translateAnimation.setRepeatCount(2);//设置重复次数
        translateAnimation.setRepeatMode(Animation.REVERSE);//设置反方向执行
    }
    public void myClick(View v) {
       switch (v.getId()){
           case  R.id.start:
               iv.startAnimation(translateAnimation);//开始动画
               break;
           case R.id.stop:
               translateAnimation.cancel();//取消动画
               break;
       }
    }
}

这里写图片描述

4.RotateAnimation 旋转动画
构造方法讲解:

RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
  • float fromDegrees:旋转的开始角度。
  • float toDegrees:旋转的结束角度。
  • int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
  • float pivotXValue:X坐标的伸缩值。
  • int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
  • float pivotYValue:Y坐标的伸缩值。

代码:

public class MainActivity extends AppCompatActivity{
    ImageView iv;
    RotateAnimation rotateAnimationAnimation;//透明度动画
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.iv);
        rotateAnimationAnimation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimationAnimation.setDuration(3000);//设置动画持续时间
        /** 常用方法 */
        rotateAnimationAnimation.setRepeatCount(2);//设置重复次数
        rotateAnimationAnimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        rotateAnimationAnimation.setStartOffset(1000);//执行前的等待时间

    }
    public void myClick(View v) {
       switch (v.getId()){
           case  R.id.start:
               iv.startAnimation(rotateAnimationAnimation);//开始动画
               break;
           case R.id.stop:
               rotateAnimationAnimation.cancel();//取消动画
               break;
       }
    }
}

效果图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值