自定义HorizontalScrollView实现侧滑菜单

下图是工程目录结构:

MainActivity代码如下:

package com.example.tommy_qq_50;

import com.example.tommy_qq_50.view.SlidingMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity {

	private SlidingMenu menu = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		menu = (SlidingMenu) findViewById(R.id.menu);
	}

	public void toggleMenu(View v){
		menu.toggle();
	}

}

通过自定义类SlidingMenu继承自HorizontalScrollView,可以比较方便的实现侧滑的效果。而不是通过自定义ViewGroup来实现,因为这样需要考虑的TouchEvent会更多。

这里有对setTranslationX()函数的详解: http://blog.csdn.net/yanzi1225627/article/details/47850471

以下是自定义HorizontalScrollView的代码。

package com.example.tommy_qq_50.view;

import com.example.tommy_qq_50.R;
import com.nineoldandroids.view.ViewHelper;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SlidingMenu extends HorizontalScrollView {

	private  LinearLayout mWapper;
	private ViewGroup mMenu;
	private ViewGroup mContent;

	private int  mScreenWidth ;

	private int mMenuWidth;

	//单位dp,菜单与
	private int mMenuRightPadding = 50;

	private boolean once = false;

	private boolean isOpen = false;

	/**
	 * 当使用了自定义属性时,会调用此构造方法
	 * @param context
	 * @param attrs
	 * @param defStyleAttr
	 */
	public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		//获取我们定义的属性
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.SlidingMenu,defStyleAttr,0);

		int n =  a.getIndexCount();
		for(int i =0;i<n;i++){
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.SlidingMenu_rightPadding:
				mMenuRightPadding = a.getDimensionPixelSize(attr, (int) TypedValue.
						applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
						context.getResources().getDisplayMetrics()));

				break;

			default:
				break;
			}
		}

		a.recycle();
		WindowManager wm= (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		mScreenWidth = outMetrics.widthPixels;

	}
	/**
	 * 未使用自定义属性时,会调用此构造方法
	 * @param context
	 * @param attrs
	 */
	public SlidingMenu(Context context, AttributeSet attrs) {
		this(context, attrs,0);

	}
	public SlidingMenu(Context context) {
		this(context,null);
	}

	/**
	 * 设置子View的宽和高
	 * 设置自己的宽和高
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		if(once == false){
			mWapper = (LinearLayout) getChildAt(0);
			mMenu =  (ViewGroup) mWapper.getChildAt(0);
			mContent =(ViewGroup) mWapper.getChildAt(1);

			mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
			mContent.getLayoutParams().width = mScreenWidth;

			once = true;
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	//设置子View放置的位置
	//通过设置偏移量,将Menu隐藏
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		if(changed){
			this.scrollTo(mMenuWidth, 0);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {

		int action = ev.getAction();

		switch (action) {
		case MotionEvent.ACTION_UP://用户手指抬起时
			//隐藏在左边的宽度
			int scrollX = (int) 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 ;
		else{
			this.smoothScrollTo(0, 0);
			isOpen = true;
		}
	}
	/**
	 * 关闭菜单
	 */
	public void closeMenu(){
		if(!isOpen){
			return ;
		}else {
			this.smoothScrollTo(mMenuWidth, 0);
			isOpen = false;
		}
	}
	/**
	 * 切换菜单
	 */
	public void toggle(){
		if(isOpen){
			closeMenu();
		}else{
			openMenu();
		}
	}

	@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

		/**
		 * 	区别一:内容区域从1.0-0.7 缩放的效果
			scale:1.0 - 0.0
			0.7 + 0.3 * scale

			区别二:菜单的偏移量要修改
			区别三:菜单的显示时有缩放,以及透明度变化。
			缩放:0.7 - 1.0
			1.0 - scale * 0.3
			透明度:0.6 - 1.0
			0.6 + 0.4 * (1 - scale);
		 */

		float rightScale = (0.7f + 0.3f * scale);
		float leftScale = 1.0f - scale * 0.3f;
		float leftAlpha = 0.6f + 0.4f * (1 - scale);

		//调用属性动画,设置TranslationX
		ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f);
		ViewHelper.setScaleX(mMenu, leftScale);
		ViewHelper.setScaleY(mMenu, leftScale);
		ViewHelper.setAlpha(mMenu, leftAlpha);

		//设置mContent缩放的中心点
		ViewHelper.setPivotX(mContent, 0);
		ViewHelper.setPivotY(mContent, mContent.getHeight()/2);
		ViewHelper.setScaleX(mContent,  rightScale);
		ViewHelper.setScaleY(mContent, rightScale);

	}

}

