相信有很多人都看过关于QQ5.0的侧滑,以下我先贴出我项目中正在使用的侧滑
public class SlidingMenu extends HorizontalScrollView {
private LinearLayout mWapper;
//菜单的组件
private ViewGroup mMenu;
//内容的组件
private ViewGroup mContent;
private int mMenuWidth;
//屏幕的宽度
private int mScreenWidth;
//菜单和右侧间距,dp
private int mMenuRightPadding=50;
private boolean once=false;
//当期的菜单状态
private boolean isOpen=false;
public SlidingMenu(Context context,AttributeSet attrs) {
this(context,attrs,0);
}
/**
* 设置子View的宽高,和自己的宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(!once){
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);
}
/**
* 通过设置偏移量,设置menu隐藏
*/
@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 );
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action=ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
//隐藏在左边的宽度
int scorllX=getScrollX();
if(scorllX>=mMenuWidth/2){//小于1/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;
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();
}
}
//当自定义属性时调用该方法
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//获取自定义属性
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:
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;
}
public SlidingMenu(Context context) {
this(context,null);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float scale=l*1.0f/mMenuWidth;
//内容的缩放
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.8f);
ViewHelper.setScaleX(mMenu, leftScale);
ViewHelper.setScaleY(mMenu, leftScale);
ViewHelper.setAlpha(mMenu, leftAlpha);
//设置菜单缩放的中心点
ViewHelper.setPivotX(mContent, 0);
ViewHelper.setPivotY(mContent, mContent.getHeight()/2);
ViewHelper.setScaleX(mContent, rightScale);
ViewHelper.setScaleY(mContent, rightScale);
}
}
关于上边的代码其中自己在values中创建了一个attr.xml,是为了实现自定义右边的间距。然后就是布局文件:
<com.example.mytea.view.SlidingMenu
android:id="@+id/id_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
hyman: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:orientation="vertical"
android:background="#FFFFFF">
<include layout="@layout/action_bar"/>"
<android.support.v4.view.ViewPager
android:id="@+id/main_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</com.example.mytea.view.SlidingMenu>
关于这个我觉得我们应该注意两点:
1.给菜单背景时,不应该设置到left_menu中,这样会照成图片一起滑动,并且在上部出现空白区域。所以应该在整个activity_main中设置背景。
2.如果我们使用viewPager,会照成左右滑动冲突,下边给大家一个解决这个问题的网址:http://blog.csdn.net/ameryzhu/article/details/11712599