In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to achieve left and right sliding switch pictures in Android". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to achieve left and right sliding switching pictures in Android" can help you solve the problem.
Brief description
In this paper, ImageSwitcher is used to switch pictures by sliding left and right. First, call the setFactory method to set the view factory; then set the finger touch monitor to judge that the left and right slide and then switch the picture.
Local picture
Xml
Activity
Package com.imageSwitcherimport android.os.Bundleimport android.view.MotionEventimport android.view.Viewimport android.view.animation.AnimationUtilsimport android.widget.ImageViewimport androidx.appcompat.app.AppCompatActivityimport com.bumptech.glide.Glideimport kotlinx.android.synthetic.main.activity_main.*class MainActivity: AppCompatActivity () {/ / Local Picture privateval images = arrayOf (R.drawable.t1, R.drawable.t2, R.drawable.t3, R.drawable.t4 R.drawable.t5, R.drawable.t6) / / Network picture privateval 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 () {/ / set View Factory imageSwitcher .setFactory {val imageView = ImageView (this@MainActivity) imageView.setImageResource (images [index]) imageView} / / set the touch monitor imageSwitcher.setOnTouchListener (object: View.OnTouchListener {override fun onTouch (view: View? Event: MotionEvent?): Boolean {/ / determines whether the action is pressed by if (event?.action = = MotionEvent.ACTION_DOWN) {/ / to get the X coordinate touchDownX = event.x return true} else if (event?.action = = MotionEvent.ACTION) when the finger is pressed. _ UP) {/ / get the X coordinate touchUpX = event.x / / determine whether to slide left or right if (touchUpX-touchDownX > 100) {/ / previous if (index = = 0) {index = images.size-1} else {index--}} else if (touchDownX-touchUpX > 100) {/ / next If (index > = images.size-1) {index = 0} else {index++}} / / use the built-in fade imageSwitcher.inAnimation = AnimationUtils.loadAnimation (this@MainActivity Android.R.anim.fade_in) ImageSwitcher.outAnimation = AnimationUtils.loadAnimation (this@MainActivity, android.R.anim.fade_out) / / display another image imageSwitcher.setImageResource (images [index]) return true} return false} network image (loaded with Glide network)
The setFactory method corresponds to the replacement:
ImageSwitcher.setFactory {val imageView = ImageView (this@MainActivity) Glide.with (this) .load (urlImages [this]) .into (imageView) imageView}
SetOnTouchListener corresponding replacement:
Glide.with (this@SwipeRecommend) .asBitmap () .load (urlImages [index]) .into (imageSwitcher.currentView as ImageView)
Note that you need to set network permissions to load http network images:
Add to AndroidManifest.xml:
Add the application tag for AndroidManifest.xml:
Android:networkSecurityConfig= "@ xml/network_security_config"
Network_security_config.xml (under the res/xml/ folder, you can not create it by yourself) is as follows:
Effect display
This is the end of the introduction of "how to swipe left and right to switch pictures by Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.