自定义SiidingMenu简单实现

先看效果:

实现思路:

自定义Slidingmenu继承HorizontalScrollView嵌套 左侧菜单布局+右侧内容布局,滑动时分别显示

菜单布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:gravity="center"
 7     android:background="#fff000"
 8     >
 9 <LinearLayout
10     android:layout_width="wrap_content"
11     android:layout_height="wrap_content"
12     android:orientation="horizontal"
13     android:gravity="center">
14     <ImageView
15         android:src="@mipmap/ic_launcher"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content" />
18     <TextView
19         android:text="第一个按钮"
20         android:gravity="center"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content" />
23 </LinearLayout>
24 <LinearLayout
25     android:layout_width="wrap_content"
26     android:layout_height="wrap_content"
27     android:orientation="horizontal"
28     android:gravity="center">
29     <ImageView
30         android:src="@mipmap/ic_launcher"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content" />
33     <TextView
34         android:text="第二个按钮"
35         android:gravity="center"
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content" />
38 </LinearLayout>
39 <LinearLayout
40     android:layout_width="wrap_content"
41     android:layout_height="wrap_content"
42     android:orientation="horizontal"
43     android:gravity="center">
44     <ImageView
45         android:src="@mipmap/ic_launcher"
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content" />
48     <TextView
49         android:text="第三个按钮"
50         android:gravity="center"
51         android:layout_width="wrap_content"
52         android:layout_height="wrap_content" />
53 </LinearLayout>
54 <LinearLayout
55     android:layout_width="wrap_content"
56     android:layout_height="wrap_content"
57     android:orientation="horizontal"
58     android:gravity="center">
59     <ImageView
60         android:src="@mipmap/ic_launcher"
61         android:layout_width="wrap_content"
62         android:layout_height="wrap_content" />
63     <TextView
64         android:text="第四个按钮"
65         android:gravity="center"
66         android:layout_width="wrap_content"
67         android:layout_height="wrap_content" />
68 </LinearLayout>
69 </LinearLayout>

效果:

主布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:background="@color/colorPrimaryDark"
 5     android:id="@+id/activity_main"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     tools:context="com.example.king.slidingmenutest.MainActivity">
 9     <!--自定义Slidingmenu继承HorizontalScrollView-->
10     <com.example.king.slidingmenutest.silding_menu.SildingMenu
11         android:id="@+id/sp_menu"
12         android:layout_width="match_parent"
13         android:layout_height="match_parent"
14         android:scrollbars="none"
15         >
16         <LinearLayout
17             android:orientation="horizontal"
18             android:layout_width="wrap_content"
19             android:layout_height="match_parent">
20             <include layout="@layout/menu_layout"/>
21             <LinearLayout
22                 android:background="@color/colorAccent"
23                 android:layout_width="wrap_content"
24                 android:layout_height="match_parent"
25                 android:orientation="vertical">
26
27             </LinearLayout>
28         </LinearLayout>
29     </com.example.king.slidingmenutest.silding_menu.SildingMenu>
30 </RelativeLayout>

效果:

