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方法,并根据需要设定具体手势判断方法。

转载于:https://www.cnblogs.com/songhaipeng/p/3625110.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值