Android technology-view switching (4) "ViewSwitcher + Gesture Recognition" enables slide switching of views and androidviewswitcher

Source: Internet
Author: User

Android technology-view switching (4) "ViewSwitcher + Gesture Recognition" enables slide switching of views and androidviewswitcher



Android technology-view switching (1 )~ (4) project source code in: https://github.com/YongYuIT/MeiNv_Liulanqi


In the image switching implemented in "Android technology-view switching (3)", although there is an animation effect during switching, a button is required to switch. In this example, we will try to use Gesture Recognition instead of buttons to implement image switching.

This instance is also added based on the projects in the first three articles.

/MeiNv_Liulanqi/res/layout/activity_view_switcher_huadong.xml file:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- Define a ViewSwitcher component -->
<ViewSwitcher
Android: id = "@ + id/viewSwitcher"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
<! -- Define the button to scroll to the previous screen -->
</RelativeLayout>

/MeiNv_Liulanqi/src/com/example/meinv_liulanqi/ViewSwitcherHuadongActivity. java file:

Package com. example. meinv_liulanqi;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. GestureDetector;
Import android. view. LayoutInflater;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. ViewSwitcher;
Import android. widget. ViewSwitcher. ViewFactory;
Public class ViewSwitcherHuadongActivity extends Activity
{
Private int screenNo =-1;
Private int screenNum;
Public ViewSwitcher switcher;
Public int [] img_ids;
Private GestureDetector myDesDet;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_view_switcher_huadong );
// GestureDetector is used to receive data from the onTouch function of the OnTouchListener to determine the gesture,
// The OnGestureListener responds to the attack.
MyOnGestureListener listener = new myOnGestureListener (this );
MyDesDet = new GestureDetector (listener );
Img_ids = new int [] {R. drawable. linzhiling, R. drawable. liuyan,
R. drawable. yangmi };
ScreenNum = img_ids.length;
// Provides a view factory for ViewSwitcher
Switcher = (ViewSwitcher) findViewById (R. id. viewSwitcher );
MyViewFactory factory = new myViewFactory (this. getLayoutInflater (),
MyDesDet );
Switcher. setFactory (factory );
// Initialization
GetNext (switcher, img_ids );
}
Public void getNext (ViewSwitcher _ switcher, int [] _ img_ids)
{
If (screenNo <screenNum-1)
{
ScreenNo ++;
// Set the animation effect for view switching
_ Switcher. setInAnimation (ViewSwitcherHuadongActivity. this,
R. anim. slide_in_right );
_ Switcher. setOutAnimation (ViewSwitcherHuadongActivity. this,
R. anim. slide_out_left );
// Obtain the instance of the next view
LinearLayout lil = (LinearLayout) _ switcher. getNextView ();
ImageView img = (ImageView) lil. findViewById (R. id. img_meinv );
Img. setImageResource (_ img_ids [screenNo]);
// Switch the view
_ Switcher. showNext ();
}
}
Public void getPrev (ViewSwitcher _ switcher, int [] _ img_ids)
{
If (screenNo> 0)
{
ScreenNo --;
// Set the animation effect for view switching
_ Switcher. setInAnimation (ViewSwitcherHuadongActivity. this,
R. anim. slide_in_lef );
_ Switcher. setOutAnimation (ViewSwitcherHuadongActivity. this,
R. anim. slide_out_right );
// Obtain the instance of the next view
LinearLayout lil = (LinearLayout) _ switcher. getNextView ();
ImageView img = (ImageView) lil. findViewById (R. id. img_meinv );
Img. setImageResource (_ img_ids [screenNo]);
// Switch the view
_ Switcher. showPrevious ();
}
}
Class myViewFactory implements ViewFactory
{
Private LayoutInflater inflater;
Private GestureDetector desDet;
Public myViewFactory (LayoutInflater _ inf, GestureDetector _ desDet)
{
Inflater = _ inf;
DesDet = _ desDet;
}
@ Override
Public View makeView ()
{
// Provide the instance for the next view
View v = inflater. inflate (R. layout. fragment_layout, null );
ImageView img = (ImageView) v. findViewById (R. id. img_meinv );
// Register the touch event listener for the image
Img. setOnTouchListener (new myOnTouchListener (desDet ));
Return v;
}
}
// Extended to get your own touch event listener
Class myOnTouchListener implements OnTouchListener
{
Private GestureDetector desDet;
Public myOnTouchListener (GestureDetector _ desDet)
{
DesDet = _ desDet;
}
@ Override
Public boolean onTouch (View arg0, MotionEvent arg1)
{
// Upload the listening data to GestureDetector, which can identify the gesture
Return desDet. onTouchEvent (arg1 );
}
}
}

/MeiNv_Liulanqi/src/com/example/meinv_liulanqi/myOnGestureListener. java file:

Package com. example. meinv_liulanqi;
Import java. util. Date;
Import android. view. MotionEvent;
Import android. view. GestureDetector. OnGestureListener;
Public class myOnGestureListener implements OnGestureListener
{
Private ViewSwitcherHuadongActivity act;
Private static Date lastTime = null;
Public myOnGestureListener (ViewSwitcherHuadongActivity)
{
Act =;
}
@ Override
// The third parameter is the displacement of the sliding gesture in the horizontal direction.
// The fourth parameter is the displacement of the sliding gesture in the vertical direction.
Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX,
Float distanceY)
{
// Obtain the current time
Date curDate = new Date (System. currentTimeMillis ());
Float shijiancha = 1000;
If (lastTime! = Null)
{
Shijiancha = curDate. getTime ()-lastTime. getTime ();
}
// Compare the interval between two sliding gestures.
// Because a slide on the screen may be recognized by GestureDetector for several gestures, You need to filter out gesture with too short intervals.
If (shijiancha> 500)
{
If (distanceX> 10)
{
Act. getNext (act. switcher, act. img_ids );
}
If (distanceX <-10)
{
Act. getPrev (act. switcher, act. img_ids );
}
}
LastTime = curDate;
Return true;
}
@ Override
Public boolean onDown (MotionEvent arg0)
{
// TODO Auto-generated method stub
Return true;
}
@ Override
Public boolean onFling (MotionEvent arg0, MotionEvent arg1, float arg2,
Float arg3)
{
// TODO Auto-generated method stub
Return true;
}
@ Override
Public void onLongPress (MotionEvent arg0)
{
// TODO Auto-generated method stub
}
@ Override
Public void onShowPress (MotionEvent arg0)
{
// TODO Auto-generated method stub
}
@ Override
Public boolean onSingleTapUp (MotionEvent arg0)
{
// TODO Auto-generated method stub
Return true;
}
}

Implementation results:


Sliding beauty...


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.