自定义Slidingmenu类:

  1 package com.example.king.slidingmenutest.silding_menu;
  2
  3 import android.content.Context;
  4 import android.util.AttributeSet;
  5 import android.util.DisplayMetrics;
  6 import android.util.TypedValue;
  7 import android.view.MotionEvent;
  8 import android.view.ViewGroup;
  9 import android.view.WindowManager;
 10 import android.widget.HorizontalScrollView;
 11 import android.widget.LinearLayout;
 12
 13 /**
 14  * Created by King on 2016/9/6.
 15  */
 16
 17 public class SildingMenu extends HorizontalScrollView {
 18     //主布局
 19     private LinearLayout mWapper;
 20     //菜单布局
 21     private ViewGroup mMenu;
 22     //内容布局
 23     private ViewGroup mContent;
 24     //屏幕宽度
 25     private int screenWidth;
 26     //菜单布局宽度
 27     private int mMenuwidth;
 28     //内容布局宽度
 29     private  int mContentwidth;
 30     //菜单布局内边距
 31     private int mMenuRightPadding = 50;
 32
 33     public SildingMenu(Context context) {
 34         super(context);
 35
 36     }
 37
 38     public SildingMenu(Context context, AttributeSet attrs) {
 39         super(context, attrs);
 40         //获得屏幕宽度
 41        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 42         DisplayMetrics displayMetrics = new DisplayMetrics();
 43         windowManager.getDefaultDisplay().getMetrics(displayMetrics);
 44         screenWidth = displayMetrics.widthPixels;
 45         //内边距转成dip类型
 46         mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics());
 47
 48     }
 49
 50     public SildingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
 51         super(context, attrs, defStyleAttr);
 52
 53     }
 54     //测量高宽
 55     @Override
 56     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 57         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 58         //主布局
 59         mWapper = (LinearLayout) getChildAt(0);
 60         //菜单布局
 61         mMenu  = (ViewGroup) mWapper.getChildAt(0);
 62         //内容布局
 63         mContent  = (ViewGroup) mWapper.getChildAt(1);
 64         //菜单布局宽度
 65         mMenuwidth = mMenu.getLayoutParams().width = screenWidth-mMenuRightPadding;
 66         //内容布局宽度
 67         mContentwidth = mContent.getLayoutParams().width = screenWidth;
 68     }
 69     //摆放
 70     @Override
 71     protected void onLayout(boolean changed, int l, int t, int r, int b) {
 72         super.onLayout(changed, l, t, r, b);
 73         if(changed){
 74             //侧滑时
 75             this.scrollTo(mMenuwidth,0);
 76         }
 77     }
 78     //根据滑动幅度判断
 79     @Override
 80     public boolean onTouchEvent(MotionEvent ev) {
 81         int action = ev.getAction();
 82         switch (action){
 83             case MotionEvent.ACTION_UP:
 84                 int scrolWidth = getScrollX();
 85                 if(scrolWidth>=mContentwidth/2){
 86                     this.smoothScrollTo(mMenuwidth,0);
 87                 }else{
 88                     this.smoothScrollTo(0,0);
 89                 }
 90                 return true;
 91         }
 92         return super.onTouchEvent(ev);
 93
 94     }
 95
 96     @Override
 97     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
 98         super.onScrollChanged(l, t, oldl, oldt);
 99         float scale = l*1.0f/mMenuwidth;
100         float rightScale = 0.7f+0.3f*scale;
101         float leftScale = 1.0f-scale*0.3f;
102         float leftAlpha = 0.3f+0.7f*(1-scale);
103         //设置动画
104         mMenu.setScaleX(leftScale);
105         mMenu.setScaleY(leftScale);
106         mMenu.setAlpha(leftAlpha);
107         mMenu.setTranslationX(scale);
108         mContent.setPivotX(0);
109         mContent.setPivotY(mContent.getHeight()/2);
110         mContent.setScaleX(rightScale);
111         mContent.setScaleY(rightScale);
112     }
113 }
时间: 2024-08-19 04:10:50

自定义SiidingMenu简单实现的相关文章

Android peferenceActivity 自定义标题简单方法

