简单侧拉菜单

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.bwie.sldingmenu.MainActivity">
            android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >


                            android:layout_width="match_parent"
                android:layout_height="match_parent">

                                    android:background="@mipmap/ic_blackground"

                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/timg" >
            

        

    

2 主Activity

public class MainActivity extends AppCompatActivity {



    private SlidingMenu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menu=(SlidingMenu)findViewById(R.id.menu);
    }

}

3 继承HorzizontalScrollView 自带侧滑

public class SlidingMenu extends HorizontalScrollView {

    /**
     16.     * 屏幕宽度
     17.     */
     private int mScreenWidth;
       /**
     20.     * dp
     21.     */
               private int mMenuRightPadding = 50;
      /**
     24.     * 菜单的宽度
     25.     */
                private int mMenuWidth;
      private int mHalfMenuWidth;

              private boolean once;

               public SlidingMenu(Context context, AttributeSet attrs)
    {
              super(context, attrs);
             mScreenWidth = ScreenUtils.getScreenWidth(context);
          }

              @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
     {
               /**
         41.         * 显示的设置一个宽度
         42.         */
             if (!once)
                {
                      LinearLayout wrapper = (LinearLayout) getChildAt(0);
                     ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
                     ViewGroup content = (ViewGroup) wrapper.getChildAt(1);
                      // dp to px
                      mMenuRightPadding = (int) TypedValue.applyDimension(
                                     TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, content
                                     .getResources().getDisplayMetrics());

                      mMenuWidth = mScreenWidth - mMenuRightPadding;
                        mHalfMenuWidth = mMenuWidth / 2;
                        menu.getLayoutParams().width = mMenuWidth;
                      content.getLayoutParams().width = mScreenWidth;

                 }
               super.onMeasure(widthMeasureSpec, heightMeasureSpec);
           }

               @Override
      protected void onLayout(boolean changed, int l, int t, int r, int b)
      {
             super.onLayout(changed, l, t, r, b);
               if (changed)
                   {
                       // 将菜单隐藏
                       this.scrollTo(mMenuWidth, 0);
                      once = true;
                    }
           }

               @Override
       public boolean onTouchEvent(MotionEvent ev)
       {
                int action = ev.getAction();
              switch (action)
                {
                  // Up时,进行判断,如果显示区域大于菜单宽度一半则完全显示,否则隐藏
                    case MotionEvent.ACTION_UP:
                          int scrollX = getScrollX();
                            if (scrollX > mHalfMenuWidth)
                                  this.smoothScrollTo(mMenuWidth, 0);
                          else
                              this.smoothScrollTo(0, 0);
                          return true;
                  }
               return super.onTouchEvent(ev);
          }

}

4 第三部里会报错 创建ScreenUtils

public class ScreenUtils {

    private ScreenUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context)
    {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context)
    {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context)
    {

        int statusHeight = -1;
        try
        {
            Class clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 获取当前屏幕截图,包含状态栏
     *
     * @param activity
     * @return
     */
    public static Bitmap snapShotWithStatusBar(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;

    }

    /**
     * 获取当前屏幕截图,不包含状态栏
     *
     * @param activity
     * @return
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return bp;

    }

}

5 效果图


你可能感兴趣的:(简单侧拉菜单)