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 = 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設定Factory,聲明MakeView方法,並根據需要設定具體手勢判斷方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM