温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android怎么实现左右滑动切换图片

发布时间:2022-05-17 11:08:38 来源:亿速云 阅读:220 作者:iii 栏目:开发技术

这篇文章主要介绍“Android怎么实现左右滑动切换图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android怎么实现左右滑动切换图片”文章能帮助大家解决问题。

简要说明

本文采用ImageSwitcher实现左右滑动切换图片。首先调用setFactory方法,设置视图工厂;然后设置手指触碰监听,判断左滑右滑进而切换图片。

本地图片

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity

package com.imageSwitcher

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    // 本地图片
    private val images = arrayOf(R.drawable.t1,
            R.drawable.t2,
            R.drawable.t3,
            R.drawable.t4,
            R.drawable.t5,
            R.drawable.t6)
    // 网络图片
    private val urlImages = arrayOf("http://ip/aa.jpg",
            "http://ip/bb.jpg",
            "http://ip/cc.jpg",
            "http://ip/dd.jpg",
            "http://ip/ee.jpg",
            "http://ip/ff.jpg")
            
    private var index = 0
    private var touchDownX: Float = 0f
    private var touchUpX: Float = 0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()
    }

    private fun initView() {
        // 设置视图工厂
        imageSwitcher.setFactory {
            val imageView = ImageView(this@MainActivity)
            imageView.setImageResource(images[index])
            imageView
        }
        // 设置触摸监听
        imageSwitcher.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(view: View?, event: MotionEvent?): Boolean {
                //判断动作是不是按下
                if (event?.action == MotionEvent.ACTION_DOWN) {
                    // 获取手指按下时的X坐标
                    touchDownX = event.x
                    return true
                } else if (event?.action == MotionEvent.ACTION_UP) {
                    // 获取手指离开后的X坐标
                    touchUpX = event.x
                    // 判断是左滑还是右滑
                    if (touchUpX - touchDownX > 100) {
                        // 上一张
                        if (index == 0) {
                            index = images.size - 1
                        } else {
                            index--
                        }
                    } else if (touchDownX - touchUpX > 100) {
                        // 下一张
                        if (index >= images.size - 1) {
                            index = 0
                        } else {
                            index++
                        }
                    }
                    // 使用自带的淡入淡出
                    imageSwitcher.inAnimation = AnimationUtils.loadAnimation(this@MainActivity, android.R.anim.fade_in);
                    imageSwitcher.outAnimation = AnimationUtils.loadAnimation(this@MainActivity, android.R.anim.fade_out);
                    // 显示另一张图片
                    imageSwitcher.setImageResource(images[index])
                    return true
                }
                return false
            }
        })
    }
}

网络图片(采用Glide网络加载)

  • setFactory方法对应替换:

imageSwitcher.setFactory {
            val imageView = ImageView(this@MainActivity)       
            Glide.with(this).load(urlImages[index]).into(imageView)
            imageView
        }
  • setOnTouchListener对应替换:

Glide.with(this@SwipeRecommend).asBitmap().load(urlImages[index]).into(imageSwitcher.currentView as ImageView)

注意加载http网络图片需要设置网络权限:

  • AndroidManifest.xml中添加:

<uses-permission android:name="android.permission.INTERNET" />
  • AndroidManifest.xml的application标签添加:

android:networkSecurityConfig="@xml/network_security_config"
  • network_security_config.xml(res/xml/文件夹下,没有自行创建即可)内容为:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

效果展示

Android怎么实现左右滑动切换图片

关于“Android怎么实现左右滑动切换图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI