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

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

概述效果图代码/***根据手指拖动的当前位置,自动贴边的View*/publicclassDragViewextendsImageViewimplementsView.OnTouchListener{

效果图

代码

/** * 根据手指拖动的当前位置,自动贴边的VIEw */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,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(); } /**  * 获取状态栏的高度  * @return 状态栏高度  */ 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样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android实现跟随手指拖动并自动贴边的View样式(实例demo)全部内容,希望文章能够帮你解决Android实现跟随手指拖动并自动贴边的View样式(实例demo)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147233.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存