android之动画、xml实现动画res\anim

2024-05-31 09:58
文章标签 xml 实现 android 动画 res anim

本文主要是介绍android之动画、xml实现动画res\anim,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

----补间动画(View动画)------------

原理:动画不会改变控件真实的坐标;

  //透明动画public void alphaAnimate(){ImageView iv=findViewById(R.id.imagev);AlphaAnimation aa=new AlphaAnimation(1.0f,0.0f);aa.setDuration(2000);//动画执行的时间aa.setRepeatCount(1);//动画重复的次数.,默认一次aa.setRepeatMode(Animation.RESTART);//设置模式iv.startAnimation(aa);}//旋转动画public void rotateAnimate(){ImageView iv=findViewById(R.id.imagev);RotateAnimation ra=new RotateAnimation(0,360);//参数一,开始的角度,参数二,结束的角度ra.setDuration(2000);//动画执行的时间ra.setRepeatCount(1);//动画重复的次数.,默认一次ra.setRepeatMode(Animation.RESTART);//设置模式iv.startAnimation(ra);}//缩放动画public void scaleAnimate(){ImageView iv=findViewById(R.id.imagev);ScaleAnimation sa=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//参数:开始的缩放比例,结束的缩放比例,相对于自己的中心sa.setDuration(2000);//动画执行的时间sa.setRepeatCount(1);//动画重复的次数.,默认一次sa.setRepeatMode(Animation.RESTART);//设置模式iv.startAnimation(sa);}//平移动画'public  void translateAnimate(){ImageView iv=findViewById(R.id.imagev);TranslateAnimation tra=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);//相对于父控件移动tra.setDuration(2000);tra.setFillAfter(true);//当动画结束后停留在结束的位置上iv.startAnimation(tra);}//组合动画---一起执行public void togetherAnimate(){ImageView iv=findViewById(R.id.imagev);AnimationSet aset=new AnimationSet(true);//创建动画合集RotateAnimation ra=new RotateAnimation(0,360);//参数一,开始的角度,参数二,结束的角度ra.setDuration(2000);//动画执行的时间ra.setRepeatCount(1);//动画重复的次数.,默认一次ra.setRepeatMode(Animation.RESTART);//设置模式TranslateAnimation tra=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);//相对于父控件移动tra.setDuration(2000);tra.setFillAfter(true);//当动画结束后停留在结束的位置上
aset.addAnimation(ra);//添加动画aset.addAnimation(tra);//添加动画iv.startAnimation(aset);}

-------xml创建View动画-------

补间动画,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。

View animation只能应用于View对象,而且只支持一部分属性,如支持缩放旋转而不支持背景颜色的改变。

在View Animation(Tween Animation)中,其改变的是View的绘制效果,真正的View的属性保持不变,比如无论你在对话中如何缩放Button的大小,Button的有效点击区域还是没有应用动画时的区域,其位置与大小都不变。

用XML定义的动画放在/res/anim/文件夹内。

 

 

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:fromAlpha="1.0f"android:toAlpha="0.5f"android:repeatModel="reverse"android:duration="2000"android:repeatMode="reverse"><alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"android:pivotX="50%"android:pivotY="50%"android:fromDegrees="0"android:toDegrees="300"android:duration="2000"
android:repeatCount="1"></rotate>
<?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:pivotX="50%"android:pivotY="50%"android:fromDegrees="0"android:toDegrees="300"android:duration="2000"android:repeatCount="1"></rotate><alpha xmlns:android="http://schemas.android.com/apk/res/android"android:fromAlpha="1.0f"android:toAlpha="0.5f"android:repeatModel="reverse"android:duration="2000"android:repeatMode="reverse"><alpha></set>

应用动画:

  1. ImageView spaceshipImage = (ImageView)findViewById(R.id.spaceshipImage);

  2. Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump;

  3. spaceshipImage.startAnimation(hyperspaceJumpAnimation);

 

 

----------属性动画-----

它更改的是对象的实际属性。在Property Animation中,改变的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。而且Property Animation不止可以应用于View,还可以应用于任何对象。

