Android custom component series [15]-menu implementation with four sliding directions, android custom

Source: Internet
Author: User
Tags gety

Android custom component series [15]-menu implementation with four sliding directions, android custom

Today, I accidentally implemented a menu that slides in four directions. It seems very interesting and smooth to slide. I have never seen such an application yet, in the future, it is hard to say whether it can appear in the application or whether it has practical value. If it has already been made, it will be posted so that everyone can play with it. It may be helpful to you.

I. effect demonstration

(Note: currently, the Android simulator is not installed. If the dynamic image is too choppy, paste a static image. You can download the source code to view the actual effect)


(Slide up)


(Slide down)


(Sliding left)


(Slide to the right)

II. Implementation Process

1. place 5 Views (Up, down, left, and right)

@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {mTopView.layout(0, -mViewHeight, mViewWidth, 0);mBottomView.layout(0, mViewHeight, mViewWidth, 2 * mViewHeight);mCenterView.layout(0, 0, mViewWidth, mViewHeight);mLeftView.layout(-mViewWidth, 0, 0, mViewHeight);mRightView.layout(mViewWidth, 0, 2 * mViewWidth, mViewHeight);}

Reprint please explain the source: http://blog.csdn.net/dawanganban

2. Determine the moving direction through the onTouchEvent event

