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. 学习java第7天

    关于继承还需要留意的是,子类中的所有构造方法都默认访问父类的无参构造,注意是无参,而且是必须的,如果父类没有无参子类就会报错.如果你不想给父类无参构造,那么在子类中加上super(),显式的调用有参构 ...

  2. python学习笔记:文件操作和集合(转)

    转自:http://www.nnzhp.cn/article/16/ 这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句 ...

  3. Bootstrap 更改Navbar默认样式

    alt+shift+n 新建文件ctrl+shift+/ 注释ctrl+shift+f 重新排版代码ctrl+/ 注释 /* navbar */.navbar-default { background ...

  4. Kafka深入理解-2:Kafka的Log存储解析

    摘自http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互 ...

  5. centos安装zendopcache

    由于linux用的还不熟,导致很简单的一次安装过程遇到一堆问题,还好最后安装成功了,备忘就写在这里了. zendopcache的的主要原理: PHP执行后的数据缓冲到内存中避免重复的编译,能够直接使用 ...

  6. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

  7. perl中的运算符

    字符计算的运算符

  8. Gradle Android Studio basic

    1. change gradle version in gradle/wrapper/gradle-wrapper.properties  change distributionUrl=http\:/ ...

  9. max10中对DDR数据的采样转换

    (1)发现IP是这样处理DDR的数据:上长沿采的数据放在低位,下降沿采的数据在高位 (2)对于视频的行场信号是在下降沿采集,再延时一拍才能与数据对齐.

  10. ajax头像上传

    html代码: <input id="fileinput" type="file" /><br /> <br /> < ...