添加了拖动功能,按住小图标,拖动超过一半的时候打开,没到一半的时候关闭。只添加了OnGestureListener接口和OnTouchListener。
具体代码看下面:
具体代码看下面:
- package com.dl.test;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewTreeObserver;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout.LayoutParams;
- public class App extends Activity implements OnPanelStatusChangedListener,OnTouchListener,GestureDetector.OnGestureListener{
- private boolean hasMeasured=false;
- private LinearLayout layout_left;
- private LinearLayout layout_right;
- private ImageView iv;
- private int layout_left_width,layout_right_width=0;
- /**每次自动展开/收缩的范围*/
- private int MAX_WIDTH=0;
- /**每次自动展开/收缩的速度*/
- private final static int SPEED=20;
- private GestureDetector mGestureDetector;
- private boolean isScrolling=false;
- private float mScrollX;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- layout_left=(LinearLayout)findViewById(R.id.layout_left);
- layout_right=(LinearLayout)findViewById(R.id.layout_right);
- iv=(ImageView)findViewById(R.id.iv);
- iv.setOnTouchListener(this);
- //定义手势识别
- mGestureDetector = new GestureDetector(this,this);
- mGestureDetector.setIsLongpressEnabled(false);
- calculatorWidth();
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- }
- @Override
- public void onAttachedToWindow() {
- // TODO Auto-generated method stub
- super.onAttachedToWindow();
- }
- private void calculatorWidth(){
- ViewTreeObserver observer = layout_right.getViewTreeObserver();
- //为了取得控件的宽
- // observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
- // @Override
- // public void onGlobalLayout() {
- // if (hasMeasured == false){
- // layout_right_width = layout_right.getMeasuredWidth();//105
- // layout_left_width=layout_left.getMeasuredWidth();//480
- // MAX_WIDTH=layout_left_width-layout_right_width;//375
- // hasMeasured = true;
- // }
- // }
- // });
- //为了取得控件的宽
- observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
- public boolean onPreDraw(){
- if (hasMeasured == false){
- layout_right_width = layout_right.getMeasuredWidth();
- layout_left_width=layout_left.getMeasuredWidth();
- MAX_WIDTH=layout_left_width-layout_right_width;
- // Log.i("tag", "MAX_WIDTH=="+MAX_WIDTH);
- hasMeasured = true;
- //设置可拉动容器的宽为全屏(即不可拉动容器)的宽
- View layout_max_width=findViewById(R.id.layout_max_width);
- LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)layout_max_width.getLayoutParams();
- lp.width=layout_left_width;
- layout_max_width.setLayoutParams(lp);
- }
- return true;
- }
- });
- }
- class AsynMove extends AsyncTask<Integer, Integer, Void> {
- @Override
- protected Void doInBackground(Integer... params) {
- int times;
- 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);
- try {
- Thread.sleep(Math.abs(params[0]));
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Integer... params) {
- LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();
- if (params[0] < 0)
- lp.leftMargin = Math.max(lp.leftMargin + params[0], 0);
- else
- lp.leftMargin = Math.min(lp.leftMargin + params[0], MAX_WIDTH);
- if(lp.leftMargin<=0){//展开之后
- onPanelOpened();//调用OPEN回调函数
- }
- else if(lp.leftMargin>=MAX_WIDTH){//收缩之后
- onPanelClosed();//调用CLOSE回调函数
- }
- layout_right.setLayoutParams(lp);
- }
- }
- @Override
- public void onPanelOpened() {
- // TODO Auto-generated method stub
- // Log.i("tag", "=========onPanelOpened========");
- }
- @Override
- public void onPanelClosed() {
- // TODO Auto-generated method stub
- // Log.i("tag", "=========onPanelClosed========");
- }
- @Override
- public boolean onDown(MotionEvent e) {
- // TODO Auto-generated method stub
- mScrollX=0;
- isScrolling=false;
- return true;//需要返回ture才能触发onSingleTapUp
- }
- @Override
- public void onShowPress(MotionEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- // TODO Auto-generated method stub
- LayoutParams lp = (android.widget.RelativeLayout.LayoutParams)layout_right.getLayoutParams();
- if (lp.leftMargin >=MAX_WIDTH)// CLOSE的状态
- new AsynMove().execute(new Integer[] { -SPEED });// 负数展开
- else if (lp.leftMargin >= 0)// OPEN的状态
- new AsynMove().execute(new Integer[] { SPEED });// 正数收缩
- return true;
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // TODO Auto-generated method stub
- isScrolling=true;
- mScrollX+=distanceX;//distanceX:向左为正,右为负
- LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();
- lp.leftMargin=lp.leftMargin-(int)mScrollX;
- if(lp.leftMargin<=0){//展开之后
- isScrolling=false;//拖过头了不需要再执行AsynMove了
- lp.leftMargin=0;
- onPanelOpened();//调用OPEN回调函数
- }
- if(lp.leftMargin>=MAX_WIDTH){//收缩之后
- isScrolling=false;
- lp.leftMargin=MAX_WIDTH;
- onPanelClosed();//调用CLOSE回调函数
- }
- layout_right.setLayoutParams(lp);
- return false;
- }
- @Override
- public void onLongPress(MotionEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- //拖到一半放开手指时的运动
- if(event.getAction()==MotionEvent.ACTION_UP && isScrolling==true){
- LayoutParams lp=(LayoutParams) layout_right.getLayoutParams();
- if (lp.leftMargin >= (MAX_WIDTH>>1)) {//往左没超过一半
- new AsynMove().execute(new Integer[] { SPEED });
- }
- else{
- new AsynMove().execute(new Integer[] { -SPEED });
- }
- }
- return mGestureDetector.onTouchEvent(event);
- }
- }