Private float mDownY; private float mDownX; @ Overridepublic boolean onTouchEvent (MotionEvent event) {int disY; int disX; float eventY = event. getY (); float eventX = event. getX (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: mDownY = eventY; mDownX = eventX; break; case MotionEvent. ACTION_UP: disY = (int) (eventY-mDownY); disX = (int) (eventX-mDownX); if (Math. abs (disY)> Math. abs (disX) {if (Math. abs (disY)> MIN_VIEW_HEIGHT/2) {if (disY> 0) {// slide Log down. d (TAG, "TO_BOTTOM"); changeToBottom ();} else {// slide the Log up. d (TAG, "TO_TOP"); changeToTop () ;}} else {if (Math. abs (disX)> MIN_VIEW_WIDTH/2) {if (disX> 0) {// slide the Log to the right. d (TAG, "TO_RIGHT"); changeToRight ();} else {// slide Log to the left. d (TAG, "TO_LEFT"); changeToLeft () ;}} break; default: break;} return true ;}
3. Smooth Movement using the computerScroll () method

@Overridepublic void computeScroll() {super.computeScroll();if(mScroller.computeScrollOffset()){scrollTo(mScroller.getCurrX(), mScroller.getCurrY());postInvalidate();}}
4. Determine the critical condition (otherwise it will continue to slide in one direction)

int[] location = new int[2];mCenterView.getLocationOnScreen(location);if(location[1] >= mViewHeight - MIN_VIEW_HEIGHT * 2) return;
For example, the above Code is the critical condition for judging downward sliding. location [1] represents the y coordinate of the center View (relative to the screen ).

Iii. source code of the entire View

Package com. example. tew.x4update; import android. content. context; import android. graphics. color; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; import android. widget. scroller;/*** custom View that can be dragged * @ author sunshine small strong http://blog.csdn.net/dawanganban **/public class MyCanPullView extends ViewGroup {private static final int MIN_VIEW_HEIGHT = 200; private static final int MIN_VIEW_WIDTH = 400; private static final String TAG = "TEST"; private int mViewHeight; private int mViewWidth; private View mTopView; private View mBottomView; private View mCenterView; private View mLeftView; private View mRightView; private Scroller mScroller; public MyCanPullView (Context context, AttributeSet attrs) {super (context, attrs); initView (context ); mScroller = new Scroller (context);} private void initView (Context context) {setTopView (context); setBottomView (context); setCenterView (context); setLeftView (context ); setRightView (context);} private float mDownY; private float mDownX; @ Overridepublic boolean onTouchEvent (MotionEvent event) {int disY; int disX; float eventY = event. getY (); float eventX = event. getX (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: mDownY = eventY; mDownX = eventX; break; case MotionEvent. ACTION_UP: disY = (int) (eventY-mDownY); disX = (int) (eventX-mDownX); if (Math. abs (disY)> Math. abs (disX) {if (Math. abs (disY)> MIN_VIEW_HEIGHT/2) {if (disY> 0) {// slide Log down. d (TAG, "TO_BOTTOM"); changeToBottom ();} else {// slide the Log up. d (TAG, "TO_TOP"); changeToTop () ;}} else {if (Math. abs (disX)> MIN_VIEW_WIDTH/2) {if (disX> 0) {// slide the Log to the right. d (TAG, "TO_RIGHT"); changeToRight ();} else {// slide Log to the left. d (TAG, "TO_LEFT"); changeToLeft () ;}} break; default: break;} return true;} private void changeToBottom () {int [] location = new int [2]; mCenterView. getLocationOnScreen (location); if (location [1]> = mViewHeight-MIN_VIEW_HEIGHT * 2) return; int dy = (int) (mViewHeight-MIN_VIEW_HEIGHT); mScroller. startScroll (0, getScrollY (), 0,-dy, 500); invalidate ();} private void changeToTop () {int [] location = new int [2]; mTopView. getLocationOnScreen (location); if (location [1] <=-mViewHeight-MIN_VIEW_HEIGHT/2) return; int dy = (int) (mViewHeight-MIN_VIEW_HEIGHT); mScroller. startScroll (0, getScrollY (), 0, dy, 500); invalidate ();} private void changeToRight () {int [] location = new int [2]; mCenterView. getLocationOnScreen (location); if (location [0]> = mViewWidth-MIN_VIEW_WIDTH * 2) return; int dx = (int) (mViewWidth-MIN_VIEW_WIDTH); mScroller. startScroll (getScrollX (), 0,-dx, 0,500); invalidate ();} private void changeToLeft () {Log. d (TAG, "TO_LEFT"); int [] location = new int [2]; mLeftView. getLocationOnScreen (location); if (location [0] <=-mViewWidth-MIN_VIEW_WIDTH/2) return; int dx = (int) (mViewWidth-MIN_VIEW_WIDTH); mScroller. startScroll (getScrollX (), 0, dx, 0,500); invalidate () ;}@ Overridepublic void computeScroll () {super. computeScroll (); if (mScroller. computescroloffset () {scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); postInvalidate () ;}@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {mTopView. layout (0,-mViewHeight, mViewWidth, 0); mBottomView. layout (0, mViewHeight, mViewWidth, 2 * mViewHeight); mCenterView. layout (0, 0, mViewWidth, mViewHeight); mLeftView. layout (-mViewWidth, 0, 0, mViewHeight); mRightView. layout (mViewWidth, 0, 2 * mViewWidth, mViewHeight);} @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); // obtain the width and height of the entire View. mViewWidth = MeasureSpec. getSize (widthMeasureSpec); mViewHeight = MeasureSpec. getSize (heightMeasureSpec);} private void setTopView (Context context) {View topButton = new View (context); topButton. setBackgroundColor (Color. RED); mTopView = topButton; this. addView (mTopView);} private void setBottomView (Context context) {View bottomButton = new View (context); bottomButton. setBackgroundColor (Color. GREEN); mBottomView = bottomButton; this. addView (mBottomView);} private void setCenterView (Context context) {View centerButton = new View (context); centerButton. setBackgroundColor (Color. WHITE); mCenterView = centerButton; this. addView (mCenterView);} private void setLeftView (Context context) {View leftButton = new View (context); leftButton. setBackgroundColor (Color. BLUE); mLeftView = leftButton; this. addView (mLeftView);} private void setRightView (Context context) {View rightButton = new View (context); rightButton. setBackgroundColor (Color. YELLOW); mRightView = rightButton; this. addView (mRightView );}}
Obtain all source code. Add the source code to the group and obtain it in group sharing (142979499)

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.