发布于 2017-03-14 18:47:27 | 53 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android TouchListener实现拖拽删实例代码的相关资料,需要的朋友可以参考下

Android TouchListener实现拖拽删实例代码

如果为一个控件设置了该触摸监听, 控件会随着用户的拖动而移动, 如果拖动的距离大过设置的临界值, 那么当松开手指时会有回调onDragComplete, 用户可在该方法中将该控件从父布局中删除, 或这进行其他操作。 如果用户拖拽的距离小于临界值, 那么当用户松开手指时控件会回谈到原来的初始位置。这时会触发onDragRebound回调。 如果用户触摸控件之后没有拖拽而是直接松开手指, 会触发onClick回调, 这样用户就不用为该控件设置onClick监听。

源码如下:



import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
 
/** 
 * Created by zhangjg on 14-10-10. 
 */ 
public class DragTouchListener implements View.OnTouchListener { 
 
  /** 
   * drag directions 
   */ 
  public static final int DIRECTION_UP = 0; 
  public static final int DIRECTION_DOWN = 1; 
  public static final int DIRECTION_LEFT = 2; 
  public static final int DIRECTION_RIGHT = 3; 
 
 
  private int mDragDirection = -1; 
  private int mDragDistance = -1; 
  private ViewGroup.MarginLayoutParams mParams; 
  private ViewGroup.MarginLayoutParams mOriginParams; 
 
 
  private int viewOriginMargin = -1000; 
 
  private float mStartY = 0; 
  private float mStartX = 0; 
  private boolean isTouched = false; 
 
  public DragTouchListener(int dragDirection, int dragDistance){ 
    mDragDirection = dragDirection; 
    mDragDistance = dragDistance; 
  } 
 
  protected void onClick(View view){ 
 
  } 
 
  protected void onDragComplete(View view){ 
 
  } 
 
  protected void onDragRebound(View view){ 
 
  } 
 
  @Override 
  public boolean onTouch(View view, MotionEvent motionEvent) { 
 
 
    if (viewOriginMargin == -1000){ 
      mParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams(); 
      if (mDragDirection == DIRECTION_UP) { 
        viewOriginMargin = mParams.bottomMargin; 
      }else if (mDragDirection == DIRECTION_DOWN){ 
        viewOriginMargin = mParams.topMargin; 
      }else if (mDragDirection == DIRECTION_LEFT){ 
        viewOriginMargin = mParams.rightMargin; 
      }else if (mDragDirection == DIRECTION_RIGHT){ 
        viewOriginMargin = mParams.leftMargin; 
      } 
    } 
 
 
    int action = motionEvent.getAction(); 
    switch (action){ 
      case MotionEvent.ACTION_DOWN: 
        isTouched = true; 
        mStartY = motionEvent.getY(); 
        mStartX = motionEvent.getX(); 
        return true; 
      case MotionEvent.ACTION_MOVE: 
        float y = motionEvent.getY(); 
        float x = motionEvent.getX(); 
 
        if (mDragDirection == DIRECTION_UP){ 
          if(y < mStartY){ 
            mParams.bottomMargin = viewOriginMargin +(int) (mStartY - y); 
          } 
        }else if (mDragDirection == DIRECTION_DOWN){ 
          if (y > mStartY){ 
            mParams.topMargin = viewOriginMargin + (int) (y - mStartY); 
          } 
        }else if (mDragDirection == DIRECTION_LEFT){ 
          if (x < mStartX){ 
            mParams.rightMargin = viewOriginMargin + (int) (mStartX - x); 
          } 
        }else if (mDragDirection == DIRECTION_RIGHT){ 
          if (x > mStartX){ 
            mParams.leftMargin = viewOriginMargin + (int) (x - mStartX); 
          } 
        } 
 
        view.setLayoutParams(mParams); 
        break; 
      case MotionEvent.ACTION_UP: 
        float nowY = motionEvent.getY(); 
        float nowX = motionEvent.getX(); 
 
        int deltaX = (int)nowX - (int)mStartX; 
        int deltaY = (int)nowY - (int)mStartY; 
 
        if (isTouched && Math.abs(deltaX) < 5 && Math.abs(deltaY) < 5){ 
          onClick(view); 
          break; 
        } 
 
        if (mDragDirection == DIRECTION_UP){ 
          if (isTouched && mStartY - nowY > mDragDistance){ 
 
//            Log.i("test-drag", "direction up , startY = " + mStartY + ", nowY = " + nowY + 
//                ", startY - nowY = " + (mStartY - nowY) + ", dragDistance : " + mDragDistance); 
            onDragComplete(view); 
 
          }else if (mStartY - nowY > 0 && mStartY - nowY < mDragDistance ){ 
            mParams.bottomMargin = viewOriginMargin; 
            view.setLayoutParams(mParams); 
            onDragRebound(view); 
          } 
        }else if (mDragDirection == DIRECTION_DOWN){ 
 
          if (isTouched && nowY - mStartY > mDragDistance){ 
            onDragComplete(view); 
          }else if ( nowY - mStartY > 0 && nowY - mStartY < mDragDistance ){ 
            mParams.topMargin = viewOriginMargin; 
            view.setLayoutParams(mParams); 
            onDragRebound(view); 
          } 
        }else if (mDragDirection == DIRECTION_LEFT){ 
          if (isTouched && mStartX - nowX > mDragDistance){ 
            onDragComplete(view); 
          }else if ( mStartX - nowX > 0 && mStartX - nowX < mDragDistance ){ 
            mParams.rightMargin = viewOriginMargin; 
            view.setLayoutParams(mParams); 
            onDragRebound(view); 
          } 
        }else if (mDragDirection == DIRECTION_RIGHT){ 
          if (isTouched && nowX - mStartX > mDragDistance){ 
            onDragComplete(view); 
          }else if ( nowX - mStartX > 0 && nowX - mStartX < mDragDistance ){ 
            mParams.leftMargin = viewOriginMargin; 
            view.setLayoutParams(mParams); 
            onDragRebound(view); 
          } 
        } 
 
        isTouched = false; 
 
        break; 
    } 
    return false; 
  } 
} 

在使用时继承该类, 并覆盖三个回调方法, 就可以在合适的时机得到回调。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



相关阅读 :
Android TouchListener实现拖拽删实例代码
Android 轻松实现图片倒影效果实例代码
Android MediaPlayer实现音乐播放器实例代码
Android图片实现压缩处理的实例代码
Android中实现图文并茂的按钮实例代码
Android recyclerview实现拖拽排序和侧滑删除
android 大图片拖拽并缩放实现原理
Android程序开发之Fragment实现底部导航栏实例代码
Android使用RecycleView实现拖拽交换item位置
Android自定义view实现拖拽选择按钮
Android 实现扫雷小游戏实例代码
Android通过Webservice操作sqlserver数据库实例代码
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务