zoukankan      html  css  js  c++  java
  • android 动画效果

    android的动画效果可以通过在代码中Animation实现,可以实现的效果有平移,旋转,透明,缩放,同样的效果可以使用anim文件夹下的xml实现同样的动画效果,还可以在drawable文件使用多张图片形成动画。

    1.透明度动画

    代码

    //1.创建透明度动画对象,数值越小越透明
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.1f);
    //设置动画的持续时间
    alphaAnimation.setDuration(3000);
    //设置是否保留最终状态
    alphaAnimation.setFillAfter(true);
    //设置重复次数,填-1无限循环
    alphaAnimation.setRepeatCount(1);
    //设置动画的重复模式,默认是Restart,Reverse是反方向执行
    alphaAnimation.setRepeatMode(Animation.REVERSE);
    //通过控件启动动画
    img.startAnimation(alphaAnimation);

    xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:repeatMode="reverse"
    android:fillAfter="true"
    >
    <!-- 透明度动画 -->
    <!-- repeatMode 设置动画的重复模式,默认是Restart,Reverse是反方向执行 -->
    <!-- fillAfter 保持最终形态-->
    <!-- repeatCount 设置动画重复次数-->
    <!-- android:fromAlpha="1" android:toAlpha="0.2" 从完全不透明到透明-->
    <alpha android:fromAlpha="1" android:toAlpha="0.2"
    android:repeatCount="0"/>
    </set>


    2.缩放动画

    代码

    ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setDuration(3000);
    scaleAnimation.setFillAfter(true);
    img.startAnimation(scaleAnimation);

    xml

    <?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator= "@android:anim/decelerate_interpolator"
    android:duration="10000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:startOffset="0"
    android:toXScale="1.5"
    android:toYScale="1.5" />

    3.旋转动画

    代码

    //相对自己的左上角旋转,正数代表顺时针,负数逆时针
    RotateAnimation rotateAnimation = new RotateAnimation(0,-180);
    //相对(200,300)点旋转
    //相对于中心旋转
    //RotateAnimation rotateAnimation = new RotateAnimation(0,-180,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
    //RotateAnimation rotateAnimation = new RotateAnimation(0,-180,200,300);
    rotateAnimation.setDuration(3000);
    img.startAnimation(rotateAnimation);

    xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="10000"
    android:fromDegrees="300"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="10%"
    android:pivotY="100%"
    android:toDegrees="-360" />
    </set>

    4.平移动画

    代码

    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    int imgeWidth = img.getWidth();
    //TranslateAnimation translateAnimation = new TranslateAnimation(0,screenWidth-imgeWidth,0,0);
    TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,0);
    translateAnimation.setDuration(3000);
    translateAnimation.setFillAfter(true);
    img.startAnimation(translateAnimation);

    xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    >
    <translate
    android:duration="1000"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="200"
    android:toYDelta="0" />
    </set>

    执行xml动画的代码

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate);
    img.setAnimation(anim);
    img.startAnimation(nim);

     动画还有可以将图片循环播放的一种动画,使用animation_list来选择多张图片AnimationDrawable类来处理

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
    <item
    android:drawable="@drawable/img1"
    android:duration="200"/>
    <item
    android:drawable="@drawable/img2"
    android:duration="200"/>
    <item
    android:drawable="@drawable/img3"
    android:duration="200"/>

    </animation-list>

    开始动画和停止动画

    img.setImageResource(R.drawable.animation_img);
    //给动画资源赋值
    animationDrawable = (AnimationDrawable) img.getDrawable();
    animationDrawable.start();//开始
    animationDrawable.stop();//结束

    实例下载网址:https://github.com/tempest1/amin 

  • 相关阅读:
    django1.8升级1.9的几个问题
    App免费推广途径概要
    Django Channels 入门指南
    小谈业务应用架构
    比技术债更可怕的人债
    js数据结构与算法--递归
    常见react面试题汇总
    如何使用koa实现socket.io官网的例子
    Vue插槽
    10分钟了解 react 引入的 Hooks
  • 原文地址:https://www.cnblogs.com/kkrs/p/9505347.html
Copyright © 2011-2022 走看看