Xamarin开发Android笔记:图片切换ImageSwitcher
在移动应用开发过程中经常会使用到图片展示场景,例如利用多张图片说明一个产品的特点,此处就会使用到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的更多相关文章
- Xamarin开发Android笔记:背景操作
使用Xamarin开发Android UI的时可能会遇到一些场景背景的问题,虽然可以利用多层或直接使用图片背景来完成,但这样会增加不少的资源消耗,最终导致内存溢出的情况.最好的方法还是利用内部方法或代 ...
- Xamarin开发Android笔记:拍照或相册选取图片角度问题
在开发Android应用的时候,可能会遇到类似微信朋友圈中拍照或相册选取图片的场景,拍照或选取图片之后在显示的时候却发现图片的角度不对,明明是竖版拍照,显示出来缺失躺着的. 这是因为在某些特定手机上例 ...
- Xamarin开发Android笔记:使用ZXing进行连续扫描
在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用.不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了. 在Xama ...
- Xamarin开发IOS笔记:切换输入法时输入框被遮住
在进行IOS开发的过程中,出现类似微信朋友圈的交互界面,当用户遇到感兴趣的内容可以进行评论.为了方便评论输入,当出现评论输入框的时候自动将评论输入框移动至键盘的上方,这样方便边输入边查看. 当用户隐藏 ...
- Xamarin开发Android笔记:TextView行间距设定
TextView 在使用TextView的时候会遇到调整行间距的问题,可通过Layout文件添加属性完成,具体属性如下: //设置行间距,如”3dp”. android:lineSpacingExtr ...
- 【Xamarin开发 Android 系列 13】 应用打包部署
原文:[Xamarin开发 Android 系列 13] 应用打包部署 开始倒叙咯................ 先更新大宝部署吧,这个章节比较的Easy,童鞋们不用费脑筋.点解?从界面上填写几个参 ...
- 【Xamarin开发 Android 系列 7】 Android 结构基础(下)
原文:[Xamarin开发 Android 系列 7] Android 结构基础(下) *******前期我们不打算进行太深入的东西,省的吓跑刚进门的,感觉门槛高,so,我们一开始就是跑马灯一样,向前 ...
- 【Xamarin开发 Android 系列 3】循序渐进的学习顺序
原文:[Xamarin开发 Android 系列 3]循序渐进的学习顺序 指定合理的学习步骤,将各个技术点进行强化.慢慢 的就从点到线 到面的飞跃,一切仅仅是时间问题,开始前,请记住,学习是最佳的投资 ...
- xamarin开发android收集的一些工具
xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Vi ...
随机推荐
- 使用get传参的时候,参数在后头获取不到或者出现别的错误。
把传递的参数使用encode转换一下,符合HTTP规定的编码,再使用. String encode = java.net.URLEncoder.encode("VSrYJoDat8z7Ad9 ...
- java 字节数组转int
4字节数组转int类型 小端模式 /** * 数组转int类型 * * @param src * @return */ public static int bytesToInt(byte[] src) ...
- Python学习笔记(四)字符串型
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 在最新的Python 3版本中,字符串是以Unicode编码的,也就是说,Python的字符串支持多语言 ...
- android系统中查看哪些端口被哪些应用打开
1 查看哪些端口开放,netstat 2 根据端口号获取到UID,比如端口号为10050,转成16进制是2742,使用命令grep -i 2742 /proc/net/tcp6,就能看到其UID,假如 ...
- JSF 嵌套
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com ...
- NSPredicate谓词
NSPredicate——谓词(is) 作用:判断条件表达式的求值返回真或假的过程 使用步骤: . 定义NSPredicate对象并指定条件 . 调用谓词的evaluateWithObject方法判断 ...
- iterator 及 迭代器模式(转发)
Iterator definitions An iterator is any object that, pointing to some element in a range of elements ...
- jquery中prop()与attr()方法的区别
一.prop() 简单来说是当需要判断真假时使用,如复选框时: if( $(this).prop('checked')){ //当返回true时在这里调用 }else{ //当返回false时在这里调 ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 《Java程序设计》课程准备之问卷调查
一.你对自己的未来有什么规划?做了哪些准备? 答:未来就是找个好工作,在保证自己与父母生活条件良好的基础上,进一步的提高精神上的需求.如:旅游度假,支持更多业余爱好等.准备就是:好好学习,好好运动,好 ...