android pad 侧菜单,android 仿苹果pad左侧右滑退出界面

最近做pad项目,需要加入左侧右滑退出界面的功能。安卓要实现这个功能,我的思路是将这个activity 对话框化。

@android:color/transparent

@null

true

将activity的theme指定成上面的ItemDetail , activity就变成了dialog啦,当然你也可以直接指定为系统的dialog样式。

activity变成了dialog样式, window的大小都变成了wrap_content属性了。所以我们在activity的onCreate中设置window的大小。

WindowManager.LayoutParams params = getWindow().getAttributes() ;

DisplayMetrics dm = getResources().getDisplayMetrics() ;

params.width = dm.widthPixels ;

params.height = dm.heightPixels ;

onWindowAttributesChanged(params);

这样变将window设置成全屏大小啦!

activity的准备工作已经好啦!接着需要监听左侧滑动监听啦!

packagecom.czy.slideback;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.util.TypedValue;importandroid.view.MotionEvent;importandroid.widget.LinearLayout;/*** Created by nahuo16 on 2015/6/18.*/public classTouchLinearLayout extendsLinearLayout {

private final intbarHeight;privateOnTouchListener touchListener;private booleanshouldListener;private intside;private intdownX,downY;private intcanBackSide;publicTouchLinearLayout(Context context) {

this(context, null);}

publicTouchLinearLayout(Context context,AttributeSet attrs) {

this(context,attrs,0);}

publicTouchLinearLayout(Context context,AttributeSet attrs, intdefStyleAttr) {

super(context,attrs,defStyleAttr);side= dip2px(context,80) ;canBackSide= dip2px(context,20) ;TypedValue value = newTypedValue();context.getTheme().resolveAttribute(R.attr.actionBarSize,value, true);barHeight= context.getResources().getDimensionPixelSize(value.resourceId);}

private intdip2px(Context context, intdp) {

return(int) (context.getResources().getDisplayMetrics().density* dp + 0.5f);}

@Overridepublic booleanonInterceptTouchEvent(MotionEvent ev) {

switch(ev.getAction()){

caseMotionEvent.ACTION_DOWN:

downX= (int) ev.getX();downY= (int) ev.getY();shouldListener= downX< side&& downY> barHeight;break ;caseMotionEvent.ACTION_MOVE:

if(shouldListener){

intx = (int) (ev.getX() - downX);if( x> canBackSide&& Math.abs(x / (ev.getY() - downY))>1.1f){

return true ;}

}

break ;caseMotionEvent.ACTION_CANCEL:

caseMotionEvent.ACTION_UP:

break ;}

return false;}

@Overridepublic booleanonTouchEvent(MotionEvent event) {

if(touchListener!=null)

touchListener.onTouch(this ,event) ;return true;}

public voidsetOnTouchListener(OnTouchListener l){

touchListener= l ;}

} 自定义监听左侧滑动监听的线性布局。

将这个线性布局做为根布局。(需要RelativeLayout?将上面的linearLayout换成RelativeLayout即可)。xml布局如下:

在activity的oncreate获取到TouchLinearLayout对象后,将下面的onTouchListener对象传给

TouchLinearLayout.setOnTouchListener。这样就完成了左侧右滑退出当前界面啦!

privateView.OnTouchListener touchListener=newView.OnTouchListener() {

VelocityTracker velocity;@Overridepublic booleanonTouch(finalView v,MotionEvent event) {

getWindow().getDecorView().setX(event.getX());switch(event.getAction()){

caseMotionEvent.ACTION_DOWN:

if(velocity== null){

velocity= VelocityTracker.obtain() ;}

else{

velocity.clear();}

velocity.addMovement(event);break ;caseMotionEvent.ACTION_MOVE:

if(velocity== null){

velocity= VelocityTracker.obtain() ;}

else{

velocity.clear();}

velocity.addMovement(event);velocity.computeCurrentVelocity(1000);break ;caseMotionEvent.ACTION_CANCEL:

caseMotionEvent.ACTION_UP:

final intwidth = v.getWidth() ;intplus = dip2px(v.getContext() ,20) ;intadd = -plus ;intmax = (int) event.getX();if(max>width/3|| (velocity!=null&&velocity.getXVelocity()>2000)){

add = plus ;}

final inta = add ;final intm = max ;newThread(){

public voidrun(){

intrun = m;intadd = a;intaddPlus = add / 10;while(run > 0&& run < width){

try{

Thread.sleep(10) ;} catch(InterruptedException e) {

e.printStackTrace();}

run += add ;add += addPlus ;final intr = run ;v.post(newRunnable() {

@Overridepublic voidrun() {

getWindow().getDecorView().setX(r);}

});}

if(run <= 0){

final intr = 0;v.post(newRunnable() {

@Overridepublic voidrun() {

getWindow().getDecorView().setX(r);}

});}

else{

final intr = width;v.post(newRunnable() {

@Overridepublic voidrun() {

getWindow().getDecorView().setX(r);finish();}

});}

}

}.start();break ;}

return false;}

} ;private intdip2px(Context context, intdp) {

return(int) (context.getResources().getDisplayMetrics().density* dp + 0.5f);}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值