Translate动画

这个动画是最常使用到的,主要就是将控件从一个位置移动到另一个位置,并且还可以在这其中增加一定的效果,下面我们将采用两种方式实现动画,首选的是利用XML来制作动画,其次就是利用代码。

首先我们在Resources中新建一个名为anim的文件夹,然后在该文件夹下新建两个xml,分别命名为in_from_bottomout_from_bottom,然后我们将下面的代码写入其中:

in_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate android:startOffset="500" android:fromYDelta="0" android:toYDelta="80%p" android:duration="1000" />
</set>

out_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate android:startOffset="500" android:fromYDelta="80%p" android:toYDelta="0" android:duration="1000"/>
</set>

其中set标签表示一个动画集合,该标签下可以包含多个不同的动画,这样就可以将他们组合成一个动画,这里我们不需要过多的了解它,主要是理解translate标签,这个标签代表的就是滑动动画,其中各个属性的说明如下所示:

Interpolator:表示下面的动画的过渡形式,比如逐渐变慢或者逐渐变快。

startOffset:表示动画开始前的延迟(单位毫秒)

fromYDelta:表示动画开始的位置(其中80%p表示屏幕的80%的高度部分,对应的还有fromXDelta属性)

toYDelta:表示动画结束的位置(对应的还有toXDelta属性)

Duration:表示动画持续的时间(单位毫秒)

介绍完了具体属性,下面就是利用这个动画。首先我们新建一个活动,然后将其视图的xml改成如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/relativeLayout1"
p1:padding="5dp">
<TextView
p1:text="会动的TextView"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="@+id/tvAnim"
p1:gravity="center"
p1:padding="5dp"
p1:background="#00f"
p1:textSize="30dp" />
<Button
p1:text="消 失"
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:id="@+id/btnHide"
p1:layout_alignParentBottom="true" />
<Button
p1:text="出 现"
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:layout_toRightOf="@id/btnHide"
p1:id="@+id/btnShow"
p1:layout_alignParentBottom="true" />
</RelativeLayout>

对应的代码部分改成如下所示:

我们可以看到动画文件需要利用AnimationUtils这个静态类的LoadAnimation方法读取,然后将返回值传递给控件的StartAnimation方法,其中我们多了一行代码,就是FillAfter = true,如果不存在这个代码,我们会发现动画在结束后控件又回到原来的位置了,有些时候这并不是我们需要的,所以需要将FillAfter设置为True即可。

效果展示:

下面我们利用代码的形式实现跟上面一样的动画效果,我们直接在OnCreate中创建动画:

         protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.AnimationActivity); inAnim = new TranslateAnimation(Dimension.RelativeToParent, , Dimension.RelativeToParent, ,
Dimension.RelativeToParent, , Dimension.RelativeToParent, (float)0.8);
inAnim.FillAfter = true;
inAnim.StartOffset = ;
inAnim.Duration = ;
inAnim.SetInterpolator(this, Android.Resource.Animation.BounceInterpolator); outAnim = new TranslateAnimation(Dimension.RelativeToParent, , Dimension.RelativeToParent, ,
Dimension.RelativeToParent, (float)0.8, Dimension.RelativeToParent, );
outAnim.FillAfter = true;
outAnim.StartOffset = ;
outAnim.Duration = ;
outAnim.SetInterpolator(this, Android.Resource.Animation.BounceInterpolator); TvAnim = FindViewById<TextView>(Resource.Id.tvAnim);
FindViewById<Button>(Resource.Id.btnHide).Click += (e, s) =>
{
TvAnim.StartAnimation(inAnim);
}; FindViewById<Button>(Resource.Id.btnShow).Click += (e, s) =>
{
TvAnim.StartAnimation(outAnim);
};
}

我们实例化一个TranslateAnimation对象,后面的属性跟XML中一摸一样的,直接就可以使用,唯一的区别就是Interpolator需要通过SetInterpolator方法来进行设置。有时我们需要利用代码控制移动的距离,比如在FrameLayout布局下要让底层的控件呈现,就需要移动我们预想的值,但是TranslateAnimation只能接收px为单位的距离,我们就需要将DP转换成PX,笔者这里顺便将实现功能的带么也贴出来,方面有需要的人:

         public static int DpToPx(this Context context, float dp)
{
return (int)(context.Resources.DisplayMetrics.Density * dp + 0.5f);
}

Alpha动画

这个动画比较简单,所以笔者就不单独写了,就跟着上面的例子,直接在XML中增加这个动画,也正好可以证明set下的多个动画是可以同步执行的,通过这样的组合我们就可以作出很多非常炫酷的动画了。下面我们直接看对应的XML的代码:

