自定义slideMenu
来源:互联网 发布:零基础学编程需要多久 编辑:程序博客网 时间:2024/05/17 17:59
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
自定义view
//布局 样式1 2 是一样的package com.example.slidemenu;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.MotionEvent;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;import com.nineoldandroids.view.ViewHelper;/** * Name: SlideMenu * Action: * Author: liuan * creatTime:2017-06-24 19:27 */public class SlideMenu extends HorizontalScrollView { private LinearLayout mWapepr; private ViewGroup mMenu; private ViewGroup mContent; private int mScreenWidth; private int mMenuWidth; private int mMenuRightPadding = 50; private boolean once = false; private boolean isOpen = false; private boolean isDrawe = false; private boolean isSpeciallyGoodEffct = false; public SlideMenu(Context context) { this(context, null); } /** * 当使用了自定义属性的时候 会走此构造方法 * * @param context * @param attrs * @param defStyleAttr */ public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //获取我们自定以的属性 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenu, defStyleAttr, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.SlideMenu_rightPadding: mMenuRightPadding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics())); break; } } a.recycle(); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mScreenWidth = outMetrics.widthPixels;//// //把dp转化为px// mMenuRightPadding= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics()); } /** * 未使用自定义属性时候 调用 * * @param context * @param attrs */ public SlideMenu(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** * 设置子View的宽和高 * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (!once) { mWapepr = (LinearLayout) getChildAt(0); mMenu = (ViewGroup) mWapepr.getChildAt(0); mContent = (ViewGroup) mWapepr.getChildAt(1); mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding; mContent.getLayoutParams().width = mScreenWidth; mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding; once = true; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 通过设置偏移量 将menu隐藏 * * @param changed * @param l * @param t * @param r * @param b */ @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); } } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_UP: //隐藏在左边的宽度 int scrollX = getScrollX(); if (scrollX >= mMenuWidth / 2) { this.smoothScrollTo(mMenuWidth, 0); isOpen = false; } else { this.smoothScrollTo(0, 0); isOpen = true; } return true; } return super.onTouchEvent(ev); } public void openMenu() { if (isOpen) { return; } else { this.smoothScrollTo(0, 0); isOpen = true; } } public void CloseMenu() { if (!isOpen) { return; } else { this.smoothScrollTo(mMenuWidth, 0); isOpen = false; } } //切换菜单 public void toggle() { if (isOpen) { CloseMenu(); return; } openMenu(); } public void setIsDrawe(Boolean is) { this.isDrawe = is; } public void setSpeciallyGoodEffect(Boolean is) { this.isSpeciallyGoodEffct = is; } /** * 滚动发生的时候 * * @param l * @param t * @param oldl * @param oldt */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); float scale = l * 1.0f / mMenuWidth;//1~0 float translationX = mMenuWidth * scale; if(isDrawe&&!isSpeciallyGoodEffct){ }else if(isDrawe&&isSpeciallyGoodEffct){ translationX*=0.8; } if (isDrawe) { //调用属性动画 设置 ViewHelper.setTranslationX(mMenu, translationX); //区别1 1.0-0.7 缩放效果 //scale :1.0-0.0 //0.7+0.3*scale //区别2 菜单的偏移量需要修改 //区别3 菜单的显示时 有缩放 以及透明度变化 /* * 缩放:0.7-1.0 * 1.0-scale*0.3 * 透明度0.6-1.0 * 0.6+0.4*(1-scale); * */ if (isSpeciallyGoodEffct) { float rightScale = 0.7f + 0.3f * scale; float leftScale = 1.0f-0.3f * scale; float leftAlpha = 0.6f+0.4f * (1-scale); //设置content的缩放的中心点 ViewHelper.setPivotX(mContent,0); ViewHelper.setPivotY(mContent,mContent.getHeight()/2); ViewHelper.setScaleX(mContent,rightScale); ViewHelper.setScaleY(mContent,rightScale); ViewHelper.setScaleX(mMenu,leftScale); ViewHelper.setScaleY(mMenu,leftScale); ViewHelper.setAlpha(mMenu,leftAlpha); } } }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.slidemenu.SlideMenu android:id="@+id/view_slideMenu" app:rightPadding="100dp" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_menu"></include> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <Button android:id="@+id/bt_toogle" android:text="切换菜单" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </com.example.slidemenu.SlideMenu></RelativeLayout>//样式3 4又是一个样子的
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.slidemenu.SlideMenu android:background="@color/colorPrimary" android:id="@+id/view_slideMenu" app:rightPadding="100dp" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_menu"></include> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <Button android:id="@+id/bt_toogle" android:text="切换菜单" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </com.example.slidemenu.SlideMenu></RelativeLayout>left
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@color/colorPrimary" android:layout_height="match_parent"><LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_centerInParent="true" android:layout_height="wrap_content"><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_pic1" android:src="@mipmap/ic_launcher" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="50dp" android:layout_centerVertical="true" android:layout_height="50dp" /> <TextView android:text="第1个item" android:layout_toRightOf="@id/iv_pic1" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_pic2" android:src="@mipmap/ic_launcher" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="50dp" android:layout_centerVertical="true" android:layout_height="50dp" /> <TextView android:text="第2个item" android:layout_toRightOf="@id/iv_pic2" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_pic3" android:src="@mipmap/ic_launcher" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="50dp" android:layout_centerVertical="true" android:layout_height="50dp" /> <TextView android:text="第3个item" android:layout_toRightOf="@id/iv_pic3" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_pic4" android:src="@mipmap/ic_launcher" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="50dp" android:layout_centerVertical="true" android:layout_height="50dp" /> <TextView android:text="第4个item" android:layout_toRightOf="@id/iv_pic4" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_pic5" android:src="@mipmap/ic_launcher" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="50dp" android:layout_centerVertical="true" android:layout_height="50dp" /> <TextView android:text="第5个item" android:layout_toRightOf="@id/iv_pic5" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout></LinearLayout></RelativeLayout>values/arrr.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="SlideMenu" > <attr name="rightPadding" format="dimension"></attr> </declare-styleable></resources>
样式1 onCreate未做处理 1-4都有下面这句话
@OnClick(R.id.bt_toogle) public void onClick() { viewSlideMenu.toggle(); }
2 的话onCreate 中多了
viewSlideMenu.setIsDrawe(true);
3 的话
viewSlideMenu.setIsDrawe(true); viewSlideMenu.setSpeciallyGoodEffect(true);
4 的话
viewSlideMenu.setIsDrawe(false); viewSlideMenu.setSpeciallyGoodEffect(true);
需要的jar包 放在libry目录下点击下载需要2币
阅读全文
0 0
- 自定义SlideMenu
- 自定义slideMenu
- 自定义slideMenu
- 自定义控件之SlideMenu
- 自定义SlideMenu侧滑菜单
- 简单的自定义View-仿SlideMenu
- Android学习笔记:(一)添加自定义组件(slideMenu)
- 由浅入深学习自定义控件(4)-滑动开关slidemenu
- 由浅入深学习自定义控件(6)- 超简捷的slidemenu实现
- Android SlideMenu
- 侧滑栏: SlideMenu
- 彷QQ5.0侧滑菜单(自定义控件--SlideMenu的实现)
- Java Script的SlideMenu
- af.slidemenu.js
- SlideMenu屏蔽滑动问题
- 1.ViewGroup实现slideMenu
- Android开源--SlideMenu
- layout版的slidemenu
- 使用异步消息机制在子线程中更新UI
- Java+Selenium3方法篇23-iframe切换的问题
- 给20岁的自己说点话
- python-pandas-Series和DataFrame数据结构构建
- C++中结构体与类的区别(struct与class的区别)
- 自定义slideMenu
- jQuery ajax 传数据到后台,后台返回时success函数不执行问题
- 数组指针与指针数组
- MySQL使用数据处理函数和汇总数据.md
- git 配置
- 双向链表_双环
- Hello Kotlin(二)环境搭建
- C++的队列和pair
- C++ this指针