会改变控件的真实坐标

 //创建属性动画public void proportyAnimate(){ImageView iv=findViewById(R.id.imagev);ObjectAnimator oa=ObjectAnimator.ofFloat(iv,"translationX",10,50,20,100);oa.setDuration(2000);oa.start();//开始动画//缩放ObjectAnimator ra=ObjectAnimator.ofFloat(iv,"rotation",0,180,90,360);ra.setDuration(2000);ra.start();//开始动画//alphaObjectAnimator aa=ObjectAnimator.ofFloat(iv,"alpha",0,0.5f,0,1,0,1);//scaleObjectAnimator sa=ObjectAnimator.ofFloat(iv,"scaleY",0.1f,2,2,2);iv.setTranslationX(30);}
  //*************往集合中添加动画**********AnimatorSet set =new AnimatorSet();set.setTarget(iv);set.playSequentially(oa,ra,aa,sa);//一个一个执行set.playTogether(oa,ra,aa,sa);//一起执行set.start();
//xml的方式可以创建按属性动画----在res文件夹下创建animator文件夹
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android"><objectAnimatorandroid:duration="2000"android:propertyName="translationX"android:valueFrom="10"android:valueTo="100"></objectAnimator></animator>

 

---------帧动画------在AndroidStudio中强制规定带animation-list节点xml文件必须放在res/drawable文件下,帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

 

//帧动画public void animate(){//动画分帧动画(drawable Animation )、view动画(view Animation)、属性动画(proprty额Animaion)final ImageView imageAnimate=(ImageView) findViewById(id.image_animate);imageAnimate.setBackgroundResource(R.drawable.dr_animation);//设置背景资源AnimationDrawable rocketAnimate= (AnimationDrawable) imageAnimate.getBackground();
rocketAnimate.start();}
drawable文件夹下创建的dr_animation.xml.                   <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true">//:oneshot="true"动画只显示一次,//duration="200"是图片显示的时长<itemandroid:drawable="@drawable/ic_launcher1"android:duration="200" /><itemandroid:drawable="@drawable/ic_launcher1"android:duration="200" /><itemandroid:drawable="@drawable/ic_launcher1"android:duration="200" /><itemandroid:drawable="@drawable/ic_launcher1"android:duration="200" />
</animation-list>

 

==========xml中实现动画activity跳转========

从左向右弹出:

用XML定义的动画放在/res/anim/文件夹内,创建:选中anim文件夹-》右键New-》animate Resource File;

从左向右弹出

在 res目录创建anim目录, 然后在目录创建动画的xml文件:out_to_left.xml (从左边退出动画) 、in_from_right.xml(从右边进入动画) 
out_to_right.xml(从右边退出动画)、in_from_left.xml(从左边进入动画)

in_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="0%p" >
 
</translate>

in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="0%p" >
 
</translate>

out_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"
    android:interpolator="@android:anim/accelerate_interpolator" >
 
</translate>

out_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator" >
 
</translate>

在style.xml中设置主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item> <item name="android:windowAnimationStyle">@style/fade</item>
</style>
<style name="fade" parent="@android:style/Animation.Activity"><item name="android:activityOpenEnterAnimation">@anim/in_from_right</item><item name="android:activityOpenExitAnimation">@anim/out_to_left</item><item name="android:activityCloseEnterAnimation">@anim/in_from_left</item><item name="android:activityCloseExitAnimation">@anim/out_to_right</item>
</style>

 

从下向上弹出

in_from_bottom:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="5000"android:fromYDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator"android:toYDelta="0%p" ></translate><alphaandroid:duration="50"android:fromAlpha="1.0"android:toAlpha="1.0" />
</set>

in_from_top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="5000"android:fromYDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator"android:toYDelta="0%p" ></translate>
</set>

out_to_bottom:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="5000"android:fromYDelta="0%p"android:toYDelta="0%p"   //这两两个都是之为0,表示原来的页面不移动android:interpolator="@android:anim/accelerate_interpolator" ></translate>
</set>

