Android实现左右滑动切换图片
作者:Biu→Biu丶 发布时间:2021-06-20 10:51:30
标签: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>
效果展示
来源:https://blog.csdn.net/weixin_40145819/article/details/112535995


猜你喜欢
- 使用场景在 Java 应用中,对于访问频率高,更新少的数据,通常的方案是将这类数据加入缓存中。相对从数据库中读取来说,读缓存效率会有很大提升
- 一、OutputStreamWriter流 API说明:OutputStreamWriter是从字符流到
- 关于“标签PDF文件(Tagged PDF)标签PDF文件包含描述文档结构和各种文档元素顺序的元数据,是一种包含后端提供
- 1、创建Windows服务 说明:a)Description 服务描述,直接显示到Windows服务列表中的描述;b)Displa
- 使用List.contains(Object object)方法判断ArrayList是否包含一个元素对象(针对于对象的属性值相同,但对象地
- 本文实例为大家分享了C# Winform选项卡集成窗体的具体代码,供大家参考,具体内容如下知识要点:利用反射动态的加载窗体到对应的TabPa
- 最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的
- 1.图的遍历从图中某一顶点出发访问图中其余顶点,且每个顶点仅被访问一次图的遍历有两种深度优先遍历DFS、广度优先遍历BFS2.深度优先遍历深
- 本文实例为大家分享了android绘制曲线和折线图的具体代码,供大家参考,具体内容如下(曲线) (折线)1.CurveView.j
- 这里记录下C#中using关键字的使用方法。Using的使用大致分别以下三种:1 :using 指令(命名空间)using System;u
- 原理简介Java中提供了Calendar这个专门用于对日历进行操作的类,那么这个类有什么特殊的地方呢,首先我们来看Calendar的声明:p
- Windows系统启动Java程序会弹出黑窗口。黑窗口有几点不好。首先它不美观;其次容易误点导致程序关闭;但最让我匪夷所思的是:将鼠标光标选
- 插入排序原理①把所有元素分成已排序和未排序两组②找到未排序组的第一个元素,向已经排序的组中进行插入③倒序遍历已经排好的元素,依次和待插入的元
- Android Rreact Native 常见错误总结 1.invariant violation:expecte
- C# 字符串进制转换/// <summary> /// 进制转换 &nbs
- 本文实例讲述了Android编程使用Fragment界面向下跳转并一级级返回的实现方法。分享给大家供大家参考,具体如下:1.首先贴上项目结构
- 简介Microsoft官网关于 WindowChome 的介绍截取Microsoft文章的一段话:若要在保留其标准功能时自定义窗口,可以使用
- using System;using System.Collections.Generic;using System.Text;namesp
- 在导入studio工程的时候,进行sync的时候,提示Error:Configuration with name 'default&
- Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架。和其它RPC框架相比,它主要具有如