android关于侧滑菜单slidingMenu
2016-09-20 17:53
302 查看
简单的侧滑的实现:
Mainactivity
package com.example.sudo.slidemenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import view.SlideMenus;
public class MainActivity extends AppCompatActivity {
private SlideMenus mMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMenu = (SlideMenus) findViewById(R.id.menu);
}
public void toggleMenu(View v){
mMenu.toggle();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:Jin="http://schemas.android.com/apk/res-auto"
tools:context="com.example.sudo.slidemenu.MainActivity">
<view.SlideMenus
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu"
Jin:rightPadding="150dp">
<LinearLayout
android:layout_width="match_parent"
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="@mipmap/qq">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="切换菜单"/>
</LinearLayout>
</LinearLayout>
</view.SlideMenus>
</RelativeLayout>
left_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bbbg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:id="@+id/tv_img1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableLeft="@mipmap/img_1"
android:text="第一个Item"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:drawablePadding="20dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableLeft="@mipmap/img_2"
android:text="第二个Item"
android:paddingLeft="20dp"
android:drawablePadding="20dp"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="第三个Item"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:drawablePadding="20dp"
android:drawableLeft="@mipmap/img_3"
android:paddingTop="10dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="20dp"
android:drawableLeft="@mipmap/img_4"
android:text="第四个Item"
android:drawablePadding="20dp"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="第五个Item"
android:drawableLeft="@mipmap/img_5"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:drawablePadding="20dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
activity_sliding_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:Huan="http://schemas.android.com/apk/res-auto"
tools:context="com.example.sudo.slidemenu.SlidingDrawable">
<view.SlideMenus
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/twomenu"
Huan:rightPadding="150dp">
<LinearLayout
android:layout_width="match_parent"
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="@mipmap/qq">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="切换菜单"/>
</LinearLayout>
</LinearLayout>
</view.SlideMenus>
</RelativeLayout>
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="rightPadding" format="dimension">
</attr>
<declare-styleable name="SlideMenus">
<attr name="rightPadding"></attr>
</declare-styleable>
<declare-styleable name="TwoSlideMenu">
<attr name="rightPadding"></attr>
</declare-styleable>
</resources>
SlideMenus
/**
* Created by Sudo on 2016/9/19.
*/
public class SlideMenus extends HorizontalScrollView{
private LinearLayout mWapper;
private ViewGroup mMenu;
//内容区域
private ViewGroup mContent;
//屏幕的宽度
private int mScreenWidth;
//menu与右边的距离单位dp
private int mMenuRightPadding = 10;
private boolean once=false;
//表示当前的状态
private boolean isOpen;
//menu的宽度
private int mMenuWidth;
/*
未使用自定义属性时调用
*/
public SlideMenus(Context context, AttributeSet attrs) {
this(context, attrs,0);
// //获取屏幕的宽度
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//
// DisplayMetrics outMetrics= new DisplayMetrics();
// wm.getDefaultDisplay().getMetrics(outMetrics);
// //widthPixels像素值
// mScreenWidth = outMetrics.widthPixels;
// //将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
/**
*
*当自定义了属性时会调用此方法
*/
public SlideMenus(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenus,defStyleAttr,0);
int n = a.getIndexCount();
for (int i=0;i<n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.SlideMenus_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);
//widthPixels像素值
mScreenWidth = outMetrics.widthPixels;
//将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
public SlideMenus(Context context) {
this(context,null);
}
/*
设置子View的宽和高
设置自己的宽和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//如果为false设置子view的宽和高
if(!once){
mWapper = (LinearLayout) getChildAt(0);
//LinearLayout里的第一个元素
mMenu = (ViewGroup) mWapper.getChildAt(0);
//LinearLayout里的第二个元素
mContent = (ViewGroup) mWapper.getChildAt(1);
//menu的宽度
mMenuWidth= mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding;
//屏幕的宽度
mContent.getLayoutParams().width = mScreenWidth;
//只调用一次
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
*
*通过设置偏移量将Menu隐藏
*/
@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) {
int action = ev.getAction();
//通过action判断用户的行为
switch (action){
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;
}
this.smoothScrollTo(0,0);
isOpen=true;
}
/**
* 关闭菜单
*/
public void closeMenu(){
if(!isOpen){
return;
}
this.smoothScrollTo(mMenuWidth,0);
isOpen=false;
}
/**
* 切换菜单
*/
public void toggle(){
if (isOpen){
closeMenu();
}else {
openMenu();
}
}
}
抽屉式侧滑菜单:
SlidingDrawable
//抽屉式侧滑菜单
public class SlidingDrawable extends AppCompatActivity {
private SlideMenus twomenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_drawable);
twomenu = (SlideMenus) findViewById(R.id.twomenu);
}
public void toggleMenu(View v){
twomenu.toggle();
}
}
TwoSlideMenu
public class TwoSlideMenu extends HorizontalScrollView{
private LinearLayout mWapper;
private ViewGroup mMenu;
//内容区域
private ViewGroup mContent;
//屏幕的宽度
private int mScreenWidth;
//menu与右边的距离单位dp
private int mMenuRightPadding = 10;
private boolean once=false;
//表示当前的状态
private boolean isOpen;
//menu的宽度
private int mMenuWidth;
/*
未使用自定义属性时调用
*/
public TwoSlideMenu(Context context, AttributeSet attrs) {
this(context, attrs,0);
// //获取屏幕的宽度
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//
// DisplayMetrics outMetrics= new DisplayMetrics();
// wm.getDefaultDisplay().getMetrics(outMetrics);
// //widthPixels像素值
// mScreenWidth = outMetrics.widthPixels;
// //将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
/**
*
*当自定义了属性时会调用此方法
*/
public TwoSlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenus,defStyleAttr,0);
int n = a.getIndexCount();
for (int i=0;i<n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.SlideMenus_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);
//widthPixels像素值
mScreenWidth = outMetrics.widthPixels;
//将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
public TwoSlideMenu(Context context) {
this(context,null);
}
/*
设置子View的宽和高
设置自己的宽和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//如果为false设置子view的宽和高
if(!once){
mWapper = (LinearLayout) getChildAt(0);
//LinearLayout里的第一个元素
mMenu = (ViewGroup) mWapper.getChildAt(0);
//LinearLayout里的第二个元素
mContent = (ViewGroup) mWapper.getChildAt(1);
//menu的宽度
mMenuWidth= mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding;
//屏幕的宽度
mContent.getLayoutParams().width = mScreenWidth;
//只调用一次
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
*
*通过设置偏移量将Menu隐藏
*/
@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) {
int action = ev.getAction();
//通过action判断用户的行为
switch (action){
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;
}
this.smoothScrollTo(0,0);
isOpen=true;
}
/**
* 关闭菜单
*/
public void closeMenu(){
if(!isOpen){
return;
}
this.smoothScrollTo(mMenuWidth,0);
isOpen=false;
}
/**
* 切换菜单
*/
public void toggle(){
if (isOpen){
closeMenu();
}else {
openMenu();
}
}
/**
*滚动发生时
* int l:当前scaleX的值
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//scaleX的值1~0
float scale = l*1.0f/mMenuWidth;
//内容区域
float rightScale = 0.7f+0.3f*scale;
//缩放比例
float leftScale = 1.0f-scale*3f;
//透明度
float leftAlpha = 0.6f+0.4f*(1-scale);
//调用属性动画,设置TransationX
ViewHelper.setTranslationX(mMenu,mMenuWidth*scale);
//给菜单设置动画
ViewHelper.setScaleX(mMenu,leftScale);
ViewHelper.setScaleY(mMenu,leftScale);
ViewHelper.setAlpha(mMenu,leftAlpha);
//设置contnt缩放的中心点
ViewHelper.setPivotX(mContent,0);
ViewHelper.setPivotY(mContent,mContent.getHeight()/2);
//内容区域的缩放
ViewHelper.setScaleX(mContent,rightScale);
ViewHelper.setScaleY(mContent,rightScale);
}
}
用框架写的SlidingMenu转自http://blog.csdn.net/lmj623565791/article/details/36677279
Mainactivity
package com.example.sudo.slidemenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import view.SlideMenus;
public class MainActivity extends AppCompatActivity {
private SlideMenus mMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMenu = (SlideMenus) findViewById(R.id.menu);
}
public void toggleMenu(View v){
mMenu.toggle();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:Jin="http://schemas.android.com/apk/res-auto"
tools:context="com.example.sudo.slidemenu.MainActivity">
<view.SlideMenus
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu"
Jin:rightPadding="150dp">
<LinearLayout
android:layout_width="match_parent"
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="@mipmap/qq">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="切换菜单"/>
</LinearLayout>
</LinearLayout>
</view.SlideMenus>
</RelativeLayout>
left_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bbbg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:id="@+id/tv_img1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableLeft="@mipmap/img_1"
android:text="第一个Item"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:drawablePadding="20dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableLeft="@mipmap/img_2"
android:text="第二个Item"
android:paddingLeft="20dp"
android:drawablePadding="20dp"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="第三个Item"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:drawablePadding="20dp"
android:drawableLeft="@mipmap/img_3"
android:paddingTop="10dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="20dp"
android:drawableLeft="@mipmap/img_4"
android:text="第四个Item"
android:drawablePadding="20dp"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_img5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="第五个Item"
android:drawableLeft="@mipmap/img_5"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:drawablePadding="20dp"
android:gravity="center_vertical"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
activity_sliding_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:Huan="http://schemas.android.com/apk/res-auto"
tools:context="com.example.sudo.slidemenu.SlidingDrawable">
<view.SlideMenus
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/twomenu"
Huan:rightPadding="150dp">
<LinearLayout
android:layout_width="match_parent"
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="@mipmap/qq">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="切换菜单"/>
</LinearLayout>
</LinearLayout>
</view.SlideMenus>
</RelativeLayout>
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="rightPadding" format="dimension">
</attr>
<declare-styleable name="SlideMenus">
<attr name="rightPadding"></attr>
</declare-styleable>
<declare-styleable name="TwoSlideMenu">
<attr name="rightPadding"></attr>
</declare-styleable>
</resources>
SlideMenus
/**
* Created by Sudo on 2016/9/19.
*/
public class SlideMenus extends HorizontalScrollView{
private LinearLayout mWapper;
private ViewGroup mMenu;
//内容区域
private ViewGroup mContent;
//屏幕的宽度
private int mScreenWidth;
//menu与右边的距离单位dp
private int mMenuRightPadding = 10;
private boolean once=false;
//表示当前的状态
private boolean isOpen;
//menu的宽度
private int mMenuWidth;
/*
未使用自定义属性时调用
*/
public SlideMenus(Context context, AttributeSet attrs) {
this(context, attrs,0);
// //获取屏幕的宽度
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//
// DisplayMetrics outMetrics= new DisplayMetrics();
// wm.getDefaultDisplay().getMetrics(outMetrics);
// //widthPixels像素值
// mScreenWidth = outMetrics.widthPixels;
// //将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
/**
*
*当自定义了属性时会调用此方法
*/
public SlideMenus(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenus,defStyleAttr,0);
int n = a.getIndexCount();
for (int i=0;i<n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.SlideMenus_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);
//widthPixels像素值
mScreenWidth = outMetrics.widthPixels;
//将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
public SlideMenus(Context context) {
this(context,null);
}
/*
设置子View的宽和高
设置自己的宽和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//如果为false设置子view的宽和高
if(!once){
mWapper = (LinearLayout) getChildAt(0);
//LinearLayout里的第一个元素
mMenu = (ViewGroup) mWapper.getChildAt(0);
//LinearLayout里的第二个元素
mContent = (ViewGroup) mWapper.getChildAt(1);
//menu的宽度
mMenuWidth= mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding;
//屏幕的宽度
mContent.getLayoutParams().width = mScreenWidth;
//只调用一次
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
*
*通过设置偏移量将Menu隐藏
*/
@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) {
int action = ev.getAction();
//通过action判断用户的行为
switch (action){
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;
}
this.smoothScrollTo(0,0);
isOpen=true;
}
/**
* 关闭菜单
*/
public void closeMenu(){
if(!isOpen){
return;
}
this.smoothScrollTo(mMenuWidth,0);
isOpen=false;
}
/**
* 切换菜单
*/
public void toggle(){
if (isOpen){
closeMenu();
}else {
openMenu();
}
}
}
抽屉式侧滑菜单:
SlidingDrawable
//抽屉式侧滑菜单
public class SlidingDrawable extends AppCompatActivity {
private SlideMenus twomenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_drawable);
twomenu = (SlideMenus) findViewById(R.id.twomenu);
}
public void toggleMenu(View v){
twomenu.toggle();
}
}
TwoSlideMenu
public class TwoSlideMenu extends HorizontalScrollView{
private LinearLayout mWapper;
private ViewGroup mMenu;
//内容区域
private ViewGroup mContent;
//屏幕的宽度
private int mScreenWidth;
//menu与右边的距离单位dp
private int mMenuRightPadding = 10;
private boolean once=false;
//表示当前的状态
private boolean isOpen;
//menu的宽度
private int mMenuWidth;
/*
未使用自定义属性时调用
*/
public TwoSlideMenu(Context context, AttributeSet attrs) {
this(context, attrs,0);
// //获取屏幕的宽度
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//
// DisplayMetrics outMetrics= new DisplayMetrics();
// wm.getDefaultDisplay().getMetrics(outMetrics);
// //widthPixels像素值
// mScreenWidth = outMetrics.widthPixels;
// //将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
/**
*
*当自定义了属性时会调用此方法
*/
public TwoSlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenus,defStyleAttr,0);
int n = a.getIndexCount();
for (int i=0;i<n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.SlideMenus_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);
//widthPixels像素值
mScreenWidth = outMetrics.widthPixels;
//将dp转换为px
// mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
}
public TwoSlideMenu(Context context) {
this(context,null);
}
/*
设置子View的宽和高
设置自己的宽和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//如果为false设置子view的宽和高
if(!once){
mWapper = (LinearLayout) getChildAt(0);
//LinearLayout里的第一个元素
mMenu = (ViewGroup) mWapper.getChildAt(0);
//LinearLayout里的第二个元素
mContent = (ViewGroup) mWapper.getChildAt(1);
//menu的宽度
mMenuWidth= mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding;
//屏幕的宽度
mContent.getLayoutParams().width = mScreenWidth;
//只调用一次
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
*
*通过设置偏移量将Menu隐藏
*/
@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) {
int action = ev.getAction();
//通过action判断用户的行为
switch (action){
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;
}
this.smoothScrollTo(0,0);
isOpen=true;
}
/**
* 关闭菜单
*/
public void closeMenu(){
if(!isOpen){
return;
}
this.smoothScrollTo(mMenuWidth,0);
isOpen=false;
}
/**
* 切换菜单
*/
public void toggle(){
if (isOpen){
closeMenu();
}else {
openMenu();
}
}
/**
*滚动发生时
* int l:当前scaleX的值
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//scaleX的值1~0
float scale = l*1.0f/mMenuWidth;
//内容区域
float rightScale = 0.7f+0.3f*scale;
//缩放比例
float leftScale = 1.0f-scale*3f;
//透明度
float leftAlpha = 0.6f+0.4f*(1-scale);
//调用属性动画,设置TransationX
ViewHelper.setTranslationX(mMenu,mMenuWidth*scale);
//给菜单设置动画
ViewHelper.setScaleX(mMenu,leftScale);
ViewHelper.setScaleY(mMenu,leftScale);
ViewHelper.setAlpha(mMenu,leftAlpha);
//设置contnt缩放的中心点
ViewHelper.setPivotX(mContent,0);
ViewHelper.setPivotY(mContent,mContent.getHeight()/2);
//内容区域的缩放
ViewHelper.setScaleX(mContent,rightScale);
ViewHelper.setScaleY(mContent,rightScale);
}
}
用框架写的SlidingMenu转自http://blog.csdn.net/lmj623565791/article/details/36677279
相关文章推荐
- Android开源项目SlidingMenu深切解析 侧滑菜单
- android侧滑菜单SlidingMenu加上手势滑动及首次运行引导页面混合使用
- Android侧滑菜单——SlidingMenu详解使用
- 侧滑菜单 Android开源项目SlidingMenu深切解析
- Android:SlidingMenu(侧滑菜单)用法示例
- android之SlidingMenu简单实现侧滑菜单
- Android官方侧滑菜单DrawerLayout类似slidingMenu
- Android 侧滑菜单的简单实现(SlidingMenu)二
- Android SlidingMenu 侧滑菜单
- android侧滑菜单框架SlidingMenu的使用
- 关于Android DrawerLayout的侧滑菜单的点击事件
- android初学------SlidingMenu(侧滑菜单的使用—)
- Android 自定义SlidingMenu 实现QQ5.0侧滑菜单动画效果
- Android笔记(五十二) 侧滑菜单SlidingMenu
- android侧滑菜单,slidingmenu集成进androidstudio以及使用
- Android经典侧滑菜单SlidingMenu使用介绍
- Android经典侧滑菜单SlidingMenu使用介绍
- Android基础学习之SlidingMenu(侧滑菜单)
- Android第三方类库:slidingmenu 实现侧滑菜单
- Android 侧滑菜单的简单实现(SlidingMenu)