Android仿旧版酷狗滑动侧滑栏的效果
龙旋
共 11900字,需浏览 24分钟
· 2021-04-27
1、缓慢滑动手指离开屏幕时判断左右滑
2、快速滑动时判断屏幕左右滑
3、菜单页打开时,点击内容页关闭菜单并且拦截触摸事件
我们先来看看效果图:
public class SlidingMenu extends HorizontalScrollView {
private View mMenuView,mContentView;
private int mMenuWidth;
private Context mContext;
private GestureDetector mDetector;//系统自带手势处理类
private boolean mMenuIsOpen = false;//菜单页是否已经打开
private boolean mIntercept = false;//是否拦截事件
public SlidingMenu(Context context) {
this(context,null);
}
public SlidingMenu(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
float rightMargin = array.getDimension(R.styleable.SlidingMenu_rightMargin,dip2px(50));
mMenuWidth = (int) (getScreenWidth(context) - rightMargin);
array.recycle();
//用于处理快速滑动时,进行打开或者关闭菜单页
mDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//快速滑动时就会回调,打开菜单页时往右滑动,关闭的时候往左快速滑动
//快速往左滑velocityX是负数,快速往右滑velocityX是正数
if (mMenuIsOpen){
if (velocityX<0){
close();
return true;
}
}else {
if (velocityX>0){
open();
return true;
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
protected void onFinishInflate() {
//xml布局文件解析完毕后调用
super.onFinishInflate();
//1、内容页指定宽高,就是屏幕的宽度
//2、菜单页指定宽度,就是屏幕宽度减去 - 自定义宽度
ViewGroup container = (ViewGroup) getChildAt(0);//外层LinearLayout
if (container.getChildCount() != 2){
throw new RuntimeException("只能且必须放置两个子View!");
}
mMenuView = container.getChildAt(0);//菜单页
//设置宽高
ViewGroup.LayoutParams menuParams = mMenuView.getLayoutParams();
menuParams.width = mMenuWidth;
mMenuView.setLayoutParams(menuParams);//7.0以下的手机必须加这句
mContentView = container.getChildAt(1);//内容页
//设置宽高
ViewGroup.LayoutParams contentParams = mContentView.getLayoutParams();
contentParams.width = getScreenWidth(mContext);
mContentView.setLayoutParams(contentParams);//7.0以下的手机必须加这句
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
mIntercept = false;
//处理当菜单页打开的时候,触摸内容页部门时关闭菜单页,并且拦截事件
if (mMenuIsOpen){
float currentX = ev.getX();
if (currentX > mMenuWidth){
close();
//返回true :拦截子View的事件,但是会执行自己的onTouchEvent方法
mIntercept = true;
return true;
}
}
return super.onInterceptTouchEvent(ev);
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//要在onLayout执行后再调用scrollTo,否则无效果
//初始化的时候是关闭的,注意,此时的getScrollX = mMenuWidth
close();
}
public boolean onTouchEvent(MotionEvent ev) {
if (mIntercept){//如果拦截子View的事件,同时不执行自己的onTouchEvent
return true;
}
if (mDetector.onTouchEvent(ev)){//假如快速滑动执行了,以下代码就不执行
return true;
}
//根据手指抬起时的滚动距离判断,要么关闭,要么打开
// Log.e("111","getScrollX="+getScrollX());
if (ev.getAction() == MotionEvent.ACTION_UP){
int currentScrollX = getScrollX();
if (currentScrollX > mMenuWidth/2){
//关闭菜单
close();
}else {
//打开菜单
open();
}
return true;
}
return super.onTouchEvent(ev);
}
//关闭菜单
private void close() {
mMenuIsOpen = false;
smoothScrollTo(mMenuWidth,0);
}
//打开菜单
private void open() {
mMenuIsOpen = true;
smoothScrollTo(0,0);
}
//处理左右滑时的缩放
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float scale = 1f*l/mMenuWidth;//scale从 1 - 0
//右边的缩放:最小是0.7f,最大是1f
float rightScale = (float) (0.7f + 0.3*scale);
//设置缩放的原点和大小
ViewCompat.setPivotX(mContentView,0);
ViewCompat.setPivotY(mContentView,getMeasuredHeight()/2);
ViewCompat.setScaleX(mContentView,rightScale);
ViewCompat.setScaleY(mContentView,rightScale);
//左边的缩放:最小是0.7f,最大是1f; 透明度:最小是0f,最大是1f
float leftScale = (float) (1 - 0.3*scale);
float alphaScale = (float) (1 - 1*scale);
//设置缩放的原点和大小和透明度
// ViewCompat.setPivotX(mMenuView,mMenuWidth);
// ViewCompat.setPivotY(mMenuView,getMeasuredHeight()/2);
ViewCompat.setScaleX(mMenuView,leftScale);
ViewCompat.setScaleY(mMenuView,leftScale);
ViewCompat.setAlpha(mMenuView,alphaScale);
//平移,抽屉效果
ViewCompat.setTranslationX(mMenuView,l*0.3f);
}
//获取屏幕宽度
private static int getScreenWidth(Context context){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
private int dip2px(float dp){
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dp * scale +0.5f);
}
}
<util.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
app:rightMargin="62dp"
tools:context=".KuGouActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include layout="@layout/layout_home_menu"/>
<include layout="@layout/layout_home_content"/>
</LinearLayout>
</util.SlidingMenu>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/default_avatar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#F57F17"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:text="张三"/>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="皮肤中心"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="消息中心"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="会员中心"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="定时关闭"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="听歌识曲"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="彩铃模式"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:padding="5dp"
android:text="私人云盘"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:gravity="center"
android:padding="5dp"
android:text="设置"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:gravity="center"
android:padding="5dp"
android:text="登录"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#F57F17"
android:gravity="center"
android:padding="5dp"
android:text="关闭"/>
</LinearLayout>
</RelativeLayout>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25sp"
android:text="我是内容"
android:textColor="@color/colorPrimary"
android:gravity="center"/>
</android.support.constraint.ConstraintLayout>
<resources>
<declare-styleable name="SlidingMenu">
<attr name="rightMargin" format="dimension"/>
</declare-styleable>
</resources>
public class KuGouActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ku_gou);
}
}
评论
真高!比亚迪员工爆料比亚迪在越南的薪资水平:基本工资480万,全勤奖35万,交通补助20万,餐补110万,每周6天,每天10小时
上一篇:某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...对此,你怎么看?--完--PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。全文完,感谢你的耐心阅读。如果你还想看到我的文章,请一定给本
开发者全社区
0
太敢穿了!透视纱裙!性感火辣的身材
绝了呀今天的厂花:吴宣仪1995年1月26日,吴宣仪出生于海南省海口市,中国内地流行乐女歌手、影视演员。2016年2月,吴宣仪随宇宙少女发行首张迷你专辑正式出道。2018年4月,她参加《创造101》综艺选秀,获得第二名,成功加入火箭少女101组合。吴宣仪的颜值一直备受称赞,她的五官立体精致,皮肤白皙
逆锋起笔
0
某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...
上一篇:字节的跳动职级与薪资(2024年)我们与公司间的合作,宛如两艘船只在茫茫大海上相互依靠,共同抵御风浪,携手驶向成功的彼岸。然而,当航向开始产生分歧,或是波涛汹涌的风浪改变了我们的初衷,我们或许应当冷静地选择和平分手,而非在风雨中硬撑。最近,一位网友的遭遇引起了广大职场人的关注和热议。这位网友
开发者全社区
0
金融研究 | 使用Python测量关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
我看阿里的年终奖总算发了!
到4月底了,这两天看朋友圈,发现阿里的年终奖终于发了,问了问老同学,也从网上检索了不少信息,基本搞清楚了阿里今年的年终奖情况。近来来阿里一些集团对绩效等级做了较大的调整,以前的旧绩效系统中,绩效分为3.25、3.5、3.75、4和5五个等级,其中4和5是较高绩效等级,较少见。而且之前3.5绩效内部划
公子龙
0
CVPR 2024|大视觉模型的开山之作!无需任何语言数据即可打造大视觉模型
↑ 点击蓝字 关注极市平台作者丨科技猛兽编辑丨极市平台极市导读 本文提出一种序列建模 (sequential modeling) 的方法,不使用任何语言数据,训练大视觉模型。>>加入极市CV技术交流群,走在计算机视觉的最前沿本文目录1 序列建模打造大视觉模型(来自 U
极市平台
1
金融研究(更新) | 使用Python构建关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
字节的跳动职级与薪资(2024年)
上一篇:阿里公布年终奖,P7, 3.5+,22W年终奖,还有35W长期现金激励,真香字节跳动自2012年3月成立以来,已经迅速成长为一个全球性的科技公司。其产品和服务已经遍布全球150多个国家与地区,并且支持超过75种不同的语言。在字节跳动的官方网站上,列出了一系列引人注目的产品和服务,包括但不限于
开发者全社区
0