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 ...
随机推荐
- px,pt,em,rem
一直对px,pt,em,rem的认识有误区,现整理一下,供参考.之后还得整理下关于dpi相关的知识. px(pixe,像素l):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物 ...
- Java中的字符串常量池
ava中字符串对象创建有两种形式,一种为字面量形式,如String str = "droid";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...
- TextView 中添加超链接
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接,下面为大家介绍下这两种方法的实现 代码如下: 第一种 pu ...
- 【项目】搜索广告CTR预估(一)
本文介绍CTR相关基础知识. 一.广告投放系统 广告系统包含多个子系统.除了上图所示的广告投放系统外,还包含商业系统(广告库的获得),统计系统(点击展示日志的获得)等. 广告投放系统主要是面向用户的, ...
- 开源PLM软件Aras详解二 汉化以及界面
Aras安装完毕之后,默认语言为英语,对于国内很多制造业并不适用,那么下面就来说说如何汉化 首先下载汉化包:zh-cn_languagepack-110v3.zip 步骤如下: 步骤1- 设定安装程序 ...
- ELK日志管理
ELK一般由三部分组成:logstash(日志格式化) + elasticsearch(检索) + Kibana(前台报表展示) 官网地址:https://www.elastic.co/ 本人在这用的 ...
- wpf:样式(转)
http://www.cnblogs.com/shuang121/archive/2013/01/14/2860455.html 前面简单的说到了wpf中几种样式的用法,wpf有着类似web中的CSS ...
- 大道至简之编程的精义读后感(Java伪代码)
import.java.大道至简.*; import.java.愚公移山.*; public class YuGongYiShan { 愚公={项目组织者,团队经理,编程人员,技术分析师}: //沟通 ...
- Photoshop 使用曲线
曲线表示的是图像的明度, 通过信息办的 HSB 信息可以看到调整曲线时整个图像明度的变化 曲线的左下角表示图片的暗部, 右下角表示图片的高光部 而曲线本身的纵坐标则表示这个部分的明度, 例如将曲线的左 ...
- js判断访问终端
//判断访问终端 var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; ...