当前位置: 移动技术网 > IT编程>移动开发>Android > Android基础知识之tween动画效果

Android基础知识之tween动画效果

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

幻想神域精铸的奥秘,独弦琴之思,吴建融

android中一共提供了两种动画,其一便是tween动画,tween动画通过对view的内容进行一系列的图像变换(包括平移,缩放,旋转,改变透明度)来实现动画效果,动画效果的定义可以使用xml,也可以使用编码来实现。 下面我们逐一查看tween能够实现的动画效果。
先看看工程的整体结构吧:

这里写图片描述

我们要实现的效果图如图

这里写图片描述

点击按钮则执行相应的动画操作。

布局文件activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.tweentest.mainactivity" >

 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <button
  android:id="@+id/alpha"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="透明" />

  <button
  android:id="@+id/scale"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="缩放" />

  <button
  android:id="@+id/rotate"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="旋转" />

  <button
  android:id="@+id/translate"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="移动" />

  <button
  android:id="@+id/combo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="组合" />
 </linearlayout>

 <button
  android:id="@+id/go_other_activity"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onclick="doanim"
  android:text="go!" />
 </linearlayout>

 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

 <imageview
  android:id="@+id/image1"
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:src="@drawable/jiafeimao" />
 </linearlayout>

</linearlayout>

好了,先来看第一个动画效果。
要实现让图片变成透明的动画效果,先在anim文件夹中新建一个名为alpha的xml文件,关于透明的动画效果都写在这个文件中:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >

 <!-- 花1000毫秒,从不透明(1)变为透明(0) -->
 <!--
 repeatcount表示重复次数
 repeatmode表示开始的模式,restart表示每次重新开始,reverse表示接着来,即不透明->透明->不透明
 -->
 <alpha
 android:duration="2000"
 android:fromalpha="1"
 android:repeatcount="5"
 android:repeatmode="reverse"
 android:toalpha="0" />

</set>

duration属性表示动画执行的时间,以毫秒为单位,repeatcount表示动画重复的次数,repeatmode属性有两个值,一个是restart,表示动画每次执行完之后都重新开始执行(不透明->透明,不透明->透明….),reverse表示动画每次执行完之后不重新开始而是接着反向执行(不透明->透明->不透明->透明….),换句话说,如果是reverse,则表示动画从不透明变为透明后再慢慢变回不透明,如果是restart则表示动画从不透明变为透明后,然后快速恢复原状。以上这三个属性在所有的tween动画中都有,也是最常用的属性。fromalpha表示初始透明度,1表示不透明,0表示完全透明;toalpha表示动画结束时的透明度。

这是动画的xml文件,再看看java代码是怎样的。

public class mainactivity extends activity {

 private imageview iv = null;
 private animation animation = null;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 this.iv = (imageview) this.findviewbyid(r.id.image1);
 }
 public void doanim(view v){
 switch (v.getid()) {
 case r.id.alpha:
  //读取动画文件
  animation = animationutils.loadanimation(this, r.anim.alpha);
  //动画结束之后使用什么页面填充
  //使用动画结束后的样子填充,即保持结束时的画面
  animation.setfillafter(true);
  //应用动画特效
  iv.startanimation(animation);
  break;
 case r.id.scale:
  animation = animationutils.loadanimation(this, r.anim.scale);
  iv.startanimation(animation);
  break;
 case r.id.rotate:
  animation = animationutils.loadanimation(this, r.anim.rotate);
  iv.startanimation(animation);
  break;
 case r.id.translate:
  animation = animationutils.loadanimation(this, r.anim.translate);
  iv.startanimation(animation);
  break;
 case r.id.combo:
  animation = animationutils.loadanimation(this, r.anim.combo);
  iv.startanimation(animation);
  break;
 case r.id.go_other_activity:
  intent intent = new intent(mainactivity.this,secondactivity.class);
  startactivity(intent);
  //设置让mainactivity从左边出,secondactivity从右边进
  overridependingtransition(r.anim.enter_anim, r.anim.exit_anim);
  break;
 }
 }
}

先拿到imageview,然后在点击按钮时读取动画文件,并给imageview设置动画效果。

