欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-17 16:40:22
效果图 代码 /** * 根据手指拖动的当前位置,自动贴边的view */ public class dragview extends ima...

效果图

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

代码

/**
 * 根据手指拖动的当前位置,自动贴边的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,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();
 }
 /**
  * 获取状态栏的高度
  * @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样式,希望对大家有所帮助