时间:2022-09-15 08:56:39 | 栏目: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 } }) } }
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>
效果展示