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

相关文章

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

java父子线程之间实现共享传递数据

《java父子线程之间实现共享传递数据》本文介绍了Java中父子线程间共享传递数据的几种方法,包括ThreadLocal变量、并发集合和内存队列或消息队列,并提醒注意并发安全问题... 目录通过 ThreadLocal 变量共享数据通过并发集合共享数据通过内存队列或消息队列共享数据注意并发安全问题总结在 J

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

JS 实现复制到剪贴板的几种方式小结

《JS实现复制到剪贴板的几种方式小结》本文主要介绍了JS实现复制到剪贴板的几种方式小结,包括ClipboardAPI和document.execCommand这两种方法,具有一定的参考价值,感兴趣的... 目录一、Clipboard API相关属性方法二、document.execCommand优点:缺点: