zoukankan      html  css  js  c++  java
  • 动画(Animation) 它 (闪烁、左右摇摆、跷跷板等功效)

    一侧到另一侧的影响: (这里显示的是并不那么顺利)



    一、续播  (不知道取什么名字好,就是先播放动画A, 接着播放动画B)

    有两种方式。

    第一种。分别动画两个动画,A和B, 然后先播放动画A,设置A 的 AnimationListener。当onAnimationEnd触发(即A播放完成)时,開始播放B。

            animation1.setAnimationListener(new Animation.AnimationListener() {
    			@Override
    			public void onAnimationStart(Animation animation) {
    				
    			}
    			
    			@Override
    			public void onAnimationRepeat(Animation animation) {
    			}
    			
    			@Override
    			public void onAnimationEnd(Animation animation) {
    				animation2.start();
    			}
    		});

    另外一种,写一个动画集AnimationSet,在当中定义动画A和B,为动画B设置startOffset, 其值就是前一个动画播放的所需的时间。


    这边举一个样例,动画A是 透明度从 0.1 到 1.0 , 动画B是透明度从1.0到0.1,  使用以下这个动画集你就能够看到整个变化过程。

    <?

    xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.2" android:toAlpha="1.0" android:duration="3000" /> <alpha android:startOffset="3000" android:fromAlpha="1.0" android:toAlpha="0.2" android:duration="3000" /> </set>


    当中android:startOffset="3000"  表示延迟3秒后再运行。

     假设去掉当中的 android:startOffset="3000" , 你就什么效果也看不到了。 由于两个动画会同一时候播放。


    二、循环

    有时候,我们可能须要实现一个图片不停闪烁的功能(比方天气预报中的紧急警报功能),  或者有的时候我们须要实现图片左右晃动,都须要循环动画来实现。

    相同。也有两种办法。

    第一种,设置两个动画A 和 B, 动画A 是透明度 0 -1。 动画B是1 - 0, 然后对这两个动画都进行监听, A 结束运行B。 B结束运行A.. 无限循环... 

    另外一种,利用Animation的setRepeatCount、setRepeatMode来实现动画循环。


    比方闪烁(透明度亮 -> 暗, 暗->亮,如此循环)

            //闪烁
            AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
            alphaAnimation1.setDuration(3000);
            alphaAnimation1.setRepeatCount(Animation.INFINITE);
            alphaAnimation1.setRepeatMode(Animation.REVERSE);
            iv.setAnimation(alphaAnimation1);
            alphaAnimation1.start();

    alphaAnimation1.setRepeatCount(Animation.INFINITE); 表示反复多次。

    也能够设定详细反复的次数,比方alphaAnimation1.setRepeatCount(5);

    alphaAnimation1.setRepeatMode(Animation.REVERSE);表示动画结束后,反过来再运行。

    该方法有两种值, RESTART 和 REVERSE。

    RESTART表示从头開始,REVERSE表示从末尾倒播。


    懒得屏幕录像了,相似以下的效果:



    再比方左右摇摆

            //摇摆
            TranslateAnimation alphaAnimation2 = new TranslateAnimation(150f, 350f, 50, 50);
            alphaAnimation2.setDuration(1000);
            alphaAnimation2.setRepeatCount(Animation.INFINITE);
            alphaAnimation2.setRepeatMode(Animation.REVERSE);
            iv.setAnimation(alphaAnimation2);
            alphaAnimation2.start();

    其中 iv 是ImageView。


    好吧,。写了这么。



  • 相关阅读:
    PC端微信扫码支付和支付宝跳转支付 斧头帮
    微信支付 斧头帮
    PC端实现浏览器点击分享到QQ好友,空间,微信,微博等 斧头帮
    图片,word,Excel等附件上传 斧头帮
    java定时任务详解 斧头帮
    手机浏览器实现分享给好友或是朋友圈 斧头帮
    Java缓存EhcacheEhcache的Cache在SSM框架中的配置 斧头帮
    通过精确地址获取经纬度 斧头帮
    SpringBoot中设置自定义拦截器 斧头帮
    Visual Studio 2008创建项目(ATL)
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4592481.html
Copyright © 2011-2022 走看看