If you write an Android desktop slide to toggle the screen control (ii)

Source: Internet
Author: User

In ViewGroup execution:

public void Snaptoscreen (int whichscreen) {whichscreen = Math.max (0, Math.min (Whichscreen, Getchildcount ()-1)); Boolean  Changingscreens = whichscreen! = Mcurrentscreen;mnextscreen = Whichscreen;int mscrollx = This.getScrollX (); final int NewX = Whichscreen * GetWidth (); final int delta = NEWX-MSCROLLX; System.out.println ("====snaptoscreen delta=" +delta); <strong>mscroller.startscroll (MSCROLLX, 0, Delta, 0, Math.Abs (Delta) * 2); This means that it is important to slide the delta distance from the position of the x-coordinate mscrollx </strong>//invalidate, or you can move a little page without restoring the original invalidate ();}


To make this animation work, we have to reload Computescroll
@Overridepublic void Computescroll () {if (<strong>mscroller.computescrolloffset () </strong>) {<strong >scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());     Postinvalidate (); </strong>} else if (mnextscreen! =-1) {<strong>//This is done after the slide is completed, this time must set the current view</ Strong>setcurrentscreen (Math.max (0,math.min (Mnextscreen, Getchildcount ()-1))); mnextscreen =-1;}}


If this viewgroup loads too many child controls at once, it will have a significant impact on performance, in order to solve this problem, we should display the current control, the other should be visible to null

This is handled by:

void Setcurrentscreen (int index) {mcurrentscreen = Index;resetvisibilityforchildren ();} private void Resetvisibilityforchildren () {    int count = Getchildcount ();    for (int i = 0; i < count; i++) {        View child = Getchildat (i);        if (Math.Abs (mcurrentscreen-i) <= 0) {            child.setvisibility (view.visible);        } else {            Child.setvisibility (view.invisible);}}}    

To show the next page in order to slide, we have to rewrite Dispatchdraw

@Overrideprotected void Dispatchdraw (canvas canvas) {int childCount = Getchildcount (); if (ChildCount = = 0) {return;} Boolean restore = False;int Restorecount = 0;final Long drawingtime = Getdrawingtime (); final float Scrollpos = (float) get SCROLLX ()/getwidth (); final int leftscreen = (int) scrollpos;final int rightscreen = Leftscreen + 1;if (leftscreen >= 0 && Leftscreen < childCount) {<strong>drawchild (canvas, Getchildat (leftscreen), drawingtime);// This will cause Leftscreen to be shown by the Gone state </strong>}if  (Rightscreen < Getchildcount ()) {drawchild (canvas, Getchildat ( Rightscreen), drawingtime);} if (restore) {canvas.restoretocount (Restorecount);}}

Here's how to add dot to this interface to indicate which interface is currently sliding:

In the MyGroup class, add:

public void Setmlistener (Onviewchangedlistener mlistener) {this.mlistener = Mlistener;}     Public interface Onviewchangedlistener {/** * when view changed.     * @param viewindex Index.    */void onviewchanged (int viewindex);}     private static final int default_point_margin = 5;/** * Updates the state of the point, including the total and current position.     * @param dotslayout is used to place points of layout.     * Total number @param.     * @param current position.     * @param state Bundle, Parameter extension, can be null.  */public static void Updatedots (ViewGroup dotslayout, int, int, Bundle state) {if (total        < 0) {total = 0;        } int lastIndex =-1;        Object tag = Dotslayout.gettag (r.id.dots_current);        if (tag! = null) {LastIndex = (Integer) tag;        } int margin = Default_point_margin;        if (state = null) {margin = State.getint (key_point_margin, margin);                } int childrennum = Dotslayout.getchildcount ();for (int i = Childrennum, i < total; i++) {ImageView Pointview = new ImageView (Dotslayout.getcontext ());            Pointview.setimageresource (R.drawable.dot);            Dotslayout.addview (Pointview);            Linearlayout.layoutparams params = (linearlayout.layoutparams) pointview.getlayoutparams ();            Params.leftmargin = margin;        Params.rightmargin = margin;        } for (int i = childrenNum-1; I >= total; i--) {Dotslayout.removeviewat (i);        } if (current = = LastIndex) {return; } if (lastIndex >= 0 && lastIndex < total) {ImageView Pointview = (ImageView) dot            Slayout.getchildat (LastIndex);        Pointview.setimageresource (R.drawable.dot); } if (current >= 0 && current < total) {ImageView Pointview = (ImageView) dotslay            Out.getchildat (current); Pointview.setimageresource (r.drawabLe.dot_current);    } dotslayout.settag (r.id.dots_current, current); } public interface Workspacesnaplistener {void Onsnaptoscreen (MyGroup workspace, int whichscreen);} public void Setsnaplistener (Workspacesnaplistener listener) {Snaplistener = listener;}

@Overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {} else if (mnextscreen! =-1) {<strong> if (Mlistener! = null) {mlistener.onviewchanged (mcurrentscreen);} </strong>}}
public void Snaptoscreen (int whichscreen) {<strong>if (Snaplistener! = null) {Snaplistener.onsnaptoscreen (this, Whichscreen);} </strong>}


In the layout of mainactivity, add:

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/mainroot "    android:layout_width=" fill_parent "    android:layout_height=" fill _parent "    >    <com.ringcentral.android.utils.ui.widget.mygroup       android:background=" @drawable/ Help_background_selector "        android:id=" @+id/workspace "        android:layout_width=" Fill_parent "        android: layout_height= "Fill_parent"        />         <linearlayout android:id= "@+id/dots_layout"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_marginbottom= "28dip        " Android:layout_gravity= "Bottom|center_horizontal"        android:orientation= "Horizontal"/></FrameLayout >

Point of drawing:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Oval" >    <size android:width= "6dip" android:height= "6dip"/>    <solid android: Color= "#7fffffff"/></shape>


OnCreate

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Final ViewGroup dotslayout = (viewgroup) Findviewbyid (r.id.dots_layout); final int[] images = new Int[]{r.drawable.help_ 01,r.drawable.help_02,r.drawable.help_03};final mygroup MyGroup = (mygroup) Findviewbyid (R.id.workspace); final Bundle dotsstate = new bundle (); int margin = Getresources (). Getdimensionpixelsize (R.dimen.workspace_dot_margin); Dotsstate.putint (Mygroup.key_point_margin, MARGIN);        Mygroup.updatedots (dotslayout, images.length, 0, dotsstate); for (int i = 0; i < 3; i++) {final View item = Getlayoutinflater (). Inflate (r.layout.introduction_item_test, null); Mygro Up.addview (item), final ImageView ImageView = (ImageView) Item.findviewbyid (R.id.introduction_image_view); try { Imageview.setimageresource (Images[i]);} catch (OutOfMemoryError e) {}} mygroup.setmlistener (New OnviewchangedliStener () {@Override public void onviewchanged (int. viewindex) {mygroup.updatedots (do            Tslayout, Images.length, ViewIndex, dotsstate); }}); Mygroup.setsnaplistener (new Workspacesnaplistener () {@Overridepublic void Onsnaptoscreen (MyGroup workspace, in T whichscreen) {mygroup.updatedots (dotslayout, Images.length, Whichscreen, dotsstate);}});

:



Code: http://download.csdn.net/detail/baidu_nod/7731855

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.