Android xml实现animation的4种动画效果实例代码

Android xml实现animation的4种动画效果实例代码,第1张

概述animation有四种动画类型:分别为alpha(透明的渐变)、rotate(旋转)、scale(尺寸伸缩)、translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML,而我今天要说的是XML实现的方法,个人感觉javaCode的实

animation有四种动画类型:分别为Alpha(透明的渐变)、rotate(旋转)、scale(尺寸伸缩)、translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML,而我今天要说的是XML实现的方法,个人感觉javaCode的实现方法比xml要简单,所以有需要的可以自己去找找资料看看。

先给大家展示下效果图,如果大家感觉还不错,请继续往下阅读。

下面是我的四个xml文件,分别代表这四种动画类型。

Alpha.xml

COde:

<?xml version="1.0" enCoding="UTF-8"?><set xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:interpolator="@androID:anim/accelerate_interpolator"><!-- 渐变透明的动画效果 --><!--fromAlpha 动画起始透明的 1.0完全不透明toAlpha 动画结束时透明的 0.0完全透明startOffset 设置启动时间duration 属性动画持续时间--><Alpha androID:fromAlpha="1.0"androID:toAlpha="0.0"androID:startOffset="500"androID:duration="5000"/></set>

rotate.xml

<?xml version="1.0" enCoding="UTF-8"?><set xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:interpolator="@androID:anim/accelerate_interpolator"><!-- 画面转移旋转动画效果 --><!--fromdegrees开始角度todegrees结束角度pivotX设置旋转时的X轴坐标--><rotate androID:fromdegrees="0"androID:todegrees="+360"androID:pivotX="50%"androID:pivotY="50%"androID:duration="5000"/></set>

scale.xml

<?xml version="1.0" enCoding="UTF-8"?><set xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:interpolator="@androID:anim/accelerate_interpolator"><!-- 渐变尺寸伸缩动画效果 --><!--fromXScale 起始x轴坐标toXScale 止x轴坐标fromYScale 起始y轴坐标toYScale 止y轴坐标pivotX 设置旋转时的X轴坐标pivotY 设置旋转时的Y轴坐标duration 持续时间--><scale androID:fromXScale="1.0"androID:toXScale="0.0"androID:fromYScale="1.0"androID:toYScale="0.0"androID:pivotX="50%"androID:pivotY="50%"androID:duration="5000"/></set>

translate.xml

<?xml version="1.0" enCoding="UTF-8"?><set xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:interpolator="@androID:anim/accelerate_interpolator"><!-- 画面转移位置移动动画效果 --><translate androID:fromXDelta="0%"androID:toXDelta="100%"androID:fromYDelta="0%"androID:toYDelta="0%"androID:duration="5000" /></set>

下面是主界面xml的布局

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical" ><ImageVIEw androID:ID="@+ID/image1"androID:layout_wIDth="match_parent"androID:layout_height="200px"/><ImageVIEw androID:ID="@+ID/image2"androID:layout_wIDth="match_parent"androID:layout_height="200px"/><ImageVIEw androID:ID="@+ID/image3"androID:layout_wIDth="match_parent"androID:layout_height="200px"/><ImageVIEw androID:ID="@+ID/image4"androID:layout_wIDth="match_parent"androID:layout_height="200px"/></linearLayout>

然后是Activity代码

public class AnimationDemo extends Activity{private Animation animation,animation1,animation2,animation3;private ImageVIEw image1,image2,image3,image4;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {// Todo auto-generated method stubsuper.onCreate(savedInstanceState);setContentVIEw(R.layout.animation);initVIEw();}public voID initVIEw(){animation=AnimationUtils.loadAnimation(AnimationDemo.this,R.anim.rotate);animation1=AnimationUtils.loadAnimation(AnimationDemo.this,R.anim.scale);animation2=AnimationUtils.loadAnimation(AnimationDemo.this,R.anim.Alpha);animation3=AnimationUtils.loadAnimation(AnimationDemo.this,R.anim.translate);image1=(ImageVIEw)findVIEwByID(R.ID.image1);image1.setimageResource(R.drawable.jpeg);image2=(ImageVIEw)findVIEwByID(R.ID.image2);image2.setimageResource(R.drawable.jpg);image3=(ImageVIEw)findVIEwByID(R.ID.image3);image3.setimageResource(R.drawable.png);image4=(ImageVIEw)findVIEwByID(R.ID.image4);image4.setimageResource(R.drawable.gif);image1.startAnimation(animation);image2.startAnimation(animation1);image3.startAnimation(animation2);image4.startAnimation(animation3);}}

好了,就这样就是先了四种动画效果,另外还有一个知识点,是动画里面的速率问题,有需要的可以去上网百度看看吧。


总结

以上是内存溢出为你收集整理的Android xml实现animation的4种动画效果实例代码全部内容,希望文章能够帮你解决Android xml实现animation的4种动画效果实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1149227.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存