轉(zhuǎn)帖|使用教程|編輯:我只采一朵|2014-07-22 14:29:46.000|閱讀 4072 次
概述:本文教你如何在Xamarin中用ImageSwithcher方法實現(xiàn)圖片切換效果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在移動應(yīng)用開發(fā)過程中經(jīng)常會使用到圖片展示場景,例如利用多張圖片說明一個產(chǎn)品的特點,此處就會使用到ImageSwithcher,當(dāng)然也可以使用ViewFliper來實現(xiàn),但使用ViewFliper的時候會資源釋放的問題,需要手動進(jìn)行操作,這點在以后的文章中講述。
要使用ImageSwithcher,首先需要在界面文件中添加ImageSwithcher,然后在代碼中為ImageSwithcher指定圖片加載方法以及觸控方法。
下面講述具體的實現(xiàn)方法
界面Xml文件為:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="//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>
實現(xiàn)代碼為:
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 ("產(chǎn)品特色"); imageSwitcher = FindViewById<ImageSwitcher> (Resource.Id.SwitcherProductFeature); currentPosition = 0; 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() > 100) { currentPosition--; if (currentPosition < 0) currentPosition = imgs.Length - 1; imageSwitcher.SetInAnimation (this, Resource.Animation.LeftIn); imageSwitcher.SetOutAnimation (this, Resource.Animation.RightOut); imageSwitcher.SetImageResource (imgs [currentPosition]); } else if (e2.GetX() - e1.GetX() < -100) { currentPosition++; if (currentPosition > imgs.Length - 1) currentPosition = 0; 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設(shè)定Factory,聲明MakeView方法,并根據(jù)需要設(shè)定具體手勢判斷方法。
By博客園宋海鵬
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn