标签:
public void scrollTo(int x, int y)
说明:在当前视图内容偏移至(x , y)坐标处,即显示(可视)区域位于(x , y)坐标处。
public void scrollBy(int x, int y)
说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。显示部分在大的view中也偏移了位置。
onTouchEvent事件类型:
ACTION_DOWN: 表示用户开始触摸.
ACTION_MOVE: 表示用户在移动(手指或者其他)
ACTION_UP:表示用户抬起了手指
ACTION_CANCEL:表示手势被取消了
publicclassSlidingMenuextendsHorizontalScrollView{ privateLinearLayout mWapper; privateViewGroup mMenu; privateViewGroup mContent; privateint mScreenWidth; privateint mMenuWidth; privateint mMenuRightPadding =50;//menu与屏幕右侧的间距 、 privateboolean once =false; publicSlidingMenu(Context context,AttributeSet attrs){ super(context, attrs); WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics =newDisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mScreenWidth = outMetrics.widthPixels; //把dp转换为px mMenuRightPadding =(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50, context.getResources().getDisplayMetrics()); } /* *设置子view的宽和高,和自己的宽和高(子view确定好之后就确定自己的了) */ @Override protectedvoid onMeasure(int widthMeasureSpec,int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, 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; //子view宽度决定之后,本身也就决定了 once =true; } } /* *通过设置偏移量,将menu隐藏 */ @Override protectedvoid 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 publicboolean onTouchEvent(MotionEvent ev){ int action = ev.getAction(); switch(action){ caseMotionEvent.ACTION_UP: int scrollX = getScrollX();//scrollX是隐藏是在左边的宽度 if(scrollX >= mMenuWidth/2){ this.smoothScrollTo(mMenuWidth,0); } else{ this.smoothScrollTo(0,0); } returntrue; } returnsuper.onTouchEvent(ev); } }
<com.example.kevin.myapplication.SlidingMenu android:layout_width="match_parent" android:layout_height="match_parent"> <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"> </LinearLayout> </LinearLayout> </com.example.kevin.myapplication.SlidingMenu>
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="rightPadding" format="dimension"/> <declare-styleable name="SlidingMenu"> <attr name="rightPadding"/> </declare-styleable> </resources>
<com.example.kevin.myapplication.SlidingMenu android:id="@+id/id_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/img_frame_background" my:rightPadding="100dp"> <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.kevin.myapplication.SlidingMenu>
//获取定义的属性 TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SlidingMenu); 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; } }
/* *滚动发生时 */ @Override protectedvoid onScrollChanged(int l,int t,int oldl,int oldt){ super.onScrollChanged(l, t, oldl, oldt); float scale = l*1.0f/mMenuWidth;//1~0 //调用属性动画设置anslationX。 ViewHelper.setTranslationX(mMenu, mMenuWidth * scale); }
protectedvoid onScrollChanged(int l,int t,int oldl,int oldt){ super.onScrollChanged(l, t, oldl, oldt); float scale = l*1.0f/mMenuWidth;//1~0 //调用属性动画设置anslationX。 ViewHelper.setTranslationX(mMenu, mMenuWidth * scale); float rightScale =0.7f+0.3f* scale; float leftScale =1.0f- scale *0.3f; float leftAlpha =0.6f+0.4f*(1- scale); ViewHelper.setScaleX(mMenu, leftScale); ViewHelper.setScaleY(mMenu, leftScale); ViewHelper.setAlpha(mMenu, leftAlpha); // 设置content的缩放的中心点 ViewHelper.setPivotX(mContent,0); ViewHelper.setPivotY(mContent, mContent.getHeight()/2); ViewHelper.setScaleX(mContent, rightScale); ViewHelper.setScaleY(mContent, rightScale); }
QQ 5.0侧滑HorizontalScrollView以及自定义ViewGroup
标签:
原文地址:http://www.cnblogs.com/fruitbolgs/p/4232002.html