Android practice simple tutorial-48th gun (App guide page effect implementation), androidapp
Kids shoes that often use apps will find that the first time they enter the APP, there will be a boot page, which can include some APP usage introductions or other information. Next we will study how to implement this function, increase the APP experience.
1. Custom Controls inherit ViewGroup:
Package com. genius. scroll; import android. content. context; import android. content. intent; import android. content. sharedPreferences; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. velocityTracker; import android. view. view; import android. view. viewGroup; import android. widget. scroller; import com. genius. demo. mainActivity; public class MyScrollLayou T extends ViewGroup {private static final String TAG = "MyScrollLayout";/** used to determine the animation gesture **/private VelocityTracker mVelocityTracker; private static final int SNAP_VELOCITY = 600; /** sliding controller **/private Scroller mScroller; private int mCurScreen; private int mDefaultScreen = 0; private float mLastMotionX;/** current page **/private int currentPage; /** total page number **/private int pagesize; private Context mContext; // Context object priv Ate OnViewChangeListener mOnViewChangeListener; // View rolling listener/** indicates whether the listener has jumped to the main interface **/private boolean go_main = false; private SharedPreferences preferences; public MyScrollLayout (Context context) {super (context); this. mContext = context; init (context);} public MyScrollLayout (Context context, AttributeSet attrs) {super (context, attrs); this. mContext = context; init (context);} public MyScrollLayout (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); this. mContext = context; init (context);}/*** get the current page ** @ param page */public void setPosition (int page) {if (page> 0) {currentPage = page ;}}/*** obtain the total number of pages ** @ param size */public void setPageSize (int size) {pagesize = size ;} private void init (Context context) {mCurScreen = mDefaultScreen; mScroller = new Scroller (context);}/*** call Field Scene: When the view sets the size and position for the child, it is called parameter description: parameter 1: view has a new size or position; parameter 2: Left position relative to the parent view; * parameter 3: Top position relative to parent view; parameter 4: Right position relative to parent view; parameter 5: Bottom position relative to parent view. * // @ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {if (changed) {// if a new size or position exists, int childLeft = 0; final int childCount = getChildCount (); // return the total number of child views for (int I = 0; I <childCount; I ++) {// traverse the child Viewfinal View childView = getChildAt (I); if (ChildView. getVisibility ()! = View. GONE) {// childView. getMeasuredWidth () use measure () in the method or onDraw () of this View; (you can define the value of measure // measures ), otherwise, the result you get is the same as that obtained by getWidth (). final int childWidth = childView. getMeasuredWidth (); // parameter (relative to the left, top, right, and bottom of the parent layout) childView. layout (childLeft, 0, childLeft + childWidth, childView. getMeasuredHeight (); childLeft + = childWidth ;}}}/*** specifies the space available for the control and the metadata about the space description. Data. * // @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); // MeasureSpec encapsulates the layout requirements that the parent layout passes to the Child layout. Each MeasureSpec represents a set of width and height requirements. A MeasureSpec consists of final int width = MeasureSpec in size and mode. getSize (widthMeasureSpec); // extract the final int widthMode = MeasureSpec Based on the provided measured value. getMode (widthMeasureSpec); // extract the Log mode based on the provided measured value (Format) (three modes are available. d (TAG, "onMeasure width:" + width + "widthMode:" + widthMode); final int count = getChildCount (); // obtain the total number of subviews for (int I = 0; I <count; I ++) {// call the subview getChildAt (I ). measure (widthMeasureSpec, heightMeasureSpec);} scrollTo (m CurScreen * width, 0);} public void snapToDestination () {final int screenWidth = getWidth (); final int destScreen = (getScrollX () + screenWidth/2)/screenWidth; snapToScreen (destScreen);} public void snapToScreen (int whichScreen) {// get a valid page layout whichScreen = Math. max (0, Math. min (whichScreen, getChildCount ()-1); if (getScrollX ()! = (WhichScreen * getWidth () {final int delta = whichScreen * getWidth ()-getScrollX (); mScroller. startScroll (getScrollX (), 0, delta, 0, Math. abs (delta) * 2); mCurScreen = whichScreen; invalidate (); // re-paint the layout if (mOnViewChangeListener! = Null) {mOnViewChangeListener. onViewChange (mCurScreen) ;}}/ *** the draw () method calls */@ Overridepublic void computeScroll () {if (mScroller. computescroloffset () {// if true is returned, scrollTo (mScroller) is not finished. getCurrX (), mScroller. getCurrY (); // generate a smooth animation effect. Based on the current offset, each time you scroll a little postInvalidate (); // you also need to refresh the View, otherwise, an error may occur.} else {// if false is returned, it indicates that startScroll completes the Log. I (TAG, "scoller has finished") ;}@overridepublic boole An onTouchEvent (MotionEvent event) {final int action = event. getAction (); final float x = event. getX (); final float y = event. getY (); Log. d (TAG, "X:" + x + "Y:" + y); float oldx = 0; switch (action) {case MotionEvent. ACTION_DOWN: oldx = event. getRawX (); Log. I ("", "onTouchEvent ACTION_DOWN"); if (mVelocityTracker = null) {mVelocityTracker = VelocityTracker. obtain (); mVelocityTracker. addMovement (event);} if (! MScroller. isFinished () {mScroller. abortAnimation ();} mLastMotionX = x; break; case MotionEvent. ACTION_MOVE: int deltaX = (int) (mLastMotionX-x); float newX = event. getRawX ();/*** jump to the last page */if (newX-oldx> 3) {if (currentPage = pagesize-1 &&! Go_main) {go_main = true; preferences = mContext. getSharedPreferences ("setting", 0); SharedPreferences. editor editor = preferences. edit (); editor. putBoolean ("isOpen", true); editor. commit (); Intent intent = new Intent (); intent. setClass (mContext, MainActivity. class); // jump to the first page; mContext. startActivity (intent) ;}}if (IsCanMove (deltaX) {if (mVelocityTracker! = Null) {mVelocityTracker. addMovement (event);} mLastMotionX = x; scrollBy (deltaX, 0);} break; case MotionEvent. ACTION_UP: int velocityX = 0; if (mVelocityTracker! = Null) {mVelocityTracker. addMovement (event); mVelocityTracker. computeCurrentVelocity (1000); velocityX = (int) mVelocityTracker. getXVelocity ();} if (velocityX> SNAP_VELOCITY & mCurScreen> 0) {// move Log to the left. e (TAG, "snap left"); snapToScreen (mCurScreen-1);} else if (velocityX <-SNAP_VELOCITY & mCurScreen <getChildCount ()-1) {// move Log to the right. e (TAG, "snap right"); snapToScreen (mCurScreen + 1);} else {snapT ODestination ();} if (mVelocityTracker! = Null) {mVelocityTracker. recycle (); mVelocityTracker = null;} break;} return true;} private boolean IsCanMove (int deltaX) {if (getScrollX () <= 0 & deltaX <0) {return false;} if (getScrollX () >=( getChildCount ()-1) * getWidth () & deltaX> 0) {return false;} return true ;} public void SetOnViewChangeListener (OnViewChangeListener listener) {mOnViewChangeListener = listener ;} /// ** // * determine whether to enable the index. // * @ return/* // private boolean isOpean (){////}}
Ii. Define Interfaces
package com.genius.scroll;public interface OnViewChangeListener {public void OnViewChange(int view);}
3. Create an instance 1. Create ViewPagerAdapter. java:
Package com. genius. demo; import java. util. list; import android. OS. parcelable; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. view;/*** ViewPgageAdapter * @ author yayun **/public class ViewPageAdapter extends PagerAdapter {List <View> mViewList; public ViewPageAdapter (List <View> viewList) {mViewList = viewList;} @ Overridepublic int getCount () {If (mViewList! = Null) {return mViewList. size () ;}return 0 ;}@ Overridepublic Object instantiateItem (View view, int index) {(ViewPager) view ). addView (mViewList. get (index), 0); return mViewList. get (index) ;}@ Overridepublic void destroyItem (View view, int position, Object arg2) {(ViewPager) view ). removeView (mViewList. get (position) ;}@ Overridepublic void finishUpdate (View arg0) {}@ Overridepublic boolean isViewFromObject (View view, Object obj) {return (view = obj );} @ Overridepublic void restoreState (Parcelable arg0, ClassLoader arg1) {}@ Overridepublic Parcelable saveState () {return null ;}@ Overridepublic void startUpdate (View arg0 ){}}
2. Create SwitchViewDemoActivity. java:
Package com. genius. demo; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. linearLayout; import com. genius. scroll. myScrollLayout; import com. genius. scroll. onViewChangeListener; public class SwitchViewDemoActivity extends Activity implementsOnViewChangeListener, OnClickListener {private MyScrollLayout mScrollLayout; private ImageView [] mImageViews; private int mViewCount; private int mCurSel; /** Activity object **/public static Activity MY_ACTIVITY; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); MY_ACTIVITY = this; init ();} private void init () {mScrollLayout = (MyScrollLayout) findViewById (R. id. scrollLayout); LinearLayout linearLayout = (LinearLayout) findViewById (R. id. llayout); mViewCount = mScrollLayout. getChildCount (); mImageViews = new ImageView [mViewCount]; for (int I = 0; I <mViewCount; I ++) {mImageViews [I] = (ImageView) linearLayout. getChildAt (I); mImageViews [I]. setEnabled (true); mImageViews [I]. setOnClickListener (this); mImageViews [I]. setTag (I);} mScrollLayout. setPageSize (mImageViews. length); mCurSel = 0; mImageViews [mCurSel]. setEnabled (false); mScrollLayout. setOnViewChangeListener (this);} private void setCurPoint (int index) {if (index <0 | index> mViewCount-1 | mCurSel = index) {return ;} mImageViews [mCurSel]. setEnabled (true); mImageViews [index]. setEnabled (false); mScrollLayout. setPosition (index); mCurSel = index ;}@ Overridepublic void OnViewChange (int view) {setCurPoint (view) ;}@ Overridepublic void onClick (View v) {int pos = (Integer) (v. getTag (); setCurPoint (pos); mScrollLayout. snapToScreen (pos );}}
3. layout file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/touch" xmlns:android="http://schemas.android.com/apk/res/android"> <com.genius.scroll.MyScrollLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:background="@drawable/guide01" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> <FrameLayout android:background="@drawable/guide02" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> <FrameLayout android:background="@drawable/guide03" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> <FrameLayout android:background="@drawable/guide04" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> <FrameLayout android:background="@drawable/guide05" android:layout_width="fill_parent" android:layout_height="fill_parent"> </FrameLayout> </com.genius.scroll.MyScrollLayout> <LinearLayout android:orientation="horizontal" android:id="@+id/llayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="24.0dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"> <ImageView android:clickable="true" android:padding="15.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/guide_round" /> <ImageView android:clickable="true" android:padding="15.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/guide_round" /> <ImageView android:clickable="true" android:padding="15.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/guide_round" /> <ImageView android:clickable="true" android:padding="15.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/guide_round" /> <ImageView android:clickable="true" android:padding="15.0dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/guide_round" /> </LinearLayout> </RelativeLayout>
4. Run the instance:
If you like me, please follow me!
Source code download
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.