零、前言

试想一下,我们的应用正在请求一些数据,假设网络不是很好,要花比较长的时间等待,这个时候界面什么反应也没有,

一动不动,用户可能就会认为应用挂掉了,这么久都没反应的,说不定下一分钟用户就把它卸载了。

这样就造成了十分不好的用户的体验。为此,我们可以在这个过程中加上那么点过渡效果,告诉用户正在加载数据。

稍微改善一下用户体验,至少能让用户知道我们的app在干嘛!

本文使用的是自定义ProcessDialog来实现过渡效果,准备了三张图片资源

       

一、写个自定义的ProcessDialog

 using Android.App;
using Android.Content;
using Android.Graphics.Drawables;
using Android.Views;
using Android.Widget;
namespace Catcher.AndroidDemo.LoadingAnimationDemo.Extensions
{
public class CustomProgressDialog : Dialog
{
private static CustomProgressDialog customProgressDialog; public CustomProgressDialog(Context context):base(context)
{
}
public CustomProgressDialog(Context context, int themeResId):base(context,themeResId)
{
}
/// <summary>
/// create the dialog
/// </summary>
/// <param name="context">the context</param>
/// <returns>the instance of the customize dialog</returns>
public static CustomProgressDialog CreateDialog(Context context)
{
customProgressDialog = new CustomProgressDialog(context, Resource.Style.CustomProgressDialog);
//set the view
customProgressDialog.SetContentView(Resource.Layout.loading);
//set the gravity
customProgressDialog.Window.Attributes.Gravity = GravityFlags.Center;
return customProgressDialog;
}
/// <summary>
/// called whenever the window focus changes
/// </summary>
/// <param name="hasFocus">whether the window now has focus</param>
public override void OnWindowFocusChanged(bool hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
if (customProgressDialog == null)
{
return;
}
ImageView imageView = customProgressDialog.FindViewById<ImageView>(Resource.Id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.Background;
//start the animation
animationDrawable.Start();
}
}
}

比较简单,就是写了个创建的方法和重写了Dialog的OnWindowFocusChanged方法

二、新建一个loading.xml

loading.xml可以放在Resources下面的drawable文件夹,也可放在新建的anim文件夹。

 <?xml version="1.0" encoding="utf-8" ?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/loading0"
android:duration="120"/>
<item
android:drawable="@drawable/loading1"
android:duration="120"/>
<item
android:drawable="@drawable/loading2"
android:duration="120"/>
</animation-list>

这个就是设置我们的动画图片。

三、给自定义Dialog写个简单的布局loading.axml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/loading" />
<TextView
android:id="@+id/tv_loadingmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="12sp"
android:text="loading....." />
</LinearLayout>

一个ImageView用来显示动画,一个TextView用来显示loading

四、编写Main.axml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/go"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="go!go!go!" />
</LinearLayout>

五、在MainActivity中使用自定义的Dialog

 using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Catcher.AndroidDemo.LoadingAnimationDemo.Extensions;
using System.Threading.Tasks;
namespace Catcher.AndroidDemo.LoadingAnimationDemo
{
[Activity(Label = "Catcher.AndroidDemo.LoadingAnimationDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
CustomProgressDialog dialog;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//create a new dialog
dialog = CustomProgressDialog.CreateDialog(this);
dialog.OnWindowFocusChanged(true); Button btnGO = FindViewById<Button>(Resource.Id.go);
btnGO.Click += (s,e) =>
{
int result = ;
//show the dialog
dialog.Show();
//do some things
Task task = new Task(() =>
{
for (int i = ; i < ; i++)
{
result += i;
}
});
task.ContinueWith(t =>
{
Intent intent = new Intent(this, typeof(LastActivity));
intent.PutExtra("name", result.ToString());
StartActivity(intent);
});
task.Start();
};
}
protected override void OnResume()
{
base.OnResume();
if(dialog.IsShowing)
{
dialog.Dismiss();
}
}
}
}

在这里没有真正的用网络请求,而是用计算累加和来代替。

五、编写last.axml和LastActivity

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
android:textSize="50sp"
android:gravity="center"
android:layout_marginTop="20dp" />
</LinearLayout>
 using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
namespace Catcher.AndroidDemo.LoadingAnimationDemo
{
[Activity(Label = "LastActivity")]
public class LastActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.last);
TextView tvShow = FindViewById<TextView>(Resource.Id.tv_show);
tvShow.Text = Intent.GetStringExtra("name");
}
}
}