Android peferenceActivity 自定义标题简单方法 peferenceActivity 完全使用定义好的布局. 因此不能简单象其它好窗口进行自定,现在我们需要加 一个自定义标题,比如象其它窗口一样加一个统一topbar. 假设这个topbar的布局是 title.xml 一.标准自定义标题栏方法 Android 提供自定义标题栏方法 我们简单实现. @Override protected void onCreate(Bundle savedInstanceState) { f

JSP自定义标签——简单标签(2)

在前一篇博客中,我们已经学习了自定义的简单标签的基本使用方法,这一篇我们来学习如何在简单标签中添加标签属性.对自定义标签添加一些属性,可以使我们的标签功能更加灵活和复用.例如前一篇博客使用简单标签来对标签体内容执行一定的次数,就无法在标签上规定要执行的次数,必须在标签处理器类中修改,很不方便,如果使用带属性的标签就能很好的解决这个问题. 要想使简单标签具有属性,通常需要满足以下两个步骤: ① 在标签处理器类中定义属性,同时为每个属性生成setter方法: ② 在TLD文件中对于的<tag>标签

Android UI之自定义——最简单的仿QQ音乐歌词颜色渐变

Android UI之自定义--最简单的仿QQ音乐歌词颜色渐变 记得刚开始做android的时候,就发现QQ音乐歌词颜色渐变的效果,就在网上搜索过,但是就是没有找到满意的.今天突然用QQ音乐听歌的时候,看到歌词颜色渐变,决定来分析看看,没想到实现原来如此简单.这篇只是将最简单的歌词颜色渐变功能,不包括歌词滚动等效果. 首先来看下QQ音乐歌词界面 实现步骤 从界面上可以看出,是通过不同颜色的文本叠加所形成的视觉效果.那么android文本一般使用TextView实现,那就来试试用TextView在

基于配置的自定义的简单struts(了解struts原理)

struts的基于配置的action跳转方式使用起来特别方便,对此我也模仿struts的action,实现一个自己定义的通过配置文件配置action进行简单的操作: 首先,可以知道sturts是通过过滤器来拦截浏览器发送的请求,再在过滤器里进行操作,实现这个流程. 那么,我们也定义一个filter在我们的程序中: public class StrutsFilter implements Filter{ public void doFilter(ServletRequest arg0, Servl

Android自定义Dialog简单实例

做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合我们应用的样式. 自定义Dialog需要3步骤即可: 1.主要的重写Dialog的Java类 2.清除Dialog Theme,在style.xml文件中加一个即可 3.使用方法 一.创建CustomPopDialog2.java类 package com.lenovocw.music.app.widget; import com.lenovocw.zhuhaizxt.R; import

Android零基础入门第24节:自定义View简单使用

当我们开发中遇到Android原生的组件无法满足需求时,这时候就应该自定义View来满足这些特殊的组件需求. 一.概述 很多初入Android开发的程序员,对于Android自定义View可能比较恐惧,但这又是高手进阶的必经之路,这里先不做过多学习,只是简单了解.关于高阶的内容会在后续课程陆续进行学习,欢迎关注分享达人秀(ShareExpert)获取第一手教程. 如果说要按类型来划分的话,自定义View的实现方式大概可以分为三种:自绘控件.组合控件.以及继承控件. 自绘控件:内容都是开发者自己绘

Android自定义View 简单实现多图片选择控件

前言 相信很多朋友在开发中都会遇到图片上传的情况,尤其是多图上传,最 经典的莫过于微信的图片选择了.所有很多情况下会使用到多图选择. 所以就有了这篇文章,今天抽点时间写了个控件. 支持自定义选择图片的样式 支持设置图片选择数量 支持图片预览,删除 支持图片拍照 先来看看效果 实现分析 假如不定义控件,我们要实现这样一个功能,无非是写个GridView在item点击的时候去显示图片进行选择,在返回界面的时候进行GridView的数据刷新.我们把这些逻辑写在我们自定义的GridView中,就成了一个

android自定义permission简单实用手册

自定义permission可以提高app的安全性.通过设定permission,可以保护app免受非法授权的app的访问.比如我们在androidmanifest.xml中会注册activity/service/reciever,在intent-filter中filter我们要接收的action,如果有个不是我们知道的发送源发给我们一个匹配的action,就有可能影响到我们的app,甚至可能危害到用户. 如何使用 注册permission 在对应的activity/service和recieve

自定义实现简单的Android颜色选择器(附带源码)

在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几种颜色供User去选择.于是就自己自定义实现一个Simple ColorSelector,效果图如下 如何去使用这个ColorSelector呢,见下面代码: final ColorSelectorDialog colorDialog = new ColorSelectorDialog( mCont