Android自定义控件简单实现侧滑菜单效果
作者:lmj623565791 发布时间:2023-10-30 16:56:57
侧滑菜单在很多应用中都会见到,最近QQ5.0侧滑还玩了点花样~~对于侧滑菜单,一般大家都会自定义ViewGroup,然后隐藏菜单栏,当手指滑动时,通过Scroller或者不断的改变leftMargin等实现;多少都有点复杂,完成以后还需要对滑动冲突等进行处理~~今天给大家带来一个简单的实现,史上最简单有点夸张,但是的确是我目前遇到过的最简单的一种实现~~~
1、原理分析
既然是侧滑,无非就是在巴掌大的屏幕,塞入大概两巴掌大的布局,需要滑动可以出现另一个,既然这样,大家为啥不考虑使用Android提供的HorizontalScrollView呢~
如果使用HorizontalScrollView,还需要在ACTION_DOWN , ACTION_MOVE里面去监听,判断,不断改变控件位置了么? NO!!!HorizontalScrollView本身就带了滑动的功能~~
还需要自己的手动处理各种冲突么?NO!!!当然了,还是需要了解下事件分发机制的~~~
2、效果图
嗯,主界面搞了QQ一张图片,左边盗用了一兄弟的布局文件~~罪过~~ 谁有好看的布局、图片、图标神马的,可以给我发点,感激~
3、布局文件
<com.example.zhy_slidingmenu.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<include layout="@layout/layout_menu" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/qq" >
</LinearLayout>
</LinearLayout>
</com.example.zhy_slidingmenu.SlidingMenu>
首先是我们的自定义View,里面一个方向水平的LinearLayout,然后就是一个是菜单的布局,一个是主布局了~
4、自定义SlidingMenu
接下来就是我们最核心的代码了~
package com.example.zhy_slidingmenu;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import com.zhy.utils.ScreenUtils;
public class SlidingMenu extends HorizontalScrollView
{
/**
* 屏幕宽度
*/
private int mScreenWidth;
/**
* dp
*/
private int mMenuRightPadding = 50;
/**
* 菜单的宽度
*/
private int mMenuWidth;
private int mHalfMenuWidth;
private boolean once;
public SlidingMenu(Context context, AttributeSet attrs)
{
super(context, attrs);
mScreenWidth = ScreenUtils.getScreenWidth(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
/**
* 显示的设置一个宽度
*/
if (!once)
{
LinearLayout wrapper = (LinearLayout) getChildAt(0);
ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
ViewGroup content = (ViewGroup) wrapper.getChildAt(1);
// dp to px
mMenuRightPadding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, content
.getResources().getDisplayMetrics());
mMenuWidth = mScreenWidth - mMenuRightPadding;
mHalfMenuWidth = mMenuWidth / 2;
menu.getLayoutParams().width = mMenuWidth;
content.getLayoutParams().width = mScreenWidth;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@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);
once = true;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int action = ev.getAction();
switch (action)
{
// Up时,进行判断,如果显示区域大于菜单宽度一半则完全显示,否则隐藏
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
if (scrollX > mHalfMenuWidth)
this.smoothScrollTo(mMenuWidth, 0);
else
this.smoothScrollTo(0, 0);
return true;
}
return super.onTouchEvent(ev);
}
}
哈哈,完工~上面的演示图,就用到这么点代码~~
代码怎么样,短不短~除了设置宽度这些杂七杂八的代码~正在处理滑动的代码不过10行~~我说史上最简单不为过吧~
嗯,由于代码过于短,就不解释了,大家自己看 * 释~
5、扩展
嗯,就下来,我们完善下程序,我准备首先把菜单布局里面改成ListView来证明我们是没有冲突的;然后添加一个属性让用户配置菜单距离右边的边距的值;再对外公布一个方法,点击自动打开菜单,供用户点击某个按钮,菜单慢慢滑出来~
1)、添加自定义属性
a、首先在values文件夹下新建一个attr.xml,写入以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="rightPadding" format="dimension" />
<declare-styleable name="SlidingMenu">
<attr name="rightPadding" />
</declare-styleable>
</resources>
b、在布局中声明命名空间和使用属性
定义完了,肯定要使用么。
<com.example.zhy_slidingmenu.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zhy="http://schemas.android.com/apk/res/com.example.zhy_slidingmenu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none"
zhy:rightPadding="100dp" >
可以看到我们的命名空间:xmlns:zhy="http://schemas.android.com/apk/res/com.example.zhy_slidingmenu" 是http://schemas.android.com/apk/res/加上我们的包名;
我们的属性:zhy:rightPadding="100dp"这里我设置了100dp;
注:很多人问我,没有提示咋办,这样,你clean下项目,如果你运气好,就有提示了,嗯,运气好~
c、在我们自定义类中获得属性
public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mScreenWidth = ScreenUtils.getScreenWidth(context);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.SlidingMenu, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.SlidingMenu_rightPadding:
// 默认50
mMenuRightPadding = a.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50f,
getResources().getDisplayMetrics()));// 默认为10DP
break;
}
}
a.recycle();
}
在三个参数的构造方法中,通过TypeArray获取就行了~
本文出自【张鸿洋的博客】
来源:http://blog.csdn.net/lmj623565791/article/details/39185641
猜你喜欢
- 直接贴上代码,里面都有注释/// <summary> &n
- 注解注解定义Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。Java 语言中的类、方法、变
- 开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary<string,object&
- 1.打开项目主界面,任意打开一个类文件,如MainActivity.java,不要打开布局文件的disign界面2.点击File-->
- SpringMVC配置多个properties文件之通配符在springmvc中配置加载properties文件一般会在xml文件中配置如下
- SpringBoot配置文件的替换使用spring.profiles.active在工作中,测试或上线的时候一定会遇到的问题就是修改配置。一
- SpeSqliteManager4Android改动日志2023.2.14 完成SQLiteOpenHelper 2023.2.23 完成r
- Redis是一个缓存消息中间件及具有丰富特性的键值存储系统。Spring Boot为Jedis客户端库和由Spring Data Redis
- 1.本系统和现在有的考试系统有以下几种优势:a.和现在有的系统比较起来,本系统有科目、章节、老师、学生、班级等信息的管理,还有批阅试卷查看已
- 1. 经过简化的Property 早些时候我们这样声明Property private string _myName; public str
- 1.依赖maven依赖如下,需要说明的是,spring-boot-starter-data-redis里默认是使用lettuce作为redi
- 根据用户系统时区动态展示时间当我们使用SpringBoot+Mysql开发系统时,总是统一设置UTC+8时区,这样用户在任何地区访问系统,展
- 部署到webapps目录启动本文使用的Spring版本为Spring6,SpringBoot版本为3,JDK为17,可能会和之前有细微不同,
- 本文实例讲述了Android实现完整游戏循环的方法。分享给大家供大家参考。具体如下:1. DroidzActivity.java:packa
- 前言今天分享一个类似“孔雀开屏”的动画效果,打开新的页面时,新的页面从屏幕右上角以圆形逐渐打开到全屏。先来看下具体的效果不知道这种效果大家叫
- android开发中为activity增加左右手势识别,如右滑关闭当前页面。/* * for左右手势 *&n
- 方式1:dependency 本地jar包<dependency> <groupId>com.jouyp
- 前言:根据ThreadPoolExecutor的构造方法,JDK提供了很多工厂方法来创建各种用途的线程池.1 newFixedThreadP
- 本篇文章介绍:如何使用Toolbar;自定义Toolbar;先来看一看效果,了解一下toolbar;布局文件:<android.sup
- Spring Boot项目默认的会打包成单一的jar文件,但是有时候我们并不想让配置文件、依赖包都跟可执行文件打包到一起。这时候可以在pom