自己实现了一下侧滑的三种方式(注释都写代码里了)

本文Demo下载地址:Andriod侧滑

本文实现所需框架:nineoldandroids下载地址:nineoldandroids

1.普通侧滑:

主要是基于HorizontalScrollView做的:示例代码如下

主要布局:

xmlns:gaoyu="http://schemas.android.com/apk/res/gaoyu.com.myapplication"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_qqsideslip"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="gaoyu.com.myapplication.sideslip.QQSideslipActivity">

android:id="@+id/SlMenu_sideslip"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

gaoyu:rightPadding="100dp">

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/sliding">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="onClick_sideslip_qq"

android:text="切换菜单" />

菜单的布局

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_centerInParent="true">

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/iv_sideslip1"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_marginLeft="20dp"

android:src="@drawable/icon"/>

android:layout_centerVertical="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/iv_sideslip1"

android:layout_marginLeft="20dp"

android:text="第一个item"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/iv_sideslip2"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_marginLeft="20dp"

android:src="@drawable/icon"/>

android:layout_centerVertical="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/iv_sideslip2"

android:layout_marginLeft="20dp"

android:text="第二个item"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/iv_sideslip3"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_marginLeft="20dp"

android:src="@drawable/icon"/>

android:layout_centerVertical="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/iv_sideslip3"

android:layout_marginLeft="20dp"

android:text="第三个item"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/iv_sideslip4"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_marginLeft="20dp"

android:src="@drawable/icon"/>

android:layout_centerVertical="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/iv_sideslip4"

android:layout_marginLeft="20dp"

android:text="第四个item"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/iv_sideslip5"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_marginLeft="20dp"

android:src="@drawable/icon"/>

android:layout_centerVertical="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/iv_sideslip5"

android:layout_marginLeft="20dp"

android:text="第五个item"/>

定义view类

public class SlidingMenu_qq extends HorizontalScrollView {

private LinearLayout mWapper;

private ViewGroup mMenu;

private ViewGroup mContent;

//menu的宽度

private int mMenuWidth;

//屏幕的宽度(内容区的宽度就是屏幕宽度)

private int mScreenWdith;

//菜单与右边的距离50dp

private int mMenuRightPidding = 50;

//调用一次

private boolean once;

//标识状态

private boolean isOPen;

/**

* 未使用自定义属性时调用

* 由于设置了attr所以...

*

* @param context

* @param attrs

*/

public SlidingMenu_qq(Context context, AttributeSet attrs) {

//调用三个参数的构造方法

this(context, attrs, 0);

//获取屏幕宽度(窗口管理器)

/*WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

//展示度量

DisplayMetrics outMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

mScreenWdith = outMetrics.widthPixels;

//把dp转换成px

mMenuRightPidding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());

*/

}

/**

* 当实现自定义属性时会执行三个参数的方法

*

* @param context

* @param attrs

* @param defStyleAttr

*/

public SlidingMenu_qq(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

//获取自定义的属性

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu_qq, defStyleAttr, 0);

//自定义属性个数

int n = a.getIndexCount();

for (int i = 0; i < n; i++) {

int attr = a.getIndex(i);

switch (attr) {

case R.styleable.SlidingMenu_qq_rightPadding:

//设置默认值是50dp

mMenuRightPidding = 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);

mScreenWdith = outMetrics.widthPixels;

}

/**

* new 一个TextView时传一个上下文

*

* @param context

*/

public SlidingMenu_qq(Context context) {

//调用两个参数的构造方法

super(context, null);

}

/**

* 设置HorizontalScrollView子VIew的宽和高

* 设置HorizontalScrollView自己的宽和高

*

* @param widthMeasureSpec

* @param heightMeasureSpec

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//设置循环之调用一次

if (!once) {

//HorizontalScrollView 内部只能有一个元素 所以直接get(0)就行就是那个Linerlayout mWapper

mWapper = (LinearLayout) getChildAt(0);

//获取mWapper里的第一个元素menu

mMenu = (ViewGroup) mWapper.getChildAt(0);

//获取mWapper里的第二个元素content

mContent = (ViewGroup) mWapper.getChildAt(1);

//菜单和内容宽度

mMenuWidth = mMenu.getLayoutParams().width = mScreenWdith - mMenuRightPidding;

mContent.getLayoutParams().width = mScreenWdith;

//由于子对象被设置了,mWapper就先不用了

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) {

//x为正滚动条向右 内容向左(移动mMenuWidth 正好将菜单隐藏)

this.scrollTo(mMenuWidth, 0);

}

}

/**

* 判断将菜单滑出来多少了

*

* @param ev

* @return

*/

@Override

public boolean onTouchEvent(MotionEvent ev) {

int action = ev.getAction();

switch (action) {

case MotionEvent.ACTION_UP:

//隐藏在左边的宽度

int scrollX = getScrollX();

if (scrollX >= mMenuWidth / 2) {

//scrollTo也行但是动画效果不好 (隐藏)

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();

}

}

}

2.抽屉侧滑(添加此方法)

/**

* 实现抽屉滑动

* l隐藏在左边的宽度

* 后边是变化梯度

*/

@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梯度的值

//调用属性动画

ViewHelper.setTranslationX(mMenu,mMenuWidth*scale);

}

3.qq5.0侧滑,实现这个方法

/**

* 实现仿qq5.0

* l等于隐藏在左边的宽度(越来越小)

* 后边是变化梯度

*/

@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 leftScale = 1.0f-scale*0.3f;