缩放动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >

 <!--
 两个1表示开始时图像大小不变,两个2表示图像放大一倍,
 如果小于1则表示图像缩小。两个0表示基于左上角进行放大
 pivotx如果是50%,表示基于图像中心放大
 pivotx如果是50%p,表示基于父级中心放大 
 infinite如它的英文意思,无限循环

 -->
 <scale
 android:duration="1000"
 android:fromxscale="1"
 android:fromyscale="1"
 android:pivotx="0"
 android:pivoty="0"
 android:repeatcount="infinite"
 android:repeatmode="reverse"
 android:toxscale="2"
 android:toyscale="2" />

</set>

fromxscale和fromyscale分别表示初始时图像的缩放比例,1表示不缩放,toxscale和toyscale表示动画结束时的缩放比例,2表示动画放大一倍,如果是0.5则表示缩小一倍。pivotx和pivoty表示执行缩放时的参考点,两个值都为0表示是基于图像左上角来执行缩放操作,如果都是50%表示基于图像中心来执行缩放操作,如果是100%表示基于图像右下角执行缩放操作,如果是50%p,表示基于屏幕的中心执行缩放操作,如果是100%p表示基于屏幕的右下角执行缩放操作。

java代码见上文。

旋转动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >

 <!-- fromdegrees表示开始的角度
 todegrees结束的角度,180表示旋转半圈
 两个100%表示基于自己的右下角旋转
 想让倒着转,把180改为-180即可
 -->
 <rotate
 android:duration="1000"
 android:fromdegrees="0"
 android:pivotx="100%"
 android:pivoty="100%"
 android:todegrees="180" />

</set>

这里有两个参数解释,fromdegrees表示初始角度,0表示正常,todegrees表示旋转角度,180表示顺时针旋转180度,-180表示逆时针旋转180度pivotx参数的意义和缩放动画中的参数意义相同。

java代码同上文。

移动动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >
<!-- from表示从哪里开始移动
to表示移动到那里
100%表示移动到自己的右下角
100%p表示移动到屏幕右下角
 -->
 <translate
 android:repeatcount="infinite"
 android:repeatmode="reverse"
 android:duration="1000"
 android:fromxdelta="0"
 android:fromydelta="0"
 android:toxdelta="100%"
 android:toydelta="100%" />

</set>

fromxdelta表示初始时图像在x轴的位置toxdelta表示结束时图像在x轴的位置。

四种动画效果都已经说完,如果要实现组合效果呢?

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >

 <rotate
 android:duration="1000"
 android:fromdegrees="0"
 android:pivotx="100%"
 android:pivoty="100%"
 android:todegrees="180" />

 <alpha
 android:duration="2000"
 android:fromalpha="1"
 android:repeatcount="5"
 android:repeatmode="reverse"
 android:toalpha="0" />

 <scale
 android:duration="1000"
 android:fromxscale="1"
 android:fromyscale="1"
 android:pivotx="0"
 android:pivoty="0"
 android:repeatcount="infinite"
 android:repeatmode="reverse"
 android:toxscale="2"
 android:toyscale="2" />

 <translate
 android:duration="1000"
 android:fromxdelta="0"
 android:fromydelta="0"
 android:repeatcount="infinite"
 android:repeatmode="reverse"
 android:toxdelta="100%"
 android:toydelta="100%" />

</set>

把之前所有的动画效果放在一个文件中就可以了。

android中的动画效果可以对任何组件使用,因为组件都是继承自view,而startanimation(animation animation)方法就是view中的方法。那么两个activity之间切换能不能使用动画效果呢?当然可以。上文中的java代码中,有这么一句:

case r.id.go_other_activity:
  intent intent = new intent(mainactivity.this,secondactivity.class);
  startactivity(intent);
  //设置让mainactivity从左边出,secondactivity从右边进
  overridependingtransition(r.anim.enter_anim, r.anim.exit_anim);
  break;

想要实现activity之间切换的动画,使用overridependingtransition(r.anim.enter_anim, r.anim.exit_anim);方法即可,该方法要两个参数,分别表示新activity进入时的动画和旧activity出去时的动画。

进入时动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >
 <translate
 android:duration="1000"
 android:fromxdelta="100%"
 android:fromydelta="0"
 android:toxdelta="0"
 android:toydelta="0" />

</set>

出去时动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareinterpolator="false" >
 <translate
 android:duration="1000"
 android:fromxdelta="0"
 android:fromydelta="0"
 android:toxdelta="-100%"
 android:toydelta="0" />

</set>

效果如图:

这里写图片描述

mainactivity逐渐移出,secondactivity逐渐从右边进来。

原文链接:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

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

相关文章:

验证码:
移动技术网