zoukankan      html  css  js  c++  java
  • 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方法,并根据需要设定具体手势判断方法。

  • 相关阅读:
    iOS 中架构模式的浅显理解
    Block 在 ARC 下的拷贝
    Repo 的使用小结
    博客园新语言代码高亮以及OpenLiveWriter插件开发(一)
    GridLayout 使用
    PopupWindow 使用
    NDK笔记(二)-在Android Studio中使用ndk-build
    NDK 笔记(一)
    Java 多线程编程
    搭建Apache Web服务器
  • 原文地址:https://www.cnblogs.com/songhaipeng/p/3625110.html
Copyright © 2011-2022 走看看