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

相关文章

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体