Android Technology--View switch (i) ~ (iv) Project source code: Https://github.com/YongYuIT/MeiNv_Liulanqi
The above "Android technology-view switch (three)" Implementation of the image switch, although the switch is animated, but you need to use a button to switch. In this example, you will try to use gesture recognition instead of a button to switch pictures.
This example is also based on the items 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 buttons 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);
The gesturedetector is used to receive data from the Ontouchlistener Ontouch function, thus distinguishing gestures,
After the gesture is recognized, it is responded by Ongesturelistener.
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;
Providing 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++;
Animating the View toggle
_switcher.setinanimation (Viewswitcherhuadongactivity.this,
R.anim.slide_in_right);
_switcher.setoutanimation (Viewswitcherhuadongactivity.this,
R.anim.slide_out_left);
Get an instance of the next view
LinearLayout lil = (linearlayout) _switcher.getnextview ();
ImageView img = (ImageView) Lil.findviewbyid (R.ID.IMG_MEINV);
Img.setimageresource (_img_ids[screenno]);
Toggle View
_switcher.shownext ();
}
}
public void GetPrev (Viewswitcher _switcher, int[] _img_ids)
{
if (Screenno > 0)
{
screenno--;
Animating the View toggle
_switcher.setinanimation (Viewswitcherhuadongactivity.this,
R.anim.slide_in_lef);
_switcher.setoutanimation (Viewswitcherhuadongactivity.this,
R.anim.slide_out_right);
Get an instance of the next view
LinearLayout lil = (linearlayout) _switcher.getnextview ();
ImageView img = (ImageView) Lil.findviewbyid (R.ID.IMG_MEINV);
Img.setimageresource (_img_ids[screenno]);
Toggle 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 an instance of the next view
View v = inflater.inflate (r.layout.fragment_layout, NULL);
ImageView img = (ImageView) V.findviewbyid (R.ID.IMG_MEINV);
Registering a touch event listener for a picture
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)
{
Pass the listening data to the gesturedetector,gesturedetector to distinguish gestures from it.
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 a)
{
act = A;
}
@Override
The third parameter is: the amount of displacement of the slide gesture in the horizontal direction
The fourth parameter is: The amount of displacement of the slide gesture in the vertical direction
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,
float Distancey)
{
Get current time
Date curdate = new Date (System.currenttimemillis ());
float Shijiancha = 1000;
if (lasttime! = null)
{
Shijiancha = Curdate.gettime ()-lasttime.gettime ();
}
Compares the time interval between two swipe gestures.
Because a swipe on the screen may be gesturedetector to several gestures, it is necessary to filter out gestures that are too short for the time interval.
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;
}
}
The effect of the implementation:
Beauty Sliding in ...
Android Technology--view switch (d) "viewswitcher+ gesture recognition" for sliding switching of views