ViewFlipper implements page switching of ViewPager and page switching of viewpager

Source: Internet
Author: User

ViewFlipper implements page switching of ViewPager and page switching of viewpager

Activity_main.xml

<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"    android:orientation="vertical" >    <ViewFlipper        android:id="@+id/flipper"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </ViewFlipper></LinearLayout>

MainActivity

Package com. example. viewflipper; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. widget. imageView; import android. widget. viewFlipper; public class MainActivity extends Activity {private ViewFlipper flipper; private int [] resId = {R. drawable. pic1, R. drawable. pic2, R. drawable. pic3, R. drawable. pic4}; private float startX; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); flipper = (ViewFlipper) findViewById (R. id. flipper); // Add the child viewfor (int I = 0; I <resId. length; I ++) {flipper. addView (getImageView (resId [I]);} // enter the animation-cancel the automatic playback function // flipper. setInAnimation (this, R. anim. left_in); // flipper. setOutAnimation (this, R. anim. left_out); // flipper. setFlipInterval (3000); // flipper. startFlipping () ;}// obtain the image private ImageView getImageView (int resId) {ImageView imageView = new ImageView (this); // overlay the screen imageView. setBackgroundResource (resId); return imageView;}/*** process gesture events */@ Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // obtain the X axis coordinate of the starting point startX = event. getX (); break; case MotionEvent. ACTION_MOVE: break; case MotionEvent. ACTION_UP: // slide to the right if (event. getX () & gt; startX + 100) {flipper. setInAnimation (this, R. anim. left_in); flipper. setOutAnimation (this, R. anim. rightf_out); // display the previous flipper page. showPrevious ();} // move if (event. getX () + 100 <startX) {flipper. setInAnimation (this, R. anim. right_in); flipper. setOutAnimation (this, R. anim. left_out); // display the previous flipper page. showNext ();} break; default: break;} return super. onTouchEvent (event );}}

Left_in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="1000"        android:fromXDelta="-100%p"        android:toXDelta="0" /></set>

Left_out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="1000"        android:fromXDelta="0"        android:toXDelta="-100%p" /></set>

Right_in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="1000"        android:fromXDelta="100%p"        android:toXDelta="0" /></set>

Rightf_out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="1000"        android:fromXDelta="0"        android:toXDelta="100%p" /></set>


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.