在移动应用开发过程中经常会使用到图片展示场景,例如利用多张图片说明一个产品的特点,此处就会使用到ImageSwithcher,当然也可以使用ViewFliper来实现,但使用ViewFliper的时候会资源释放的问题,需要手动进行操作,这点在以后的文章中讲述。

要使用ImageSwithcher,首先需要在界面文件中添加ImageSwithcher,然后在代码中为ImageSwithcher指定图片加载方法以及触控方法。如下为完成之后的界面:

下面讲述具体的实现方法:

界面Xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout1"
android:background="#ffececec">
<ImageView
android:src="@drawable/ImgSlideLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnFeatureLeft"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<ImageSwitcher
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="1180px"
android:layout_height="638px"
android:id="@+id/SwitcherProductFeature"
android:layout_centerInParent="true" />
<ImageView
android:src="@drawable/ImgSlideRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnFeatureRight"
android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
</RelativeLayout>

实现代码为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace MobileInsured.Activitys
{
[Activity (Label = "ProductFeatureActivity")]
public class ProductFeatureActivity : ContentActivity,ViewSwitcher.IViewFactory,GestureDetector.IOnGestureListener
{
private GestureDetector gestureDetector = null;
private ImageSwitcher imageSwitcher;
private int[] imgs = new int[]{
Resource.Drawable.ImgProductFeature11,
Resource.Drawable.ImgProductFeature12,
Resource.Drawable.ImgProductFeature13
};
private int currentPosition;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle); // Create your application here
SetContent (Resource.Layout.ProductFeature);
gestureDetector = new GestureDetector(this);
var productId = Intent.GetStringExtra ("ProductId");
SetTitle ("产品特色");
imageSwitcher = FindViewById<ImageSwitcher> (Resource.Id.SwitcherProductFeature);
currentPosition = ;
imageSwitcher.SetFactory (this);
imageSwitcher.SetImageResource (Resource.Drawable.ImgProductFeature11);
} public View MakeView(){
ImageView img = new ImageView (this);
img.SetBackgroundColor (Android.Graphics.Color.Transparent);
img.SetScaleType (ImageView.ScaleType.Center);
return img;
} public override bool OnTouchEvent(MotionEvent e)
{
return gestureDetector.OnTouchEvent(e);
} public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if (e2.GetX() - e1.GetX() > ) {
currentPosition--;
if (currentPosition < )
currentPosition = imgs.Length - ;
imageSwitcher.SetInAnimation (this, Resource.Animation.LeftIn);
imageSwitcher.SetOutAnimation (this, Resource.Animation.RightOut);
imageSwitcher.SetImageResource (imgs [currentPosition]);
} else if (e2.GetX() - e1.GetX() < -) {
currentPosition++;
if (currentPosition > imgs.Length - )
currentPosition = ;
imageSwitcher.SetInAnimation (this, Resource.Animation.RightIn);
imageSwitcher.SetOutAnimation (this, Resource.Animation.LeftOut);
imageSwitcher.SetImageResource (imgs [currentPosition]);
}
return true;
} public bool OnDown(MotionEvent e)
{
return false;
} public void OnLongPress(MotionEvent e)
{
} public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
} public void OnShowPress(MotionEvent e)
{
} public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
}
}

如上代码所示,主要的地方时为ImageSwithcher设定Factory,声明MakeView方法,并根据需要设定具体手势判断方法。

Xamarin开发Android笔记:图片切换ImageSwitcher的更多相关文章

  1. Xamarin开发Android笔记:背景操作

    使用Xamarin开发Android UI的时可能会遇到一些场景背景的问题,虽然可以利用多层或直接使用图片背景来完成,但这样会增加不少的资源消耗,最终导致内存溢出的情况.最好的方法还是利用内部方法或代 ...

  2. Xamarin开发Android笔记:拍照或相册选取图片角度问题

    在开发Android应用的时候,可能会遇到类似微信朋友圈中拍照或相册选取图片的场景,拍照或选取图片之后在显示的时候却发现图片的角度不对,明明是竖版拍照,显示出来缺失躺着的. 这是因为在某些特定手机上例 ...

  3. Xamarin开发Android笔记:使用ZXing进行连续扫描

    在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用.不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了. 在Xama ...

  4. Xamarin开发IOS笔记:切换输入法时输入框被遮住

    在进行IOS开发的过程中,出现类似微信朋友圈的交互界面,当用户遇到感兴趣的内容可以进行评论.为了方便评论输入,当出现评论输入框的时候自动将评论输入框移动至键盘的上方,这样方便边输入边查看. 当用户隐藏 ...

  5. Xamarin开发Android笔记:TextView行间距设定

    TextView 在使用TextView的时候会遇到调整行间距的问题,可通过Layout文件添加属性完成,具体属性如下: //设置行间距,如”3dp”. android:lineSpacingExtr ...

  6. 【Xamarin开发 Android 系列 13】 应用打包部署

    原文:[Xamarin开发 Android 系列 13] 应用打包部署 开始倒叙咯................ 先更新大宝部署吧,这个章节比较的Easy,童鞋们不用费脑筋.点解?从界面上填写几个参 ...

  7. 【Xamarin开发 Android 系列 7】 Android 结构基础(下)

    原文:[Xamarin开发 Android 系列 7] Android 结构基础(下) *******前期我们不打算进行太深入的东西,省的吓跑刚进门的,感觉门槛高,so,我们一开始就是跑马灯一样,向前 ...

  8. 【Xamarin开发 Android 系列 3】循序渐进的学习顺序

    原文:[Xamarin开发 Android 系列 3]循序渐进的学习顺序 指定合理的学习步骤,将各个技术点进行强化.慢慢 的就从点到线 到面的飞跃,一切仅仅是时间问题,开始前,请记住,学习是最佳的投资 ...

  9. xamarin开发android收集的一些工具

    xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Vi ...

随机推荐

  1. js体验

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. The first day!!!

    我的小院子开通啦,开始记录自己的学习历程,加油!!!

  3. Corba、protocol buffer、SOA的区别 (转)

    From: http://www.zhihu.com/question/20279489 Google的protocol buffers?这个跟corba.soa没啥关系,不同层次的概念,没法比.pr ...

  4. C/C++操作MySQL数据库——增、删、改、查

    1.数据库链接 int cppDatebase::DatabaseConnect(sBit8 *uName,sBit8 *pWord,sBit8 *dbName) { dbHandle = mysql ...

  5. Shape comparison language

      形状比较语言, 九交模型 In this topic About shape comparison language Dimensionality Extensions to the CBM SC ...

  6. VC++ 结束线程 AfxBeginThread AfxEndThread

    如果你的线程是从CWinThread继承出来的,结束自己就用AfxEndThread, 如果是外部调用的话,可以用PostThreadMessage(m_nThreadID, WM_QUIT,0,0) ...

  7. 技术英文单词贴--V

    V validate 验证,确认,使生效 verify 核实,查实,验证 version 版本,译文 via 通过,凭借,经过 prep

  8. 使用 IntraWeb (41) - 数据控件速查

    TIWDBCheckBox 所在单元及继承链: IWDBStdCtrls.TIWDBCheckBox 主要成员: property AutoEditable: Boolean //根据 DataSou ...

  9. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  10. 用Backbone.js创建一个联系人管理系统(二)

    欢迎大家回来继续这一教程,第一部分我们学习了model,collection和view在Backbone中的 基本用法,还有怎么样用主视图去绑定collection去渲染出每个Contact. 这部分 ...