《Android那些事》——清晰理解各个Animation

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button Alpha_Btn, Scale_Btn, Rotate_Btn, Translate_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Alpha_Btn = (Button) findViewById(R.id.alpha);
        Scale_Btn = (Button) findViewById(R.id.scale);
        Rotate_Btn = (Button) findViewById(R.id.rotate);
        Translate_Btn = (Button) findViewById(R.id.translate);

        Alpha_Btn.setOnClickListener(this);
        Scale_Btn.setOnClickListener(this);
        Rotate_Btn.setOnClickListener(this);
        Translate_Btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //淡入淡出
            case R.id.alpha:
                //第一步,创建AnimationSet对象
                AnimationSet animationSet_1 = new AnimationSet(true);
                //第二步,创建相应的动画
                AlphaAnimation alpha = new AlphaAnimation(1, 0);//从透明度为1到透明度为0
                //第三步,设置动画执行时间
                alpha.setDuration(2000);
                //第四步,将动画放入动画集中
                animationSet_1.addAnimation(alpha);
                //第五步,作用于控件之上
                Alpha_Btn.startAnimation(animationSet_1);
                break;

            //缩放
            /**
             * 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的坐标类型  RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
             * float pivotXValue 动画相对于物件的X坐标的开始位置
             * int pivotYType 中心点Y的坐标类型  RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
             * float pivotYValue 动画相对于物件的Y坐标的开始位置
             */
            case R.id.scale:
                AnimationSet animationSet_2 = new AnimationSet(true);
                ScaleAnimation scale = new ScaleAnimation(1, 0.1f, 1, 0.1f,
                        Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF,
                        1f);
                scale.setDuration(2000);
                animationSet_2.addAnimation(scale);
                /** 常用方法 */
                //scale.setRepeatCount(int repeatCount);//设置重复次数
                //scale.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
                //scale.setStartOffset(long startOffset);//执行前的等待时间
                Scale_Btn.startAnimation(animationSet_2);
                break;

            //旋转
            //RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
            /**
             * float fromDegrees:旋转的开始角度。
             * float toDegrees:旋转的结束角度。
             * int pivotXType:中心点x的坐标类型  RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
             * float pivotXValue:X坐标的伸缩值。
             * int pivotYType:中心点Y的坐标类型  RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
             * float pivotYValue:Y坐标的伸缩值。
             */
            case R.id.rotate:
                AnimationSet animationSet_3 = new AnimationSet(true);
                RotateAnimation rotate = new RotateAnimation(0, 360,//从什么度数到什么度数
                        Animation.RELATIVE_TO_SELF, 0.5f,//x轴绕着哪个对象旋转,旋转的x坐标是多少
                        Animation.RELATIVE_TO_SELF, 0.5f);//y轴绕着哪个对象旋转,旋转的y坐标是多少
                rotate.setDuration(2000);
                animationSet_3.addAnimation(rotate);
                /** 常用方法 */
                //rotate.setRepeatCount(int repeatCount);//设置重复次数
                //rotate.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
                //rotate.setStartOffset(long startOffset);//执行前的等待时间
                Rotate_Btn.startAnimation(animationSet_3);
                break;

            //移动
            //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坐标上的差值
             */
            case R.id.translate:
                AnimationSet animationSet_4 = new AnimationSet(true);
                TranslateAnimation translate = new TranslateAnimation(
                        Animation.RELATIVE_TO_PARENT, 0f,
                        Animation.RELATIVE_TO_PARENT, 1f,
                        Animation.RELATIVE_TO_PARENT, 0f,
                        Animation.RELATIVE_TO_PARENT, 1f);
                translate.setDuration(2000);
                /** 常用方法 */
                //translate.setRepeatCount(int i);//设置重复次数
                //translate.setRepeatMode(Animation.REVERSE);//设置反方向执行
                animationSet_4.addAnimation(translate);
                Translate_Btn.startAnimation(animationSet_4);
                break;
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值