zoukankan      html  css  js  c++  java
  • Android 动画效果 及 自定义动画

    1. View动画-透明动画效果
    2. View动画-旋转动画效果
    3. View动画-移动动画效果
    4. View动画-缩放动画效果
    5. View动画-动画效果混合
    6. View动画-动画效果侦听
    7. 自定义动画效果

    工程代码:DIYAnimation.zip

    -----------------------------------

    1. View动画-透明动画效果

    Java代码实现

    AlphaAnimation aa = new AlphaAnimation(0, 1); //透明动画效果
    aa.setDuration(1000);
    v.startAnimation(aa);

    XML文件实现

    在 res/anim 下创建alpha动画效果文件 alpha_animation.xml

    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="1000" >
    </alpha>
    //使用
    v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_animation));

     

     
    2. View动画-旋转动画效果


    3. View动画-移动动画效果


    4. View动画-缩放动画效果


    5. View动画-动画效果混合

    Java实现,透明效果和移动效果结合

    public class MixedAnimDemoActivity extends Activity {
    
        private Button mixedBtn;
        private AnimationSet as;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mixed_anim_demo);
            
            as = new AnimationSet(true); //是否共用动画补间
            as.setDuration(1000);
            AlphaAnimation aa = new AlphaAnimation(0, 1);
            aa.setDuration(1000);
            as.addAnimation(aa);
            
            TranslateAnimation ta = new TranslateAnimation(200, 0, 200, 0);
            ta.setDuration(1000);
            as.addAnimation(ta);
            
            mixedBtn = (Button)findViewById(R.id.mixedBtn);
            mixedBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.startAnimation(as);
                }
            });
        }
    
    }


    6. View动画-动画效果侦听

    创建动画效果的侦听事件, 在动画结束之后弹出一个Toast

    AlphaAnimation aa = new AlphaAnimation(0, 1);
    aa.setDuration(1000);
    aa.setAnimationListener(new AnimationListener() {
        
        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }
        
        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
        }
        
        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Animate end", Toast.LENGTH_LONG).show();
        }
    });
    as.addAnimation(aa);


    7. 自定义动画效果

    需要创建一个CustomAnim动画类,集成 Animation

    public class CustomAnim extends Animation {
    
        @Override
        public void initialize(int width, int height, int parentWidth,
                int parentHeight) {
            // w我们需要知道目标动画效果的宽和高,所以需要重写该方法
            // 父级容器的宽高
            // initialize 在 applyTransformation之前执行
            super.initialize(width, height, parentWidth, parentHeight);
        }
        
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            // TODO Auto-generated method stub
            // interpolatedTime 补间时间, 从0~1, 动画执行完为1
    
            //t.setAlpha(interpolatedTime); //可以实现透明动画
            
            //可以实现移动效果
            //t.getMatrix().setTranslate(200 * interpolatedTime, 200 * interpolatedTime);
            
            //左右摇摆效果
            t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime*50)*10), 0); 
            super.applyTransformation(interpolatedTime, t);
        }
    }

    工程代码:DIYAnimation.zip

  • 相关阅读:
    浮点数如何存储与表示(精度问题)
    快速幂详解(幂运算与矩阵)
    斐波那契数列的四种解法(头递归、尾递归、迭代与矩阵快速幂)
    机器码与位运算
    pytorch中参数dim的含义(正负,零,不传)
    查看显卡名称 pytorch
    使用国内源快速下载Python包(以matplotlib为例)
    基于Attention的机器翻译模型(Visualizing A Neural Machine Translation Model)
    GNU Parallel-安装+使用
    统计文件个数、删除空文件等Linux命令
  • 原文地址:https://www.cnblogs.com/carlo/p/4725115.html
Copyright © 2011-2022 走看看