工程中一共有两个XML布局文件

1、activity_main.xml

2、left_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:tommy="http://schemas.android.com/apk/res/com.example.tommy_qq_50"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img_frame_background"
    >
	<com.example.tommy_qq_50.view.SlidingMenu
	    android:id="@+id/menu"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    tommy:rightPadding="50dp"
	    >
	    <LinearLayout
	        android:layout_width="wrap_content"
	        android:layout_height="match_parent"
	        android:orientation="horizontal"
	        >
	        <include
	            layout="@layout/left_menu"
	            />
	        <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="match_parent"
	            android:background="@drawable/qq"
	            >
	            <Button
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:onClick="toggleMenu"
	                android:text="切换菜单"
	                />
	        </LinearLayout>
	    </LinearLayout>
	</com.example.tommy_qq_50.view.SlidingMenu>

</RelativeLayout>
<?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"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        >
		<RelativeLayout
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    >
		    <ImageView
		        android:id="@+id/id_img1"
		        android:layout_width="50dp"
		        android:layout_height="50dp"
		      	android:layout_marginLeft="20dp"
		      	android:layout_marginTop="20dp"
		      	android:src="@drawable/img_1"
		      	android:layout_centerVertical="true"
		        />
		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:textSize="20sp"
		        android:textColor="#ffffff"
		        android:text="第一个Item"
		        android:layout_toRightOf="@id/id_img1"
		        android:layout_marginLeft="20dp"
		        android:layout_centerVertical="true"

		        />
		</RelativeLayout>
		<RelativeLayout
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    >
		    <ImageView
		        android:id="@+id/id_img2"
		        android:layout_width="50dp"
		        android:layout_height="50dp"
		      	android:layout_marginLeft="20dp"
		      	android:layout_marginTop="20dp"
		      	android:src="@drawable/img_2"
		      	android:layout_centerVertical="true"
		        />
		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:textSize="20sp"
		        android:textColor="#ffffff"
		        android:text="第二个Item"
		        android:layout_toRightOf="@id/id_img2"
		        android:layout_marginLeft="20dp"
		        android:layout_centerVertical="true"

		        />
		</RelativeLayout>
		<RelativeLayout
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    >
		    <ImageView
		        android:id="@+id/id_img3"
		        android:layout_width="50dp"
		        android:layout_height="50dp"
		      	android:layout_marginLeft="20dp"
		      	android:layout_marginTop="20dp"
		      	android:src="@drawable/img_3"
		      	android:layout_centerVertical="true"
		        />
		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:textSize="20sp"
		        android:textColor="#ffffff"
		        android:text="第三个Item"
		        android:layout_toRightOf="@id/id_img3"
		        android:layout_marginLeft="20dp"
		        android:layout_centerVertical="true"

		        />
		</RelativeLayout>
		<RelativeLayout
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    >
		    <ImageView
		        android:id="@+id/id_img4"
		        android:layout_width="50dp"
		        android:layout_height="50dp"
		      	android:layout_marginLeft="20dp"
		      	android:layout_marginTop="20dp"
		      	android:src="@drawable/img_4"
		      	android:layout_centerVertical="true"
		        />
		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:textSize="20sp"
		        android:textColor="#ffffff"
		        android:text="第四个Item"
		        android:layout_toRightOf="@id/id_img4"
		        android:layout_marginLeft="20dp"
		        android:layout_centerVertical="true"

		        />
		</RelativeLayout>
		<RelativeLayout
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    >
		    <ImageView
		        android:id="@+id/id_img5"
		        android:layout_width="50dp"
		        android:layout_height="50dp"
		      	android:layout_marginLeft="20dp"
		      	android:layout_marginTop="20dp"
		      	android:src="@drawable/img_5"
		      	android:layout_centerVertical="true"
		        />
		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:textSize="20sp"
		        android:textColor="#ffffff"
		        android:text="第五个Item"
		        android:layout_toRightOf="@id/id_img5"
		        android:layout_marginLeft="20dp"
		        android:layout_centerVertical="true"

		        />
		</RelativeLayout>
    </LinearLayout>

</RelativeLayout>

源码下载

时间: 2024-08-29 23:45:12

自定义HorizontalScrollView实现侧滑菜单的相关文章

自定义SwipeLayout实现侧滑菜单

请尊重个人劳动成果,转载注明出处,谢谢! http://blog.csdn.net/amazing7/article/details/51768942 先看 SwipeLayout的效果图 图太多了,我这只上传一张,想看 listview和GridView效果的,和想看源码的 -> GitHub 怎么实现后面说,先说会废话. 最近整理以前的项目,那时有一个这样的需求,要在ExpandableListView的ChildView上实现 编辑和删除的侧滑菜单.我当时并没有用别人的框架,实现出来大概是

使用HorizontalScrollView实现侧滑菜单

主要继承 HorizontalScrollView   类 ,在构造方法中设置 菜单的宽,  重写 onMeasure,  onLayout 方法 ,在 onLayout 中设置初始显示到 内容页的 scrollTo 1.SlidingMenuView 类的实现如下, package com.example.slidingmenu; import android.content.Context; import android.util.AttributeSet; import android.u

android:自定义HorizontalScrollView实现qq侧滑菜单

今天看了鸿洋_大神在慕课网讲的qq5.0侧滑菜单.学了不少的知识,同时也佩服鸿洋_大神思路的清晰. 看了教程课下也自己实现了一下.代码几乎完全相同  别喷我啊..没办法 o(︶︿︶)o 唉 像素不好 没办法 找不到好的制作gif的软件. 我们暂且称侧滑左边界面的为menu,右边为content 首先是menu的布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:androi

Android自定义View之仿QQ侧滑菜单实现

最近,由于正在做的一个应用中要用到侧滑菜单,所以通过查资料看视频,学习了一下自定义View,实现一个类似于QQ的侧滑菜单,顺便还将其封装为自定义组件,可以实现类似QQ的侧滑菜单和抽屉式侧滑菜单两种菜单. 下面先放上效果图: 我们这里的侧滑菜单主要是利用HorizontalScrollView来实现的,基本的思路是,一个布局中左边是菜单布局,右边是内容布局,默认情况下,菜单布局隐藏,内容布局显示,当我们向右侧滑,就会将菜单拉出来,而将内容布局的一部分隐藏,如下图所示: 下面我们就一步步开始实现一个

安卓开发笔记——自定义HorizontalScrollView控件(实现QQ5.0侧滑效果)

对于滑动菜单栏SlidingMenu,大家应该都不陌生,在市场上的一些APP应用里经常可以见到,比如人人网,FaceBook等. 前段时间QQ5.0版本出来后也采用了这种设计风格:(下面是效果图) 之前在GitHub上看到过关于此设计风格的开源项目,它只需要引入对应的类库,就可以定制灵活.各种阴影和渐变以及动画的滑动效果的侧滑菜单. 但作为开发人员,在学习阶段还是建议尽可能的去自己实现,所以今天我不讲此开源项目的使用方式,我们用自定义HorizontalScrollView来实现此效果. 下面先

自定义侧滑菜单

类似于QQ侧滑菜单的效果,这个最重要的就是改变摆放方式,从而达到自己想要的效果,首先先弄明白onLayout方法里的参数 这个自定义里最主要的就是通过touch事件来移动显示的范围,移动viewgroup主要有几个方法,onLayout offsetTopAndBottom(offset)和offsetLeftAndRight(offset); scrollTo和scrollBy方法; 注意:滚动的并不是viewgroup内容本身,而是它的矩形边框 它是瞬间移动, 这里我们用的是scrollTo

Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件(转)

一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: 1.对Android中Window类中的DecorView有所了解 2.对Scroller类实现平滑移动效果 3.自定义ViewGroup的实现 首先来看看效果图吧:    下面现在就来说说这里咱们实现侧滑View的基本思路吧,这里我采用的是自定义一个继承于RelativeLayout的控件叫做XC

自定义android侧滑菜单

这里实现两种侧滑菜单效果,第一种拖拽内容部分,菜单像是被拖出来的感觉的这种效果,第二种是拖拽内容部分,菜单在内容后面不动,感觉有一种层次感的效果,如下 第一种效果的代码实现如下: package com.tenghu.customsideslip.menu.view; import android.content.Context; import android.os.AsyncTask; import android.util.AttributeSet; import android.util.

自定义Android侧滑菜单控件

package com.tenghu.sideslipmenu.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 andr