零、前言

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

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

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

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

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

       

一、写个自定义的ProcessDialog

  1. using Android.App;
  2. using Android.Content;
  3. using Android.Graphics.Drawables;
  4. using Android.Views;
  5. using Android.Widget;
  6. namespace Catcher.AndroidDemo.LoadingAnimationDemo.Extensions
  7. {
  8. public class CustomProgressDialog : Dialog
  9. {
  10. private static CustomProgressDialog customProgressDialog;
  11.  
  12. public CustomProgressDialog(Context context):base(context)
  13. {
  14. }
  15. public CustomProgressDialog(Context context, int themeResId):base(context,themeResId)
  16. {
  17. }
  18. /// <summary>
  19. /// create the dialog
  20. /// </summary>
  21. /// <param name="context">the context</param>
  22. /// <returns>the instance of the customize dialog</returns>
  23. public static CustomProgressDialog CreateDialog(Context context)
  24. {
  25. customProgressDialog = new CustomProgressDialog(context, Resource.Style.CustomProgressDialog);
  26. //set the view
  27. customProgressDialog.SetContentView(Resource.Layout.loading);
  28. //set the gravity
  29. customProgressDialog.Window.Attributes.Gravity = GravityFlags.Center;
  30. return customProgressDialog;
  31. }
  32. /// <summary>
  33. /// called whenever the window focus changes
  34. /// </summary>
  35. /// <param name="hasFocus">whether the window now has focus</param>
  36. public override void OnWindowFocusChanged(bool hasFocus)
  37. {
  38. base.OnWindowFocusChanged(hasFocus);
  39. if (customProgressDialog == null)
  40. {
  41. return;
  42. }
  43. ImageView imageView = customProgressDialog.FindViewById<ImageView>(Resource.Id.loadingImageView);
  44. AnimationDrawable animationDrawable = (AnimationDrawable)imageView.Background;
  45. //start the animation
  46. animationDrawable.Start();
  47. }
  48. }
  49. }

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

二、新建一个loading.xml

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

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item
  4. android:drawable="@drawable/loading0"
  5. android:duration="120"/>
  6. <item
  7. android:drawable="@drawable/loading1"
  8. android:duration="120"/>
  9. <item
  10. android:drawable="@drawable/loading2"
  11. android:duration="120"/>
  12. </animation-list>

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:id="@+id/loadingImageView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:background="@anim/loading" />
  11. <TextView
  12. android:id="@+id/tv_loadingmsg"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center"
  16. android:textSize="12sp"
  17. android:text="loading....." />
  18. </LinearLayout>

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

四、编写Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <Button
  7. android:id="@+id/go"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="go!go!go!" />
  11. </LinearLayout>

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

  1. using Android.App;
  2. using Android.Content;
  3. using Android.OS;
  4. using Android.Widget;
  5. using Catcher.AndroidDemo.LoadingAnimationDemo.Extensions;
  6. using System.Threading.Tasks;
  7. namespace Catcher.AndroidDemo.LoadingAnimationDemo
  8. {
  9. [Activity(Label = "Catcher.AndroidDemo.LoadingAnimationDemo", MainLauncher = true, Icon = "@drawable/icon")]
  10. public class MainActivity : Activity
  11. {
  12. CustomProgressDialog dialog;
  13. protected override void OnCreate(Bundle bundle)
  14. {
  15. base.OnCreate(bundle);
  16. SetContentView(Resource.Layout.Main);
  17. //create a new dialog
  18. dialog = CustomProgressDialog.CreateDialog(this);
  19. dialog.OnWindowFocusChanged(true);
  20.  
  21. Button btnGO = FindViewById<Button>(Resource.Id.go);
  22. btnGO.Click += (s,e) =>
  23. {
  24. int result = ;
  25. //show the dialog
  26. dialog.Show();
  27. //do some things
  28. Task task = new Task(() =>
  29. {
  30. for (int i = ; i < ; i++)
  31. {
  32. result += i;
  33. }
  34. });
  35. task.ContinueWith(t =>
  36. {
  37. Intent intent = new Intent(this, typeof(LastActivity));
  38. intent.PutExtra("name", result.ToString());
  39. StartActivity(intent);
  40. });
  41. task.Start();
  42. };
  43. }
  44. protected override void OnResume()
  45. {
  46. base.OnResume();
  47. if(dialog.IsShowing)
  48. {
  49. dialog.Dismiss();
  50. }
  51. }
  52. }
  53. }

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

五、编写last.axml和LastActivity

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

取出结果放到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. PHP 文件处理

    $handler = fopen('./abc.html', 'w'); if(!feof($handler)){ // 读取文件末尾,也可以用file_exists mkdir('./abc.htm ...

  2. for循环中的占位 pass

  3. 解读ASP.NET 5 & MVC6系列(1):ASP.NET 5简介

    ASP.NET 5简介 ASP.NET 5是一个跨时代的改写,所有的功能和模块都进行了独立拆分,做到了彻底解耦.为了这些改写,微软也是蛮 拼的,几乎把.NET Framwrok全部改写了一遍,形成了一 ...

  4. 小菜学习设计模式(五)—控制反转(Ioc)

    写在前面 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Metho ...

  5. ARM的常数表达式

    ARM的常数表达式   如果说Intel指令中的立即数,相信大家都很熟悉.类似的,Arm指令中的“立即数”就是常数表达式.之所以称为常数表达式,而不称为立即数是有原因的. Intel指令属于CISC指 ...

  6. 自己动手写文件查找,字符串查找,查询jar包等工具

    文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (39) ------ 第七章 使用对象服务之配置模型和使用单复数服务

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-3  配置模型 问题 你想了解配置模型中的各种选项. 解决方案 当你添加一个AD ...

  8. fc-san

    在SAN网络中,所有的数据传输在高速.高带宽的网络中进行,SAN存储实现的是直接对物理硬件的块级存储访问,提高了存储的性能和升级能力. 早期的SAN采用的是光纤通道(FC,Fibre Channel) ...

  9. 特邀美国EMC实战专家Mark来华授课

    “轻松搞定EMC-PCB和系统设计”课程介绍 本次课程特邀美国EMC领域权威专家Mark Montrose主讲,将涵盖满足产品电磁兼容性和信号完整性的基本原理.课程涉及多个领域,不仅仅针对PCB设计, ...

  10. 《JS修炼之道》—— 读后总结

    本篇是基于<JS修炼之道>的记录性与总结性的文章,这本书从多种框架的角度,讲述了JS开发中的一些实用技巧. 比如Prototype,JQuery,Mootools,YUI,Dojo,Ext ...