栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现跟随手指拖动并自动贴边的View样式(实例demo)

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

Android实现跟随手指拖动并自动贴边的View样式(实例demo)

效果图

代码


public class DragView extends ImageView implements View.OnTouchListener{
 private int screenWidth;
 private int screenHeight;
 private Context mContext;
 private int lastX, lastY;
 private int left ,top;
 private ViewGroup.MarginLayoutParams layoutParams;
 private int startX;
 private int endX;
 private boolean isMoved = false;
 private onDragViewClickListener mLister;
 public interface onDragViewClickListener{
  void onDragViewClick();
 }
 public void setonDragViewClickListener(onDragViewClickListener listener){
  this.mLister = listener;
 }
 public DragView(Context context) {
  this(context,null);
 }
 public DragView(Context context, AttributeSet attrs) {
  super(context, attrs);
  mContext = context;
  DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
  screenWidth = displayMetrics.widthPixels;
  screenHeight = displayMetrics.heightPixels-getStatusBarHeight();
  init();
 }
 public void init(){
  setonTouchListener(this);
  post(new Runnable() {
   @Override
   public void run() {
    layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
    layoutParams.topMargin = screenHeight - getHeight();
    layoutParams.leftMargin = screenWidth - getWidth();
    setLayoutParams(layoutParams);
   }
  });
 }
 @Override
 public boolean onTouch(View v, MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    lastX = (int) event.getRawX();
    lastY = (int) event.getRawY();
    startX = lastX;
    break;
   case MotionEvent.ACTION_MOVE:
    isMoved = true;
    int dx = (int) event.getRawX() - lastX;
    int dy = (int) event.getRawY() - lastY;
    left = v.getLeft() + dx;
    top = v.getTop() + dy;
    int right = v.getRight() + dx;
    int bottom = v.getBottom() + dy;
    // 设置不能出界
    if (left < 0) {
     left = 0;
     right = left + v.getWidth();
    }
    if (right > screenWidth) {
     right = screenWidth;
     left = right - v.getWidth();
    }
    if (top < 0) {
     top = 0;
     bottom = top + v.getHeight();
    }
    if (bottom > screenHeight) {
     bottom = screenHeight;
     top = bottom - v.getHeight();
    }
    v.layout(left, top, right, bottom );
    lastX = (int) event.getRawX();
    lastY = (int) event.getRawY();
    break;
   case MotionEvent.ACTION_UP:
    //只有滑动改变上边距时,抬起才进行设置
    if (isMoved) {
     layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
     layoutParams.topMargin = top;
     setLayoutParams(layoutParams);
    }
    endX = (int) event.getRawX();
    //滑动距离比较小,当作点击事件处理
    if (Math.abs(startX - endX) < 6) {
     return false;
    }
    if (left +v.getWidth()/2 < screenWidth/2) {
     startScroll(left,screenWidth/2,true);
    } else {
     startScroll(left,screenWidth/2,false);
    }
    break;
  }
  return true;
}
 //在此处理点击事件
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  mLister.onDragViewClick();
  return super.onTouchEvent(event);
 }
 public void startScroll(final int start, int end, final boolean isLeft){
  ValueAnimator valueAnimator = ValueAnimator.ofFloat(start,end).setDuration(800);
  valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator animation) {
    if (isLeft) {
     layoutParams.leftMargin = (int) (start*(1-animation.getAnimatedFraction()));
    } else {
     layoutParams.leftMargin = (int) (start + (screenWidth - start - getWidth())*(animation.getAnimatedFraction()));
    }
    setLayoutParams(layoutParams);
   }
  });
  valueAnimator.start();
 }
 
 public int getStatusBarHeight() {
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
   result = getResources().getDimensionPixelSize(resourceId);
  }
  return result;
 }
}

以上所述是小编给大家介绍的Android实现跟随手指拖动并自动贴边的View样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160182.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号