在这里实现了两个滑动菜单效果,的拖放内容的第一部分,菜单拖出像这样的效果感觉,另一种是拖动内容。后面的内容固定菜单。我感觉有层次感的效果,如下面

第一种效果的代码实现例如以下:

package com.tenghu.customsideslip.menu.view;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout; /**
* Created by Arvin_Li on 2014/11/19.
*/
public class ScrollSideslipMenu extends LinearLayout{ private static final int SNAP_VELOCITY=200;//速度阈值
private View mMenu;//菜单布局
private View mContent;//内容布局 private int screenWidth;//屏幕宽度
private int menuWidth;//菜单宽度 private int leftEdge;//左边界
private int rightEdge=0;//右边界。值恒为0 private float xUp;//手指抬起时记录横坐标
private float xDown;//手指按下时记录横坐标
private float xMove;//手指移动时记录横坐标 private int toRightPaddingWidth=50;//菜单全然显示时,留给内容的宽度
private LayoutParams menuParams;//菜单布局參数 private boolean once=false;//初始化数据仅仅载入一次
private boolean isShowMenu;//是否显示菜单 private VelocityTracker velocityTracker;//速度跟踪器 public ScrollSideslipMenu(Context context) {
super(context);
initWindowWidth(context);
} public ScrollSideslipMenu(Context context, AttributeSet attrs) {
super(context, attrs);
initWindowWidth(context);
} /**
* 初始化获取屏幕宽度
*/
private void initWindowWidth(Context context){
WindowManager windowManager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics=new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
//获取屏幕宽度
screenWidth=displayMetrics.widthPixels;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(!once){
mMenu=this.getChildAt(0);//获取菜单布局
mContent=this.getChildAt(1);//获取内容布局
menuParams= (LayoutParams) mMenu.getLayoutParams();//获取菜单布局參数
menuWidth=menuParams.width=screenWidth-toRightPaddingWidth;//设置菜单宽度
mContent.getLayoutParams().width=screenWidth;//设置内容宽度
leftEdge=-menuWidth;//左边界
menuParams.leftMargin=leftEdge;//默认菜单不显示
once=true;
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
createVelocityTracker(event);
switch (event.getAction()){
//按下
case MotionEvent.ACTION_DOWN:
xDown=event.getRawX();//记录按下时的横坐标
break;
//移动
case MotionEvent.ACTION_MOVE:
//记录移动时的横坐标
xMove=event.getRawX();
//计算移动时与按下时的距离
int moveDistanceX= (int) (xMove-xDown);
if(isShowMenu){
menuParams.leftMargin=moveDistanceX;
}else{
menuParams.leftMargin=leftEdge+moveDistanceX;
} if(menuParams.leftMargin<leftEdge){
menuParams.leftMargin=leftEdge;
} if(menuParams.leftMargin>rightEdge){
menuParams.leftMargin=rightEdge;
} mMenu.setLayoutParams(menuParams);//设置參数
break;
//抬起
case MotionEvent.ACTION_UP:
//记录抬起时的横坐标
xUp=event.getRawX();
//计算抬起时与按下时的距离
int upDistanceX= (int) (xUp-xDown);
if(upDistanceX>0&&!isShowMenu){
if(upDistanceX>menuWidth/2||getScrollVelocity()>SNAP_VELOCITY){
scrollToMenu(); }else{
scrollToContent();
}
}else if(upDistanceX<0&&isShowMenu){
if(Math.abs(upDistanceX)>menuWidth/2||getScrollVelocity()>SNAP_VELOCITY){
scrollToContent();
}else{
scrollToMenu();
}
}
mMenu.setLayoutParams(menuParams);
break;
}
return true;
} /**
* 滚动内容部分
*/
private void scrollToContent(){
new ScrollTask().execute(-30);
} /**
* 滚动菜单部分
*/
private void scrollToMenu(){
new ScrollTask().execute(30);
} /**
* 创建速度阈值
*/
private void createVelocityTracker(MotionEvent event){
if(null==velocityTracker){
velocityTracker=VelocityTracker.obtain();
}
velocityTracker.addMovement(event);
} /**
* 获取滚动时的速度
* @return
*/
private int getScrollVelocity(){
velocityTracker.computeCurrentVelocity(1000);
int velocity= (int) velocityTracker.getXVelocity();//获取横向速度
return Math.abs(velocity);
} /**
* 创建一个异步滚动任务
*/
class ScrollTask extends AsyncTask<Integer,Integer,Integer>{ @Override
protected Integer doInBackground(Integer... params) {
int leftMargin=menuParams.leftMargin;
while(true){
leftMargin=leftMargin+params[0];
if(leftMargin<leftEdge){
leftMargin=leftEdge;
break;
}
if(leftMargin>rightEdge){
leftMargin=rightEdge;
break;
}
publishProgress(leftMargin);
sleep(20);
} if(params[0]>0){
isShowMenu=true;
}else{
isShowMenu=false;
}
return leftMargin; } /**
* 运行结束
* @param integer
*/
@Override
protected void onPostExecute(Integer integer) {
menuParams.leftMargin=integer;
mMenu.setLayoutParams(menuParams);
} /**
* 运行doInBackground中的publishProgress调用该方法
* @param values
*/
@Override
protected void onProgressUpdate(Integer... values) {
menuParams.leftMargin=values[0];
mMenu.setLayoutParams(menuParams);
}
} /**
* 当前线程睡眠多少毫秒
* @param millis
*/
private void sleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?

>
<com.tenghu.customsideslip.menu.view.ScrollSideslipMenu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/bg_01">
<!--引用菜单布局文件-->
<include layout="@layout/left_menu"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_02"></LinearLayout>
</com.tenghu.customsideslip.menu.view.ScrollSideslipMenu>

另外一种效果实现:

package com.tenghu.customsideslip.menu.view;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout; /**
* Created by Arvin_Li on 2014/11/19.
*/
public class CustomSideslipMenu extends RelativeLayout {
private static final int SNAP_VELOCITY=200;//手势滑动的速度
//屏幕宽度
private int mScreenWidth;
//菜单布局
private View mMenu;
//主体内容部分
private View mContent;
//声明菜单宽度
private int menuWidth;
//菜单全然显示时。留给内容部分的宽度
private int toRightPaddingWidth=50;
//主体内容布局參数
private LayoutParams contentParams;
//主体内容滑动到左边缘,由菜单宽度来决定
private int leftEdge;
//菜单显示时。主体内容到右边界,值恒为0
private int rightEdge=0;
private VelocityTracker velocityTracker;//声明速度跟踪器
private float xDown;//记录手指按下的横坐标
private float xUp;//记录手指抬起时的横坐标
private float xMove;//记录手指移动时的横坐标
private boolean once=false;//仅仅运行一次
private boolean isShowMenu=true;//是否显示菜单
public CustomSideslipMenu(Context context) {
super(context);
init(context);
} public CustomSideslipMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
} /**
* 初始化
*/
private void init(Context context){
//获取窗体管理类
WindowManager windowManager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//创建DisplayMetrics
DisplayMetrics displayMetrics=new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
//获取屏幕宽度
mScreenWidth=displayMetrics.widthPixels;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(!once){
//获取菜单布局
mMenu=this.getChildAt(0);
//获取主体内容布局
mContent=this.getChildAt(1);
contentParams= (LayoutParams) mContent.getLayoutParams();//获取主体菜单參数
//菜单宽度
menuWidth=mMenu.getLayoutParams().width=mScreenWidth-toRightPaddingWidth;
//设置主体内容的宽度
mContent.getLayoutParams().width=mScreenWidth;
leftEdge=menuWidth;//设置左边界
once=true;
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
//调用创建速度跟踪器
createVelocityTracker(event);
switch (event.getAction()){
//手指按下
case MotionEvent.ACTION_DOWN:
xDown=event.getRawX();
break;
//手指移动
case MotionEvent.ACTION_MOVE:
xMove=event.getRawX();
//计算移动距离
int distanceX= (int) (xMove-xDown);
if(isShowMenu){
contentParams.leftMargin=distanceX;
contentParams.rightMargin=-distanceX;
}else{
contentParams.leftMargin=leftEdge+distanceX;
} if(contentParams.leftMargin>leftEdge){
contentParams.leftMargin=leftEdge;
contentParams.rightMargin=-leftEdge;
} if(contentParams.leftMargin<rightEdge){
contentParams.leftMargin=rightEdge;
contentParams.rightMargin=rightEdge;
}
mContent.setLayoutParams(contentParams);//測试參数
break;
//手指抬起
case MotionEvent.ACTION_UP:
xUp=event.getRawX();//手指抬起时横坐标
//计算抬起时与按下时的距离
int upDistanceX=(int) (xDown-xUp);
if(upDistanceX>0&&!isShowMenu){
if(upDistanceX>menuWidth/2||getScrollVelocity()>SNAP_VELOCITY){
scrollToMenu();
}else{
scrollToContent();
}
}else if(upDistanceX<0&&isShowMenu){
if(Math.abs(upDistanceX)>menuWidth/2||getScrollVelocity()>SNAP_VELOCITY){
scrollToContent();
}else{
scrollToMenu();
}
//手指抬起时销毁
recycleVelocityTracker();
}
break;
}
return true;
} /**
* 滚动菜单
*/
private void scrollToMenu(){
new ScrollTask().execute(-30);
} /**
* 滚动内容
*/
private void scrollToContent(){
new ScrollTask().execute(30);
} /**
* 获取速度
* @return
*/
private int getScrollVelocity(){
velocityTracker.computeCurrentVelocity(1000);
int velocity= (int) velocityTracker.getXVelocity();
return Math.abs(velocity);
} /**
* 销毁速度跟踪器
*/
private void recycleVelocityTracker(){
if (null != velocityTracker) {
velocityTracker.recycle();
velocityTracker = null;
}
} /**
* 创建速度跟踪器
*/
private void createVelocityTracker(MotionEvent event){
if(null==velocityTracker){
velocityTracker=velocityTracker.obtain();
}
velocityTracker.addMovement(event);
} /**
* 创建异步滚动任务类
*/
class ScrollTask extends AsyncTask<Integer,Integer,Integer>{ @Override
protected Integer doInBackground(Integer... params) {
int leftMargin=contentParams.leftMargin;
while(true){
leftMargin=leftMargin+params[0];
if(leftMargin>leftEdge){
leftMargin=leftEdge;
break;
} if(leftMargin<rightEdge){
leftMargin=rightEdge;
break;
}
if(params[0]<0){
isShowMenu=true;
// leftMargin=rightEdge;
}else{
isShowMenu=false;
}
publishProgress(leftMargin);
sleep(20);
}
return leftMargin;
} @Override
protected void onProgressUpdate(Integer... values) {
contentParams.leftMargin=values[0];
contentParams.rightMargin=-values[0];
mContent.setLayoutParams(contentParams);
} @Override
protected void onPostExecute(Integer integer) {
contentParams.leftMargin=integer;
contentParams.rightMargin=-integer;
mContent.setLayoutParams(contentParams);
}
} /**
* 当前线程睡眠多少毫秒
* @param millis
*/
private void sleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

布局文件:

<com.tenghu.customsideslip.menu.view.CustomSideslipMenu 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:background="@drawable/bg_01"
tools:context=".MainActivity"> <include layout="@layout/left_menu" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_02"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testScrollMenu"
android:text="測试另外一种側滑菜单"/>
</LinearLayout>
</LinearLayout>
</com.tenghu.customsideslip.menu.view.CustomSideslipMenu>

菜单布局文件:

<?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:gravity="center_vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="vertical"> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="10dp"> <ImageView
android:id="@+id/iv_img_01"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/app_01" /> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img_01"
android:gravity="center_vertical"
android:text="第一个Item" />
</RelativeLayout> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="10dp"> <ImageView
android:id="@+id/iv_img_02"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/app_02" /> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img_02"
android:gravity="center_vertical"
android:text="第二个Item" />
</RelativeLayout> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="10dp"> <ImageView
android:id="@+id/iv_img_03"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/app_03" /> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img_03"
android:gravity="center_vertical"
android:text="第三个Item" />
</RelativeLayout> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="10dp"> <ImageView
android:id="@+id/iv_img_04"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/app_04" /> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img_04"
android:gravity="center_vertical"
android:text="第四个Item" />
</RelativeLayout> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="10dp"> <ImageView
android:id="@+id/iv_img_05"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/app_05" /> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img_05"
android:gravity="center_vertical"
android:text="第五个Item" />
</RelativeLayout> </LinearLayout>
</RelativeLayout>

这里的菜单,能够是用ListView来布局,做測试就没有那样做了。所有代码所有在这里了。能够看出。两种效果仅仅是继承了不通的布局,感觉另外一种效果在设置背景时有点问题,就是在这里

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_02"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testScrollMenu"
android:text="測试另外一种側滑菜单"/>
</LinearLayout>
</LinearLayout>

假设将背景设置到第一层的LinearLayout上,那么自己定义側滑菜单那里设置背景就显示不出来,设置到第二层就能够,我不知道是怎么回事肿。假设我们掌握发现,麻烦告诉小弟。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

它们的定义android滑动菜单的更多相关文章

  1. Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744400 之前我向大家介绍了史上最简单的滑动菜单的实现方式,相信大家都还记得.如 ...

  2. Android 滑动菜单SlidingMenu

    首先我们看下面视图: 这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对GestureDetect ...

  3. Android 滑动菜单框架--SwipeMenuListView框架完全解析

    SwipeMenuListView(滑动菜单) A swipe menu for ListView.--一个非常好的滑动菜单开源项目. Demo 一.简介 看了挺长时间的自定义View和事件分发,想找 ...

  4. android ——滑动菜单

    一.DrawerLayout是一个拥有两个子控件的布局,第一个子控件是主屏幕中显示的内容,第二个子控件是滑动菜单中显示的内容: <android.support.v4.widget.Drawer ...

  5. android 滑动菜单SlidingMenu的实现

    首先我们看下面视图:       这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对Gesture ...

  6. Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现

    http://blog.csdn.net/guolin_blog/article/details/8714621 http://blog.csdn.net/lmj623565791/article/d ...

  7. Android滑动菜单使用(MenuDrawer和SlidingMenu)

    项目地址: https://github.com/gokhanakkurt/android-menudrawer   https://github.com/jfeinstein10/SlidingMe ...

  8. Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9671609 记得在很早之前,我写了一篇关于Android滑动菜单的文章,其中有一个 ...

  9. Android 学习笔记之AndBase框架学习(七) SlidingMenu滑动菜单的实现

    PS:努力的往前飞..再累也无所谓.. 学习内容: 1.使用SlidingMenu实现滑动菜单..   SlidingMenu滑动菜单..滑动菜单在绝大多数app中也是存在的..非常的实用..Gith ...

随机推荐

  1. asp.net webapi 多文件上传

    使用enctype="multipart/form-data"来进行操作 /// <summary> /// 上传图片 /// </summary> /// ...

  2. 移动端 Retina屏 各大主流网站1px的解决方案

    Retina屏的移动设备如何实现真正1px的线? 在retina屏下面,如果你写了这样的meta <meta name="viewport" content="in ...

  3. Android 在非主线程无法操作UI意识

    Android在应用显示Dialog是一个非常easy事儿,但我从来没有尝试过Service里面展示Dialog. 经验UI操作要在主线程,本地的服务Service是主线程里没错,可是远程servic ...

  4. Oracle SQL操作计划基线总结(SQL Plan Baseline)

    一.基础概念 Oracle 11g開始,提供了一种新的固定运行计划的方法,即SQL plan baseline,中文名SQL运行计划基线(简称基线),能够觉得是OUTLINE(大纲)或者SQL PRO ...

  5. php用魔术方法__call实现类函数重载

    因为php是弱类型语言,不喜欢c++通过改变函数返回相同的值键入的参数的数目和功能将过载!但在需求函数的实际发展可能过载.开发需求,我们能够通过魔术方法__call()来实现函数重载. class T ...

  6. Java多线程总结之由synchronized说开去(转)

    这几天不断添加新内容,给个大概的提纲吧,方面朋友们阅读,各部分是用分割线隔开了的: synchronized与wait()/notify() JMM与synchronized ThreadLocal与 ...

  7. Visual Prolog 的 Web 专家系统 (6)

    保存用户响应询价.作为进一步推理的条件 或GOAL段开始.最初的一句是write_startform() write_startform():- write("<form action ...

  8. 对java中classloader使用的一点理解(转)

    先简单介绍下java的classloader,网上资料很多,就说点关键的. Java 中的类加载器大致可以分成两类,一类是系统提供的,另外一类则是由 Java 应用开发人员编写的.系统提供的类加载器主 ...

  9. 有一定基础的 C++ 学习者该怎样学习 Windows 编程?

    人的心理有个奇异的特性:一项知识一旦学会之后,学习过程中面临的困惑和不解非常快就会忘得干干净净,似乎一切都是自然而然,本来就该这种.因此,关于「怎样入门」这类问题,找顶尖高手来回答,未必能比一个刚入门 ...

  10. Chromium Graphics: GPUclient的原理和实现分析之间的同步机制-Part I

    摘要:Chromium于GPU多个流程架构的同意GPUclient这将是这次访问的同时GPU维修,和GPUclient这之间可能存在数据依赖性.因此必须提供一个同步机制,以确保GPU订购业务.本文讨论 ...