Select the left outbound menu and outbound menu

Source: Internet
Author: User

Select the left outbound menu and outbound menu

  
 
 <span style="font-family: Arial, Helvetica, sans-serif;">package com.support;</span>
Import com. nineoldandroids. animation. animator; import com. nineoldandroids. animation. animatorListenerAdapter; import com. nineoldandroids. animation. objectAnimator; import android. annotation. suppressLint; import android. annotation. targetApi; import android. content. context; import android. graphics. color; import android. OS. build; import android. util. attributeSet; import android. view. motionEvent; import android. vi Ew. view; import android. view. viewConfiguration; import android. view. viewTreeObserver. onGlobalLayoutListener; import android. view. animation. accelerateInterpolator; import android. view. animation. bounceInterpolator; import android. widget. frameLayout; public class MenuContainer extends FrameLayout {private Context context; private int slideMenuWidth; private int time = 500; public MenuContainer (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); this. context = context; checkChildCount (); // TODO Auto-generated constructor stub} public MenuContainer (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; checkChildCount (); // TODO Auto-generated constructor stub} public MenuContainer (Context context) {super (context); this. context = context; checkChildC Ount (); // TODO Auto-generated constructor stub} @ TargetApi (Build. VERSION_CODES.HONEYCOMB) public void openMenu () {getChildAt (1 ). setVisibility (View. VISIBLE); getChildAt (2 ). setTranslationX (slideMenuWidth);} public void hideMenu () {ObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (2), "translationX", slideMenuWidth, 0); animator. setInterpolator (new AccelerateInterpolator (); animator. setDuration (Time); animator. start (); getChildAt (1 ). setVisibility (View. GONE);} private void checkChildCount () {getViewTreeObserver (). addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ SuppressLint ("NewApi") @ Overridepublic void onGlobalLayout () {// TODO Auto-generated method stubtry {getViewTreeObserver (). removeOnGlobalLayoutListener (this);} catch (Exception e) {// TODO Auto-generated catch blocke. printStac KTrace ();} if (getChildCount ()! = 2) throw new RuntimeException ("MenuContainer must have two view"); View hideView = getChildAt (0); View darkView = new View (context); darkView. setBackgroundColor (Color. parseColor ("#88000000"); darkView. setVisibility (View. GONE); // adjust the order so that the interface you want to display is located at the bottom of FrameLatout, adding a translucent control in the middle, and adding a hidden control on the top to removeViewAt (0); addView (darkView ); addView (hideView); FrameLayout. layoutParams params = (LayoutParams) hideView. getLayoutPara MS (); params. leftMargin =-hideView. getWidth (); // move the control to be hidden on the left to hideView on the left. setLayoutParams (params); slideMenuWidth = hideView. getWidth (); darkView. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (2), "translationX", slideMenuWidth, 0); animator. setDuration (time); animator. setInterpo Lator (new AccelerateInterpolator (); animator. start (); getChildAt (1 ). setVisibility (View. GONE) ;}}); // System. out. println ("child" + getChildAt (0 ). toString () + "" + getChildAt (1 ). toString () + "" + getChildAt (2 ). toString () + "") ;}}); // TODO Auto-generated method stub // if (getChildCount ()! = 2) // throw new RuntimeException ("MenuContainer must have two view");} private float downX; private float downY; private final int scaledTouchSlop = 8; @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {// TODO Auto-generated method stubif (ev. getAction () = MotionEvent. ACTION_DOWN) {downX = ev. getRawX (); downY = ev. getRawY (); // System. out. println (downX + "" + downY); return super. onInterceptTouchEvent (ev);} if (ev. getAction () = MotionEvent. ACTION_MOVE) {float x = ev. getRawX (); float y = ev. getRawY (); // ViewConfiguration. get (context ). the getScaledTouchSlop () mobile phone can recognize the minimum sliding distance if (x-downX> Math. abs (y-downY) & Math. abs (x-downX)> = ViewConfiguration. get (context ). getScaledTouchSlop () & downX <dip2px (scaledTouchSlop) {getChildAt (1 ). setVisibility (View. VISIBLE); // System. out. println ("view" + getChildAt (1 ). getY (); return true ;}} return super. onInterceptTouchEvent (ev);} @ TargetApi (Build. VERSION_CODES.HONEYCOMB) @ Overridepublic boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stubswitch (event. getAction () {case MotionEvent. ACTION_MOVE: if (downX <dip2px (scaledTouchSlop) & getChildAt (2 ). getTranslationX () <slideMenuWidth & event. getRawX () <slideMenuWidth) {getChildAt (1 ). setVisibility (View. VISIBLE); getChildAt (2 ). setTranslationX (event. getRawX ()-downX);} break; case MotionEvent. ACTION_UP: case MotionEvent. ACTION_CANCEL: if (downX <dip2px (scaledTouchSlop) {float t = getChildAt (2 ). getTranslationX (); if (t> slideMenuWidth/4) {// ObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (2), "translationX", t, slideMenuWidth); animator. setInterpolator (new BounceInterpolator (); animator. setDuration (time); animator. start ();} else {ObjectAnimator animator = ObjectAnimator. ofFloat (getChildAt (2), "translationX", t, 0); animator. setDuration (time); animator. start () ;}} break;} return true;} public int dip2px (float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f );}}
</pre><pre code_snippet_id="660890" snippet_file_name="blog_20150507_8_4103380" name="code" class="html">
</pre><pre code_snippet_id="660890" snippet_file_name="blog_20150507_8_4103380" name="code" class="html">
</pre><p></p><pre code_snippet_id="660890" snippet_file_name="blog_20150507_9_1722276" name="code" class="html">package com.support;import android.app.Activity;import android.os.Bundle;public class MainActivity extends  Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    }



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:orientation="vertical"     >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <com.support.MenuContainer         android:layout_width="match_parent"        android:layout_height="match_parent">                <LinearLayout android:id="@+id/hideView"            android:layout_width="200dp"            android:background="#ffd8d8d8"            android:layout_height="match_parent"            android:orientation="vertical">            <Button                 android:layout_marginTop="10dp"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="hide"/>        </LinearLayout>                               <LinearLayout             android:background="#fff"            android:id="@+id/mainView"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="xxxx"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="xxxx"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="xxxx"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="xxxx"/>            <Button                 android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="xxxx"/>        </LinearLayout>                  </com.support.MenuContainer></LinearLayout>



Related Article

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.