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 realize the effect of turning pages with gestures in Android

2025-04-11 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 gesture page turning effect in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Android article on how to achieve gesture page turning effect. Let's take a look at it.

Activity_main.xml layout interface code:

MainActivity.java logic code:

Package com.fukaimei.gestureflip;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.ViewFlipper;public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {/ / ViewFlipper instance ViewFlipper flipper; / / define gesture detection instance GestureDetector detector / / define an animation array to specify for ViewFlipper to switch animation effects Animation [] animations = new Animation [4]; / / define the minimum distance between gesture highlights final int FLIP_DISTANCE = 50; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / create gesture detector detector = new GestureDetector (this, this) / / get ViewFlipper instance flipper = (ViewFlipper) this.findViewById (R.id.flipper); / / add 8 ImageView components to ViewFlipper: flipper.addView (addImageView (R.drawable.img01)); flipper.addView (addImageView (R.drawable.img02)); flipper.addView (addImageView (R.drawable.img03)); flipper.addView (addImageView (R.drawable.img04)); flipper.addView (addImageView (R.drawable.img05)); flipper.addView (addImageView (R.drawable.img06)) Flipper.addView (addImageView (R.drawable.img07)); flipper.addView (addImageView (R.drawable.img08)); / / initialize the Animation array animations [0] = AnimationUtils.loadAnimation (this, R.anim.left_in); animations [1] = AnimationUtils.loadAnimation (this, R.anim.left_out); animations [2] = AnimationUtils.loadAnimation (this, R.anim.right_in); animations [3] = AnimationUtils.loadAnimation (this, R.anim.right_out) } / / define the tool method private View addImageView (int resId) {ImageView imageView = new ImageView (this); imageView.setImageResource (resId); imageView.setScaleType (ImageView.ScaleType.CENTER); return imageView;} @ Override public boolean onTouchEvent (MotionEvent event) {/ / hand over the touch event on this Activity to GestureDetector for processing return detector.onTouchEvent (event);} @ Override public boolean onDown (MotionEvent e) {return false } @ Override public void onShowPress (MotionEvent e) {} @ Override public boolean onSingleTapUp (MotionEvent e) {return false;} @ Override public boolean onScroll (MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) {return false } @ Override public void onLongPress (MotionEvent e) {} @ Override public boolean onFling (MotionEvent E1, MotionEvent e2, float velocityX) Float velocityY) {/ / if the X coordinate of the first contact event is greater than the X coordinate of the second contact event exceeds FLIP_DISTANCE / / that is, the gesture slides from right to left if (e1.getX ()-e2.getX () > FLIP_DISTANCE) {/ / sets the switching animation effect flipper.setInAnimation for flipper (animations [0]) Flipper.setOutAnimation (animations [1]); flipper.showPrevious (); return true;} / / if the X coordinate of the second contact event is greater than the X coordinate of the first contact event exceeds FLIP_DISTANCE / / that is, the gesture slides from right to left else if (e2.getX ()-e1.getX () > FLIP_DISTANCE) {/ / sets the switching animation effect flipper.setInAnimation for flipper (animations [2]) Flipper.setOutAnimation (animations [3]); flipper.showNext (); return true;} return false;}}

Anim/left_in.xml Code:

Anim/left_out.xml Code:

Anim/right_in.xml Code:

Anim/right_out.xml Code:

This is the end of the article on "how to achieve the effect of page turning with gestures in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to achieve the effect of page turning with gestures in Android". If you want to learn more, you are welcome to follow 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