取出结果放到TextView中。

六、效果图

示例代码下载:

https://github.com/hwqdt/Demos/tree/master/src/Catcher.AndroidDemo/Catcher.AndroidDemo.LoadingAnimationDemo

Xamarin.Android之给我们的应用加点过渡效果的更多相关文章

  1. XAMARIN.ANDROID SIGNALR 实时消息接收发送示例

    SignalR 是一个开发实时 Web 应用的 .NET 类库,使用 SignalR 可以很容易的构建基于 ASP.NET 的实时 Web 应用.SignalR 支持多种服务器和客户端,可以 Host ...

  2. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  3. 我正在使用Xamarin的跨平台框架—Xamarin.Android回忆录

    一.缘起 在自己给别家公司做兼职外包的时候,已经明确知道外包的活不是那么好干的,一般在经历了初期热血澎湃的激情后,逐渐冷淡,愤怒,再冷淡,再愤怒…,听上去好像高潮迭起,但令人尴尬的是,这高潮迭起我们都 ...

  4. APP并非一个人在战斗,还有API—Xamarin.Android回忆录

    前言 一般来说,一个客户端APP并非独立存在的,很多时候需要与服务器交互.大体可分为两方面的数据,常规字符串数据和文件数据,因为这两种数据很可能传输方式不一样,比如字符串之类的数据,使用HTTP协议, ...

  5. Xamarin.Android通知详解

    一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...

  6. Xamarin.Android之SQLiteOpenHelper

    一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...

  7. Xamarin. Android实现下拉刷新功能

    PS:发现文章被其他网站或者博客抓取后发表为原创了,给图片加了个水印 下拉刷新功能在安卓和iOS中非常常见,一般实现这样的功能都是直接使用第三方的库,网上能找到很多这样的开源库.然而在Xamarin. ...

  8. Xamarin Android 之起始篇

    序言: 在博客园注册了已经有2年多了,快三年了.从开始学习这一行开始就在博客园注册了这个账号.至今也还没有写过一篇随笔,大多时候都是在园子里头潜水,看大牛写的文章,学习. 写博客不为啥,就是自己对自己 ...

  9. [译]:Xamarin.Android平台功能——位置服务

    返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...

随机推荐

  1. c#多线程介绍(上)

    转载原文:这里是链接内容 转载原文:这里写链接内容 转载原文:这里写链接内容 (重要事情说三遍) 引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个 ...

  2. CocoaPods pod install

    加参数可以提升更新的速度 方法1: pod install --verbose --no-repo-update pod update --verbose --no-repo-update 方法2: ...

  3. Code First开发系列之管理并发和事务

    返回<8天掌握EF的Code First开发>总目录 本篇目录 理解并发 理解积极并发 理解消极并发 使用EF实现积极并发 EF的默认并发 设计处理字段级别的并发应用 实现RowVersi ...

  4. ASP.Net WebForm温故知新学习笔记:一、aspx与服务器控件探秘

    开篇:毫无疑问,ASP.Net WebForm是微软推出的一个跨时代的Web开发模式,它将WinForm开发模式的快捷便利的优点移植到了Web开发上,我们只要学会三步:拖控件→设属性→绑事件,便可以行 ...

  5. js框架模版

    (function() { //注册命名空间zzw到window对象上 window['zzw'] = {} //定义一个$函数 function $() { alert("hello $& ...

  6. 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法

    前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...

  7. Hadoop学习笔记——搭建

    一搭建环境列表 操作系统:centos6.5 64位 JDK环境:jdk1.7.0_71 hadoop版本:社区版本2.7.2,hadoop-2.7.2-src.tar.gz 主机名 ip 角色 用户 ...

  8. rem单位和em单位的使用

    今天弄了一点响应式的东西,本以为很快就可以弄好,结果还是绕晕了头,所以还是写下来方便下次看吧! 一开始我打算用百分比%来做响应式布局,后来算的很懵圈,就果断放弃了,哈哈是不是很明智. 接下来就是rem ...

  9. c#写windows服务

    序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...

  10. 【Win10 应用开发】解决VS 2015 RC不能调试手机应用的问题

    VS2015 RC已发布,当然这个版本还不能用于实际生产中,如果你没有测试环境,就等正式版出来,RC都来了,RTM就不远了. 如果你也像老周一样,已经在耍RC版了,你可能会遇到下面问题: 安装Win ...