老师求救!为什么我的显示是这个样子的,代码已经写到抽屉式菜单二了!下面是我的代码,烦请老师帮忙看一下。
7 回答
package com.example.slidingmenu.view; import com.example.slidingmenu.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 mSrreenWidth; // 保存屏幕的当前宽度 private int mMenuRightPadding = 50;// 菜单显示后右侧所保留的空余位置的值 private boolean flage; private boolean isOpen; // 标识菜单是否显示 private int mMenuWidth; /** * 但使用了自定义属性时调用此构造 * * @param context * @param attrs * @param defStyle */ public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 获取自定义的属性 TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.slidingMenu, defStyle, 0); int count = ta.getIndexCount(); for (int i = 0; i < count; i++) { int attr = ta.getIndex(i); switch (attr) { case R.styleable.slidingMenu_rightpadding: mMenuRightPadding = ta.getDimensionPixelSize(i, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, context .getResources().getDisplayMetrics())); break; default: break; } } ta.recycle(); WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mSrreenWidth = outMetrics.widthPixels; /* * mMenuRightPadding = (int) TypedValue.applyDimension( * TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources() * .getDisplayMetrics()); */ } public SlidingMenu(Context context) { this(context, null); } /* * 未使用自定义属性时使用此构造 */ public SlidingMenu(Context context, AttributeSet attrs) { this(context, attrs, 0); } // 设置子VIew的宽和高 设置自己的宽和高 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (!flage) { mwapper = (LinearLayout) getChildAt(0); mMenu = (ViewGroup) mwapper.getChildAt(0); mContent = (ViewGroup) mwapper.getChildAt(1); mMenuWidth = mMenu.getLayoutParams().width = mSrreenWidth - mMenuRightPadding; mContent.getLayoutParams().width = mSrreenWidth; flage = 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 ScrollX = 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) { // TODO 自动生成的方法存根 super.onScrollChanged(l, t, oldl, oldt); float scale = l * 1.0F / mMenuWidth; float leftScale = 1.0F - scale * 0.3F; float leftAlpha = 0.6f + 0.4f * (1 - scale); float rightScale = 0.7F + 0.3F * scale; // 设置菜单的隐藏显示动画 ViewHelper.setTranslationX(mMenu, mMenuWidth * scale); 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); } }
举报
0/150
提交
取消