//透明度

float leftAlpha = 0.6f + 0.4f*(1-scale);

ViewHelper.setTranslationX(mMenu, mMenuWidth * scale*0.8f);

ViewHelper.setScaleX(mMenu,leftScale);

ViewHelper.setScaleY(mMenu,leftScale);

ViewHelper.setAlpha(mMenu,leftAlpha);

//内容区域不断缩小

float rightScale = 0.7f+0.3f*scale;

//横向纵向缩放(不更改缩放中心点就全隐藏了)

ViewHelper.setPivotX(mContent,0);

ViewHelper.setPivotY(mContent,mContent.getHeight()/2);

ViewHelper.setScaleX(mContent,rightScale);

ViewHelper.setScaleY(mContent,rightScale);

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android中仿qq最新版抽屉,Android实现3种侧滑效果(仿qq侧滑、抽屉侧滑、普通侧滑)...相关推荐

  1. Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

    Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题 这是API ...

  2. android中编译和使用luajit开发应用,Android 嵌入 LuaJIT 的曲折道路

    相关链接:Windows 下编译 LuaJIT 懒人与伸手党可以直接看最底部. 为什么使用 LuaJIT Lua 官方版的编译嵌入相对简单,但是为什么要用 LuaJIT 呢?我所了解到的优势有: 更高 ...

  3. android中调用fft函数,J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data)...

    J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data) 我一直在玩这个游戏已经有一段时间了,我无法弄清楚我在这里要做的事情. ...

  4. Android 中设置线程优先级的正确方式(2种方法)

    Android 中设置线程优先级的正确方式(2种方法) 在 Android 中,有两种常见的设置线程优先级的方式: 第一种,使用 Thread 类实例的 setPriority 方法,来设置线程优先级 ...

  5. Android中mesure过程详解 (结合Android 4.0.4 最新源码)

    如何遍历并绘制View树?之前的文章Android中invalidate() 函数详解(结合Android 4.0.4 最新源码)中提到invalidate()最后会发起一个View树遍历的请求,并通 ...

  6. Android中layout过程详解 (结合Android 4.0.4 最新源码)

    上一篇文章Android中mesure过程详解 (结合Android 4.0.4 最新源码)介绍了View树的measure过程,相对与measure过程,本文介绍的layout过程要简单多了,正如l ...

  7. Android中实现帐号密码登录及进行内存缓存逻辑(仿QQ)

    编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/69219113 Demo效果展示如下: 布局代码如下: //(la ...

  8. Android中显示输入的隐藏密码/Android多语系支持

    1.我们常常会看到我们输入的密码都是以小黑点的形式出现,这在Android中实现是很简单的,只需要设置一个属性即可. 需要设置EditText的inputType属性,设置如下: android:in ...

  9. android中gradle的作用,Gradle 之 Android 中的应用

    在上一篇文章中 Gradle 之语言基础 Groovy 主要介绍了 Groovy 的基础语法(如果没有 Groovy 的基础,建议先看看上篇文章,如果可以动手敲一下里面的示例代码就更好不过了),也是为 ...

  10. android中的 listview控件,聊聊Android中的ListView控件

    软硬件环境 Macbook Pro MGX 72 Android Studio 1.3.2 坚果手机 前言 ListView是Android系统中使用非常广泛的一种控件,几乎所有的App都会用到它.它 ...

最新文章

  1. MySQL之帮助的使用
  2. LINUX下安装ORACLE,完全搞定
  3. Python基础笔记8
  4. 原来医生的处方不是随便乱写的...
  5. envoy api 网关_在边缘,作为网关或在网格中构建控制平面以管理Envoy代理的指南...
  6. mac的safari浏览器调试h5
  7. python计算众数
  8. jdb java_JAVA初学者的JDB 尝试
  9. setuptools find_packages
  10. HTML页面打印功能js代码,JavaScript_js实现页面打印功能实例代码(附去页眉页脚功能代码),复制代码 代码如下: html - phpStudy...
  11. 吴恩达机器学习ex5:正则化线性回归和方差与偏差
  12. 日销10万台的乐视商城如何颠覆传统电商
  13. Java系统日志管理
  14. 基于bert的情感分类
  15. 家用计算机音效部件图示,唱吧新版自定义音效设置方法(附上最佳音效设置参数图)...
  16. JSK-布设光钎-Kruscal最小生成树-并查集-图的连通性
  17. 蚂蚁愚人节视频透露的真相:区块链只有科技巨头才玩得起?
  18. 安装Tensorflow 报错false Not creating XLA devices, tf_xla_enable_xla_devices not set
  19. cpu消耗 pytorch_PyTorch测试模型执行计算耗费的时间
  20. 占豪--2010年的市场机会在哪里(兼谈股指期货与楼市)

热门文章

  1. Karto的前端实现与解读
  2. MSCKF-Based Visual-Wheel Odometry 轮速视觉融合里程计
  3. 形态学处理:膨胀、腐蚀、开运算、闭运算、形态学梯度、顶帽、黑帽
  4. 卷积神经网络系列之卷积/池化后特征图大小怎么计算??
  5. go kegg_零基础 GO 与 KEGG 分析,手把手教你用多种途径实现!
  6. bootstrap_fileinput上传文件 后台接受额外的参数
  7. 欢迎使用人体运动检测与跟踪CSDN-markdown编辑器
  8. PubChem的Python接口PubChemPy
  9. Jupyter notebook运行指定的conda虚拟环境
  10. 第三十四课.模糊神经网络