out_to_top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="5000"android:fromYDelta="0%p"android:toYDelta="0%p"  //这两两个都是之为0,表示原来的页面不移动android:interpolator="@android:anim/accelerate_interpolator" ></translate>
</set>

可以在style .xml中设置主题样式,其中item设置动画,参考本文上面的内容:从左往右的动画, 在manifest.xml中吧主题设置给activity。

也可以(使用activity的overridePendingTransition方法,)

fragment中:
getActivity().overridePendingTransition(R.anim.in_from_bottom,R.anim.in_from_top);//两个参数分别是新页面进的动画上一个页面出的xml动画

activity中:

overridePendingTransition(R.anim.in_from_bottom,R.anim.in_from_top);

页面返回的时候,在finish()后面使用  overridePendingTransition(R.anim.in_from_top,R.anim.out_to_top);

在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初 始化列表数据、向服务器发送请求获取数据等等。同样,使用startActivity(intent)方法从一个Activity进入到新的 Activity时,这个过程中也会出现短暂的黑屏。这个问题的存在是由Android应用框架决定的,但的确很影响用户体验。

自定义一个主题
<style name="myautodefine" parent="AppTheme.NoActionBar"> <item name="android:windowIsTranslucent">true</item>
</style>

在manifest.xml中的activity中设置主题为自定义的主题。

 

 

这篇关于android之动画、xml实现动画res\anim的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017721

相关文章

SpringCloud整合MQ实现消息总线服务方式

《SpringCloud整合MQ实现消息总线服务方式》:本文主要介绍SpringCloud整合MQ实现消息总线服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、背景介绍二、方案实践三、升级版总结一、背景介绍每当修改配置文件内容,如果需要客户端也同步更新,

Dubbo之SPI机制的实现原理和优势分析

《Dubbo之SPI机制的实现原理和优势分析》:本文主要介绍Dubbo之SPI机制的实现原理和优势,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Dubbo中SPI机制的实现原理和优势JDK 中的 SPI 机制解析Dubbo 中的 SPI 机制解析总结Dubbo中

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Java 压缩包解压实现代码

《Java压缩包解压实现代码》Java标准库(JavaSE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RA... 目录一、解压压缩包1.zip解压代码实现:2.rar解压代码实现:3.调用解压方法:二、注意事项三、总

NGINX 配置内网访问的实现步骤

《NGINX配置内网访问的实现步骤》本文主要介绍了NGINX配置内网访问的实现步骤,Nginx的geo模块限制域名访问权限,仅允许内网/办公室IP访问,具有一定的参考价值,感兴趣的可以了解一下... 目录需求1. geo 模块配置2. 访问控制判断3. 错误页面配置4. 一个完整的配置参考文档需求我们有一

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

基于MongoDB实现文件的分布式存储

《基于MongoDB实现文件的分布式存储》分布式文件存储的方案有很多,今天分享一个基于mongodb数据库来实现文件的存储,mongodb支持分布式部署,以此来实现文件的分布式存储,需要的朋友可以参考... 目录一、引言二、GridFS 原理剖析三、Spring Boot 集成 GridFS3.1 添加依赖

利用Python实现Excel文件智能合并工具

《利用Python实现Excel文件智能合并工具》有时候,我们需要将多个Excel文件按照特定顺序合并成一个文件,这样可以更方便地进行后续的数据处理和分析,下面我们看看如何使用Python实现Exce... 目录运行结果为什么需要这个工具技术实现工具的核心功能代码解析使用示例工具优化与扩展有时候,我们需要将

Python+PyQt5实现文件夹结构映射工具

《Python+PyQt5实现文件夹结构映射工具》在日常工作中,我们经常需要对文件夹结构进行复制和备份,本文将带来一款基于PyQt5开发的文件夹结构映射工具,感兴趣的小伙伴可以跟随小编一起学习一下... 目录概述功能亮点展示效果软件使用步骤代码解析1. 主窗口设计(FolderCopyApp)2. 拖拽路径