Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android左右滑动切换图片

Android实现左右滑动切换图片

作者:Biu→Biu丶

这篇文章主要为大家详细介绍了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网络加载)

imageSwitcher.setFactory {
            val imageView = ImageView(this@MainActivity)       
            Glide.with(this).load(urlImages[index]).into(imageView)
            imageView
        }
Glide.with(this@SwipeRecommend).asBitmap().load(urlImages[index]).into(imageSwitcher.currentView as ImageView)

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

<uses-permission android:name="android.permission.INTERNET" />
android:networkSecurityConfig="@xml/network_security_config"
<?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>

效果展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文