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

相关文章

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法