自定义控件之SlideMenu

来源:互联网 发布:自动对焦爬山算法 编辑:程序博客网 时间:2024/05/02 19:29
<span style="font-family: Arial, Helvetica, sans-serif;">package com.example.view;</span>
import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.FrameLayout;public class SlideMenu extends FrameLayout {private View menuView,mainView;private int downX;private int menuWidth;private ScrollAnimation animation;public SlideMenu(Context context, AttributeSet attrs) {super(context, attrs);}public SlideMenu(Context context) {super(context);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {menuView.layout(-menuWidth, 0, 0, bottom);mainView.layout(0, 0, right, bottom);}@Overrideprotected void onFinishInflate() {super.onFinishInflate();menuView=getChildAt(1);mainView=getChildAt(0);menuWidth = menuView.getLayoutParams().width;}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:downX = (int)event.getX();break;case MotionEvent.ACTION_MOVE:int moveX=(int) event.getX();int deltaX=moveX-downX;int newscrollX = getScrollX()-deltaX;if(newscrollX>0){newscrollX=0;}if(newscrollX<-menuWidth){newscrollX=-menuWidth;}scrollTo(newscrollX, 0);break;case MotionEvent.ACTION_UP:int scrollX = getScrollX();if(scrollX<-menuWidth/2){animation = new ScrollAnimation(this, -menuWidth);}else{animation = new ScrollAnimation(this, 0);}startAnimation(animation);break;default:break;}return true;}public void switchMenu() {// TODO Auto-generated method stubScrollAnimation animation;if(getScrollX()!=0){animation=new ScrollAnimation(this, 0);}else{animation=new ScrollAnimation(this, -menuWidth);}startAnimation(animation);}}<img src="http://img.blog.csdn.net/20160209181815120?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20160209181848792?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

0 0
原创粉丝点击