• Android笔记(六十四) android中的动画——补间动画(tweened animation)


          补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐。

          补间动画由一下四种方式:

          1.AplhaAnimation——透明度动画效果

          2.ScaleAnimation ——缩放动画效果

          3.TranslateAnimation——位移动画效果

          4.RotateAnimation——旋转动画效果

    1.AplhaAnimation——透明度动画效果

          AplhaAnimation的参数:

          fromAlpha:动画开始时的透明度,0.0表示完全透明

          toAlpha:动画结束时的透明度,1.0表示完全不透明

    package cn.lixyz.animator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private ImageView iv;
        private Button alpha;
    
        private AlphaAnimation alphaAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView() {
            iv = (ImageView) findViewById(R.id.image);
            alpha = (Button) findViewById(R.id.alpha);
            alpha.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.alpha:
                alphaAnimation = new AlphaAnimation(0.1f, 1);
                alphaAnimation.setDuration(1000);
                alphaAnimation.setRepeatCount(3);
                alphaAnimation.setRepeatMode(Animation.REVERSE);
                iv.startAnimation(alphaAnimation);
                alphaAnimation.setAnimationListener(new AnimationListener() {
    
                    @Override
                    public void onAnimationStart(Animation animation) {
                        Log.d("TTTT", "动画开始");
                    }
    
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        Log.d("TTTT", "动画重复");
    
                    }
    
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Log.d("TTTT", "动画结束");
    
                    }
                });
    
                break;
            }
        }
    }

          常用属性:

          setRepeatCount(int repeatCount)——设置重复次数

          setFillAfter(boolean)——动画执行完后是否停留在执行完的状态

          setStartOffset(long startOffset)——执行前的等待时间 

    2.ScaleAnimation ——缩放动画效果

          ScakeAnimation的参数:

          fromX:动画起始时X坐标上的伸缩尺寸

          toX:动画结束时X坐标上的伸缩尺寸

          fromY:动画起始时Y坐标上的伸缩尺寸

          toY:动画结束时Y坐标上的伸缩尺寸

          pivotXType:动画在X轴相对于物件位置类型

          pivotXValue:动画相对于物件的X坐标的开始位置

          pivotYType:动画在Y轴相对于物件位置类型

          pivotYValue:动画相对于物件的Y坐标的开始位置

    package cn.lixyz.animator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.ScaleAnimation;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private ImageView iv;
        private Button scale;
    
        private ScaleAnimation scaleAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView() {
            iv = (ImageView) findViewById(R.id.image);
            scale = (Button) findViewById(R.id.scale);
            scale.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.scale:
                scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(3000);
                iv.startAnimation(scaleAnimation);
    
                break;
            }
        }
    }

          常用属性:

          setRepeatCount(int repeatCount)——设置重复次数

          setFillAfter(boolean)——动画执行完后是否停留在执行完的状态

          setStartOffset(long startOffset)——执行前的等待时间

    3.TranslateAnimation——位移动画效果

          TranslateAnimation的参数

          fromXDelta: 动画开始的点离当前View X坐标上的差值

          toXDelta: 动画结束的点离当前View X坐标上的差值

          fromYDelta: 动画开始的点离当前View Y坐标上的差值

          toYDelta动画开始的点离当前View Y坐标上的差值

    package cn.lixyz.animator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.TranslateAnimation;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private ImageView iv;
        private Button transla;
    
        private TranslateAnimation translaAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView() {
            iv = (ImageView) findViewById(R.id.image);
            transla = (Button) findViewById(R.id.transla);
            transla.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.transla:
                translaAnimation = new TranslateAnimation(0, 150, 0, 0);
                translaAnimation.setDuration(1000);
                iv.startAnimation(translaAnimation);
                break;
            }
        }
    }

          常用属性:

          animation.setDuration(long durationMillis)——设置动画持续时间
          animation.setRepeatCount(int i)——设置重复次数
          animation.setRepeatMode(Animation.REVERSE)——设置反方向执行

    4.RotateAnimation——旋转动画效果

          RotateAnimation的参数:

          fromDegrees——旋转的开始角度。

          toDegrees——旋转的结束角度。

          pivotXType——X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

          pivotXValue——X坐标的伸缩值。

          pivotYType——Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

          pivotYValue——Y坐标的伸缩值。

    package cn.lixyz.animator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.RotateAnimation;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private ImageView iv;
        private Button rotate;
    
        private RotateAnimation rotateAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView() {
            iv = (ImageView) findViewById(R.id.image);
            rotate = (Button) findViewById(R.id.rotate);
            rotate.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.rotate:
                rotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                        0.5f);
                rotateAnimation.setDuration(2000);
                iv.startAnimation(rotateAnimation);
    
                break;
            }
        }
    }

          常用方法:

                  animation.setRepeatCount(int repeatCount)——设置重复次数
                  animation.setFillAfter(boolean)——动画执行完后是否停留在执行完的状态
                  animation.setStartOffset(long startOffset)——执行前的等待时间

     

  • 相关阅读:
    c++ 中bool 的默认值
    cocos2d CCLOG格式符号表
    c++数组指针bug
    cocos2d-x-2.2.6创建工程
    Nape实现坐标旋转角度回弹
    haxe 中使用音效
    haxe 嵌入swf 读取里面的内容
    haxe 配置
    Spring Tool Suite(STS)基本安装配置
    git提交忽略文件.gitignore内容
  • 原文地址:https://www.cnblogs.com/xs104/p/5068721.html
Copyright © 2020-2023  润新知