右侧MENU划屏代码
参考http://my.eoe.cn/1188496/archive/20296.html
MainActivity
package com.nico; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnPreDrawListener; import android.view.Window; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; public class MainActivity extends Activity implements GestureDetector.OnGestureListener, OnTouchListener { public LinearLayout leftview = null; public LinearLayout rightview = null; public ImageView setBtn = null; public GestureDetector gestureD = null; public int window_width; public int max_width; public boolean isScroll = false; public int mScrollX = 0; public ImageView img = null; final public static int SPEED = 30; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @SuppressWarnings("deprecation") public void initView() { leftview = (LinearLayout) findViewById(R.id.left_part); rightview = (LinearLayout) findViewById(R.id.right_part); setBtn = (ImageView) findViewById(R.id.set_btn); img = (ImageView) findViewById(R.id.bg); gestureD = new GestureDetector(this); leftview.setOnTouchListener(this); img.setOnTouchListener(this); gestureD.setIsLongpressEnabled(false); getmaxWidth(); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //拖动过程中 isScroll = true; //distanceX是处理后点X跟前点X的距离,此时mScrollX为X方向移动距离 mScrollX += distanceX; RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) leftview .getLayoutParams(); int leftmargin = params.leftMargin; //左边距 - X方向移动的距离 等于新的左边距 leftmargin -= mScrollX; //到头了,停止移动,设定左边距为-max_width 或 0 if (leftmargin <= -max_width) { leftmargin = -max_width; params.leftMargin = leftmargin; isScroll = false; } else if (leftmargin >= 0) { leftmargin = 0; params.leftMargin = leftmargin; isScroll = false; } //其他范围内,正常设定左边距 params.leftMargin = leftmargin; leftview.setLayoutParams(params); //返回false,继续传递这个事件 return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { //单击的时候实现左右移动 isScroll = false; RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) leftview .getLayoutParams(); int leftmargin = params.leftMargin; if (leftmargin == 0) { new MoveAsyTask(leftview, max_width).execute(-SPEED); } else { new MoveAsyTask(leftview, max_width).execute(SPEED); } return false; } @Override public boolean onDown(MotionEvent e) { //按下第一个动作时,mScrollX置为0,isScroll重置为false mScrollX = 0; isScroll = false; return true; } public boolean hasMeasured = false; void getmaxWidth() { ViewTreeObserver viewTreeObserver = leftview.getViewTreeObserver(); // 获取控件宽度 viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() { @Override public boolean onPreDraw() { if (!hasMeasured) { window_width = getWindowManager().getDefaultDisplay() .getWidth(); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) leftview .getLayoutParams(); layoutParams.width = window_width; leftview.setLayoutParams(layoutParams); max_width = rightview.getWidth(); hasMeasured = true; } return true; } }); } @Override public boolean onTouch(View v, MotionEvent event) { if (MotionEvent.ACTION_UP == event.getAction() && isScroll == true) { RelativeLayout.LayoutParams params = (LayoutParams) leftview .getLayoutParams(); int leftmargin = params.leftMargin; //滑动结束后,判断拖动距离是否大于一般屏宽,是则向前,否则缩回去 if (leftmargin < -window_width / 2) { new MoveAsyTask(leftview, max_width).execute(-SPEED); } else { new MoveAsyTask(leftview, max_width).execute(SPEED); } } return gestureD.onTouchEvent(event); } }
划屏任务
package com.nico; import android.os.AsyncTask; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class MoveAsyTask extends AsyncTask<Integer, Integer, String> { public LinearLayout view; public int max_width; public MoveAsyTask(LinearLayout v, int mw) { view = v; max_width = mw; } @Override protected void onProgressUpdate(Integer... values) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view .getLayoutParams(); //更新视图 int leftmargin = params.leftMargin; int guide = values[0]; if (guide > 0) { // 往右 leftmargin = Math.min(leftmargin + guide, 0); } else { leftmargin = Math.max(leftmargin + guide, -max_width); // 往左 } params.leftMargin = leftmargin; view.setLayoutParams(params); } @Override protected String doInBackground(Integer... params) { int times = 0; if (max_width % Math.abs(params[0]) == 0)// 整除 times = max_width / Math.abs(params[0]); else times = max_width / Math.abs(params[0]) + 1;// 有余数 for (int i = 0; i < times; i++) { publishProgress(params[0]); try { Thread.sleep(Math.abs(params[0])); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } }
XML文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/right_part" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="50dp" android:background="@android:color/background_dark" android:orientation="vertical" > <LinearLayout android:id="@+id/right_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/left_part" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/left_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/nav_bg" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="我" android:textColor="@android:color/white" android:textSize="20sp" /> <ImageView android:id="@+id/set_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/set_btn" /> </RelativeLayout> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/bg" android:background="@drawable/guide_bg" /> </LinearLayout> </RelativeLayout>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28