Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android studio to swipe left and right to switch pictures

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor to share with you how to use Android studio to achieve the left and right slide to switch pictures of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.

First of all, use the picture switcher ImageSwitcher to switch pictures.

Let's learn about ImageSwitcher first.

Important attributes of 1.ImageSwitcher:

Android:inAnimation: the effect when you cut into an image.

Android:outAnimation: the effect of cutting out an image.

The above two attributes are set in XML, and you can customize the animation effects through the XML resource file. If you just want to use some simple effects that come with Android, you can call Android's built-in resources, or you can set them in the code. You can directly use the setInAnimation () and setOutAnimation () methods. They all pass an abstract object of Animation, and Animation is used to describe an animation effect, which is generally obtained using a utility class of AnimationUtils.

Common animation effects are:

Fede_in: fade in

Fade_out: fade out

Slide_in_left: slide in from the left

Slide_out_right: slide out from the right

Important methods of ImageSwitcher in 2.java files:

SetImageURL (URL) setImageResource (int) setImageDrawable (Drawable)

3. View factory setFactory ()

ImageSwitcher sets a ViewSwitcher.ViewFactory interface for it through the setFactory () method. When setting up this ViewFactory interface, you need to implement the makeView () method, which usually returns an ImageView. MakeView () generates ImageView for ImageSwitcher.

Next, the code can swipe left and right to switch pictures.

XML

The java code is as follows:

Package com.example.tablelayout;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.view.animation.AnimationUtils;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;import androidx.appcompat.app.AppCompatActivity;public class ImageSwitcha_Activity extends AppCompatActivity {private int [] arrayPicture=new int [] {R.drawable.pather. Drawable.pb}; private ImageSwitcher imageSwitcher; private int index; private float touchDownX Private float touchUpX; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.imageswitch_main); / / set full screen display getWindow () .setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); imageSwitcher=findViewById (R.id.imageswitch) / / set view factory imageSwitcher.setFactory (new ViewSwitcher.ViewFactory () {@ Override public View makeView () {ImageView imageView=new ImageView (ImageSwitcha_Activity.this); imageView.setImageResource (arrayPicture [index]); / / set display picture (use subscript) return imageView;// to return image view}}) / / set the touch listener imageSwitcher.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {/ / to determine whether the action is pressed to get the X coordinate if (event.getAction () = = MotionEvent.ACTION_DOWN) {touchDownX=event.getX () Return true;} else if (event.getAction () = = MotionEvent.ACTION_UP) {touchUpX=event.getX () / / decide whether to slide left or right (touchUpX-touchDownX > 100) {/ / determine whether the first picture changes the index into the last image index, / / if not, the current index is subtracted by one index=index==0?arrayPicture.length-1:index-1 / / use the built-in fade imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (ImageSwitcha_Activity.this,android.R.anim.fade_in)); imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (ImageSwitcha_Activity.this,android.R.anim.fade_out)); imageSwitcher.setImageResource (arrayPicture [index]) } else if (touchDownX-touchUpX > 100) {index=index==arrayPicture.length-1?0:index+1;// Note here the subscript starts at 0, so it should be length minus 1 imageSwitcher.setInAnimation (AnimationUtils.loadAnimation (ImageSwitcha_Activity.this,android.R.anim.fade_in)) ImageSwitcher.setOutAnimation (AnimationUtils.loadAnimation (ImageSwitcha_Activity.this,android.R.anim.fade_out)); imageSwitcher.setImageResource (arrayPicture [index]);} return true;} return false;}}) }} above is all the contents of the article "how to use Android studio to swipe left and right to switch pictures". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report