Android ViewFlipper Sliding Screen switching image [Android evolution 19th]

Source: Internet
Author: User

 

I recently saw the usage of ViewFlipper and Animation together, and I wrote it myself. It seems very gray and easy to use, and the effect is much better than that of gallery scrolling and viewing images, this also enables switching between images on the scrolling screen, but the user experience is much better! So I will share my own demo! I hope it will be useful to you and it will be easier for me to check it later! Reprinted please indicate the source:

Http://blog.csdn.net/wdaming1986/article/details/6766058

 

At the beginning of the program interface, the first image slides to the right and switches to the second image:


 

Slide to the right to switch the third image:

Slide left and switch to the second picture!

And so on!

 

 

Code:

1. MainActivity. Java files:

 

Package com.cn. daming;

 

Import android. app. Activity;

Import android. content. Intent;

Import android. graphics. Color;

Import android. graphics. drawable. GradientDrawable;

Import android. graphics. drawable. GradientDrawable. Orientation;

Import android. OS. Bundle;

Import android. util. Log;

Import android. view. GestureDetector;

Import android. view. GestureDetector. OnGestureListener;

Import android. view. LayoutInflater;

Import android. view. MotionEvent;

Import android. view. View;

Import android. view. animation. AnimationUtils;

Import android. widget. ViewFlipper;

 

Public class MainActivity extends Activity implements OnGestureListener {

Private GestureDetector detector;

Private ViewFlipper flipper;

Private final int help_result = 106;

Intent getMainActivity = null;

Int count = 1;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

DrawBackground ();

LayoutInflater inflater = LayoutInflater. from (this );

Final View layout = inflater. inflate (R. layout. view_flipper, null );

SetContentView (layout );

Flipper = (ViewFlipper) findViewById (R. id. view_flipper );

Detector = new GestureDetector (this );

}

 

Public void drawBackground ()

{

GradientDrawable grad = new GradientDrawable (

Orientation. TL_BR,

New int [] {Color. rgb (0, 0,127 ),

Color. rgb (0, 0,255 ),

Color. rgb (127, 0,255 ),

Color. rgb (1, 127,127,255 ),

Color. rgb (1, 127,255,255 ),

Color. rgb (1, 255,255,255 )}

);

 

This. getWindow (). setBackgroundDrawable (grad );

}

@ Override

Public boolean onTouchEvent (MotionEvent event ){

Return this. detector. onTouchEvent (event );

}

 

Public boolean onDown (MotionEvent arg0 ){

// TODO Auto-generated method stub

Return false;

}

 

Public boolean onFling (MotionEvent e1, MotionEvent e2, float arg2,

Float arg3 ){

Log. I ("Fling", "Fling Happened! ");

If (e1.getX ()-e2.getX ()> 5 ){

This. flipper. setInAnimation (AnimationUtils. loadAnimation (this,

R. anim. push_left_in ));

This. flipper. setOutAnimation (AnimationUtils. loadAnimation (this,

R. anim. push_left_out ));

If (count <3 ){

This. flipper. showNext ();

Count ++;

}

 

Return true;

} Else if (e1.getX ()-e2.getX () <-5 ){

This. flipper. setInAnimation (AnimationUtils. loadAnimation (this,

R. anim. push_right_in ));

This. flipper. setOutAnimation (AnimationUtils. loadAnimation (this,

R. anim. push_right_out ));

If (count> 1 ){

This. flipper. showPrevious ();

Count --;

}

Return true;

}

Return true;

}

 

Public void onLongPress (MotionEvent arg0 ){

// TODO Auto-generated method stub

}

 

Public boolean onScroll (MotionEvent arg0, MotionEvent arg1, float arg2,

Float arg3 ){

// TODO Auto-generated method stub

Return false;

}

 

Public void onShowPress (MotionEvent arg0 ){

// TODO Auto-generated method stub

}

 

Public boolean onSingleTapUp (MotionEvent arg0 ){

// TODO Auto-generated method stub

Return false;

}

}

 

Ii. view_flipper.xml layout File

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: id = "@ + id/layout" android: orientation = "horizontal"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

>

<ViewFlipper android: id = "@ + id/view_flipper"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

Android: persistentDrawingCache = "animation" android: flipInterval = "1000"

Android: inAnimation = "@ anim/push_left_in" android: outAnimation = "@ anim/push_left_out">

<LinearLayout android: orientation = "horizontal"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent">

<ImageView android: id = "@ + id/view_bg1" android: src = "@ drawable/bg1"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

>

</ImageView>

</LinearLayout>

<LinearLayout android: orientation = "horizontal"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent">

<ImageView android: id = "@ + id/view_bg2" android: src = "@ drawable/bg2"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

>

</ImageView>

</LinearLayout>

<LinearLayout android: orientation = "horizontal"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent">

<ImageView android: id = "@ + id/view_bg3" android: src = "@ drawable/bg3"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

>

</ImageView>

</LinearLayout>

</ViewFlipper>

</LinearLayout>

 

3. anim animation layout file: www.2cto.com

1. push_left_in.xml layout file:

 

<Span style = "font-size: 13px;"> <? Xml version = "1.0" encoding = "UTF-8"?>

<Set xmlns: android = "http://schemas.android.com/apk/res/android">

<Translate android: fromXDelta = "100% p" android: toXDelta = "0"

Android: duration = "500"/>

<Alpha android: fromAlpha = "0.1" android: toAlpha = "1.0"

Android: duration = "500"/>

</Set> </span>

 

<Strong>

</Strong>

2. push_left_out.xml layout File

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Set xmlns: android = "http://schemas.android.com/apk/res/android">

<Translate android: fromXDelta = "0" android: toXDelta = "-100% p"

Android: duration = "500"/>

<Alpha android: fromAlpha = "1.0" android: toAlpha = "0.1"

Android: duration = "500"/>

</Set>

3. push_right_in.xml layout File

 

<Span style = "font-size: 13px; color: #000000;"> <? Xml version = "1.0" encoding = "UTF-8"?>

<Set xmlns: android = "http://schemas.android.com/apk/res/android">

<Translate android: fromXDelta = "-100% p" android: toXDelta = "0"

Android: duration = "500"/>

<Alpha android: fromAlpha = "0.1" android: toAlpha = "1.0"

Android: duration = "500"/>

</Set> </span>

 

4. push_right_out.xml layout File

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Set xmlns: android = "http://schemas.android.com/apk/res/android">

<Translate android: fromXDelta = "0" android: toXDelta = "100% p"

Android: duration = "500"/>

<Alpha android: fromAlpha = "1.0" android: toAlpha = "0.1"

Android: duration = "500"/>

</Set>

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.