Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
簡要說明
本文采用ImageSwitcher實(shí)現(xiàn)左右滑動(dòng)切換圖片。首先調(diào)用setFactory方法,設(shè)置視圖工廠;然后設(shè)置手指觸碰監(jiān)聽,判斷左滑右滑進(jìn)而切換圖片。
本地圖片
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) ?? ?// 網(wǎng)絡(luò)圖片 ?? ?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() { ? ? ? ? // 設(shè)置視圖工廠 ? ? ? ? imageSwitcher.setFactory { ? ? ? ? ? ? val imageView = ImageView(this@MainActivity) ? ? ? ? ? ? imageView.setImageResource(images[index]) ? ? ? ? ? ? imageView ? ? ? ? } ? ? ? ? // 設(shè)置觸摸監(jiān)聽 ? ? ? ? imageSwitcher.setOnTouchListener(object : View.OnTouchListener { ? ? ? ? ? ? override fun onTouch(view: View?, event: MotionEvent?): Boolean { ? ? ? ? ? ? ? ? //判斷動(dòng)作是不是按下 ? ? ? ? ? ? ? ? if (event?.action == MotionEvent.ACTION_DOWN) { ? ? ? ? ? ? ? ? ? ? // 獲取手指按下時(shí)的X坐標(biāo) ? ? ? ? ? ? ? ? ? ? touchDownX = event.x ? ? ? ? ? ? ? ? ? ? return true ? ? ? ? ? ? ? ? } else if (event?.action == MotionEvent.ACTION_UP) { ? ? ? ? ? ? ? ? ? ? // 獲取手指離開后的X坐標(biāo) ? ? ? ? ? ? ? ? ? ? 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 ? ? ? ? ? ? } ? ? ? ? }) ? ? } }
網(wǎng)絡(luò)圖片(采用Glide網(wǎng)絡(luò)加載)
- setFactory方法對應(yīng)替換:
imageSwitcher.setFactory { ? ? ? ? ? ? val imageView = ImageView(this@MainActivity) ? ? ?? ? ? ? ? ? ? Glide.with(this).load(urlImages[index]).into(imageView) ? ? ? ? ? ? imageView ? ? ? ? }
- setOnTouchListener對應(yīng)替換:
Glide.with(this@SwipeRecommend).asBitmap().load(urlImages[index]).into(imageSwitcher.currentView as ImageView)
注意加載http網(wǎng)絡(luò)圖片需要設(shè)置網(wǎng)絡(luò)權(quán)限:
- AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.INTERNET" />
- AndroidManifest.xml的application標(biāo)簽添加:
android:networkSecurityConfig="@xml/network_security_config"
- network_security_config.xml(res/xml/文件夾下,沒有自行創(chuàng)建即可)內(nèi)容為:
<?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>
效果展示
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05Android原生定位服務(wù)LocationManager
這篇文章主要為大家介紹了Android原生定位服務(wù)LocationManager實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android 推送原理(Android Push Notification)詳解
這篇文章主要介紹了Android 推送原理(Android Push Notification)詳解的相關(guān)資料,這里對Android 推送的原理做了簡單的介紹,需要的朋友可以參考下2016-11-11Android 如何獲取手機(jī)總內(nèi)存和可用內(nèi)存等信息
這篇文章主要介紹了Android系統(tǒng)檢測程序內(nèi)存占用各種方法,并對內(nèi)存信息的詳細(xì)介紹,需要的朋友可以參考下2016-07-07Android 用戶Session管理的設(shè)計(jì)方案
這篇文章主要介紹了Android 用戶Session管理的設(shè)計(jì)方案,需要的朋友可以參考下2017-12-12Android如何獲取系統(tǒng)通知的開啟狀態(tài)詳解
這篇文章主要給大家介紹了關(guān)于Android如何獲取系統(tǒng)通知開啟狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧啊。2017-08-08從"Show?tabs"了解Android?Input系統(tǒng)
這篇文章主要介紹了從"Show?tabs"了解Android?Input系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2023-01-01Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法,涉及Android界面線性布局、相對布局及選項(xiàng)卡設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-02-02Android 阿里云OSS文件上傳的實(shí)現(xiàn)示例
這篇文章主要介紹了Android 阿里云OSS文件上傳的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08