当前位置: 移动技术网 > IT编程>移动开发>Android > Android xml实现animation的4种动画效果实例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

吴桥新闻,永年标准件网,少林武王演员表

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;
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.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);
}
}

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


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网