out_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate android:startOffset="500" android:fromYDelta="80%p" android:toYDelta="0" android:duration="1000"/>
<alpha android:fromAlpha="0" android:startOffset="500" android:duration="1000" android:toAlpha="1" />
</set>

In_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate android:startOffset="500" android:fromYDelta="0" android:toYDelta="80%p" android:duration="1000"/>
<alpha android:fromAlpha="1" android:startOffset="500" android:duration="1000" android:toAlpha="0" />
</set>

其中我们可以看到alpha实际上只有fromAlphatoAlpha属性,其他的属性都是公用的,是不是非常的简单,然后我们再把活动的代码改回之前的样子,使用XML中定义的动画。

效果展示:

对应的代码形式,笔者这里简单的写下,不进行举例了:

 AlphaAnimation alpha = new AlphaAnimation(, );
alpha.Duration = ;
alpha.StartOffset = ;

PS:如果读者急切的想知道如果利用代码制作多个动画的组合,可以使用AnimationSet类,将对应的动画添加进去。

Rotate动画

顾名思义,就是翻转动画。这里为了下面能够看到动画的效果,我们需要将活动视图中的TextView的属性layout_centerInParent设置为true即可,紧接着我们将对应的XML文件进行修改:

In_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<rotate android:fromDegrees="180" android:toDegrees="0" android:startOffset="500" android:duration="1000" android:pivotX="50%" android:pivotY="50%" />
</set>

out_from_bottom:

 <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<rotate android:fromDegrees="0" android:toDegrees="180" android:startOffset="500" android:duration="1000" android:pivotX="50%" android:pivotY="50%" />
</set>

其中fromDegresestoDegrees就是从多少度翻转到多少度,pivotXpivotY则需要重点介绍,既然是翻转,自然要有中心。默认情况的中心就是左上角,通过给这两个值赋上float类型的值表示中点是根据左上角进行偏移,比如pivotX=5,pivotY=10,左上角的坐标是101,50。则最终的中点就是106,60了,当然我们也可以用百分比表示,比如都赋50%就表示中点为控件的中心,如果在后面加上p单位就表示中点是父控件的中心,明白了这些这个动画我们就能够很好的掌握了。

效果展示:

对应的代码形式如下所示:

 RotateAnimation rotate = new RotateAnimation(, , Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
rotate.Duration = ;
rotate.StartOffset = ;

Scale动画

这已经是我们最后一个介绍的动画了,下面我们不多说废话,直接修改XML:

Out_from_bottom:

 <scale android:fromXScale="0.2" android:toXScale="1" android:fromYScale="0.2" android:toYScale="1" android:pivotX="50%" android:pivotY="50%" android:duration="1000" />

In_from_bottom:

 <scale android:fromXScale="1" android:toXScale="0.2" android:fromYScale="1" android:toYScale="0.2" android:pivotX="50%" android:pivotY="50%" android:duration="1000" />

这里的pivotYpivotX跟上上节的使用方式是相同的,对应fromXScalefromYScaletoXScaletoYScale的作用就是X轴和Y轴上等比缩放的比例了。

效果展示:

对应的代码形式如下:

 ScaleAnimation scale = new ScaleAnimation(1f, 0.2f, 1f, 0.2f, Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
scale.FillAfter = true;
scale.Duration = ;

Interpolator属性可用参考图:

关于Xamarin下如何强制菜单在ActionBar中显示

             ViewConfiguration config = ViewConfiguration.Get(this);
var f = config.Class.GetDeclaredField("sHasPermanentMenuKey");
f.Accessible = true;
f.SetBoolean(config, false);

因为Android系统规定存在物理菜单键的情况下菜单是不会显示到ActionBar中的,所以我们需要通过修改ViewConfiguration中的私有字段sHasPermanentMenuKey将其改为false即可,但是在实际测试中发现,部分手机必须强制Menu的ActionFlags为Always。

关于Xamarin下使用Http报InvalidCastException异常

通过查阅官方资料发现这个是Xamarin本身的Bug,但是这个Bug实在是太大。会导致整个App的稳定性下降,重点是这个异常无法通过try…catch捕获,一旦发生就闪退,特别实在短时间内频繁使用Http的情况下,该解决方案只有将Xamarin.Android升级到4.12.5以及以上才可以(对于破解党来说又要开始折腾重新安装了)。

Xamarin.Android之动画的更多相关文章

  1. Xamarin.android Activity动画切换效果实现

    http://blog.csdn.net/esunshine1985/article/details/44302903 1.在Resources--values下新建styles.xml,添加内容如下 ...

  2. Xamarin Android组件篇教程RecylerView动画组件RecylerViewAnimators(1)

    Xamarin Android组件篇教程RecylerView动画组件RecylerViewAnimators(1) RecyclerView是比ListView和GridView更为强大的布局视图, ...

  3. Xamarin.Android之Splash的几种简单实现

    对现在的APP软件来说,基本上都会有一个Splash页面,类似大家常说的欢迎页面.启动界面之类的. 正常来说这个页面都会有一些相关的信息,比如一些理念,Logo,版本信息等 下面就来看看在Xamari ...

  4. Xamarin.Android绑定库分享

    使用Xamarin.Android时,会用到各种第三方库,而这些库基本上是java编写的,要在Xamarin.Android中使用这些库,就需要通过Android Binding Project绑定对 ...

  5. Xamarin.Android开发实践(十八)

    Xamarin.Android之SlidingMenu 一.前言 有位网友在评论中希望能够出个在Xamarin.Android下实现SlidingMenu效果的随笔,刚好昨天在观看官网示例项目的时候也 ...

  6. Xamarin.Android开发实践(三)

    原文:Xamarin.Android开发实践(三) 一.前言 用过Android手机的人一定会发现一种现象,当你把一个应用置于后台后,一段时间之后在打开就会发现应用重新打开了,但是之前的相关的数据却没 ...

  7. xamarin Android activity生命周期详解

    学Xamarin我为什么要写这样一篇关于Android 的activity生命周期的文章 已经学Xamarin android有一段时间了,现在想起当初Xamarin也走了不少的弯路.当然Xamari ...

  8. Xamarin.Android之SlidingMenu

    一.前言 有位网友在评论中希望能够出个在Xamarin.Android下实现SlidingMenu效果的随笔,刚好昨天在观看官网示例项目的时候也看到这个SlidingMenu,但是最终的效果并不是我们 ...

  9. Xamarin.Android开发

    使用 Visual Studio 生成第一个 Xamarin.Android 应用程序,并进一步了解使用 Xamarin 进行 Android 应用程序开发的基础知识.在此过程中,会介绍生成和部署 X ...

随机推荐

  1. Android IOS WebRTC 音视频开发总结(六七)-- 在线教育虽火要做好其实不容易

    本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 最近很多朋友在咨询在线教育的事(其实之 ...

  2. CAP理论(转)

    add by zhj: CAP理论可以简单的理解为一致性,可用性,可分区性,这三者没有办法同时满足.我们使用的关系型数据库,比如MySQL,Postgresql是CA类型, 而Redis,MongoD ...

  3. Python Django之路由系统

    1.初识路由系统 路由系统是用户发请求到URL,然后URL根据路由系统重新指向到函数名的一个对应关系 2.创建project和app django-admin startproject mysite ...

  4. centos7 ip地址设置

    centos7 ip地址设置 http://simonhu.blog.51cto.com/196416/1588971 CentOS系统 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc ...

  5. [转载] 2. JebAPI 之 jeb.api.dex

    本文转载自: https://www.zybuluo.com/oro-oro/note/142842 1. jeb.api.dex.Dex 这个类代表正在被JEB处理的DEX文件. 要想更好的了解这个 ...

  6. java1.7集合源码阅读: Vector

    Vector是List接口的另一实现,有非常长的历史了,从jdk1.0开始就有Vector了,先于ArrayList出现,与ArrayList的最大区别是:Vector 是线程安全的,简单浏览一下Ve ...

  7. flask--虚拟环境

    1.安装虚拟环境mosson@mosson:~$ sudo apt-get install virtualenv2.创建一个项目目录mosson@mosson:~$ mkdir myproject3. ...

  8. [转] Oracle analyze 命令分析

    转自:http://blog.sina.com.cn/s/blog_682841ba0101bncp.html 1.analyze table t1 compute statistics for ta ...

  9. zabbix网络发现

    zabbix的网络自动发现是一个非常强大的功能,该功能可以完成以下工作 •快速发现并添加主机. •简单的管理. •随着环境的改变而快速搭建监控系统. 网络发现基于以下信息 •ip地址段 •基于服务的F ...

  10. Infobright存储引擎的特点

    Infobright的优点: (1)高压缩比率 (2)快速响应复杂的分析查询语句 (3)随着数据库的逐渐增大,查询和装载性能基本保持稳定 (4)没有特殊的数据仓库模型(比如星状模型.雪花模型)要求 ( ...