如何使用Android上看法正确的拖动 [英] how to use correct dragging of a view on android

查看:95
本文介绍了如何使用Android上看法正确的拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有很多如何使用意见拖动机器人解决方案。

it seems that there are a lot of solutions of how to use dragging of views on android .

,他们要么太慢或马车。

however, they are either too slow or buggy.

一个已知的解决方案是有一个的FrameLayout,视图简单地改变其的LayoutParams移动时使用OnTouchListener,如下所述: <一href="http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop-of-normal-views-not-bitmaps-t13404.html" rel="nofollow">http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop-of-normal-views-not-bitmaps-t13404.html 然而,如果视图是ImageView的,达到了最右侧(或最下部)侧的FrameLayout的时,图像收缩。

a known solution is to have a framelayout , and the view simply changes its layoutParams as it moves using the OnTouchListener ,as described here: http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop-of-normal-views-not-bitmaps-t13404.html however, if the view is an imageView , when reaching the most right (or most bottom) side of the framelayout , the image shrinks .

另一种可能的解决方案是使用在画布为了显示拖动的内容,此处描述: http://www.anddev.org/basic_drag_and_drop-t3095.html 不过,我不知道如何使用它的复杂的看法。

another possible solution is to use the canvas in order to show the dragged content , as described here: http://www.anddev.org/basic_drag_and_drop-t3095.html however, i'm not sure how to use it for complex views.

没有人知道一个很好的解决办法,让拖视图的大小保持不变,无论它是在它的容器?

does anyone know of a good solution , one that let the size of the dragged view stay the same , no matter where it is in its container?

请帮助我。

推荐答案

下面是一个简单的ImageView拖着一个完整的解决方案:

Here's a complete solution for a simple imageView dragging:

findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener()
  {
    int prevX,prevY;

    @Override
    public boolean onTouch(final View v,final MotionEvent event)
      {
      final FrameLayout.LayoutParams par=(FrameLayout.LayoutParams)v.getLayoutParams();
      switch(event.getAction())
        {
        case MotionEvent.ACTION_MOVE:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          prevY=(int)event.getRawY();
          par.leftMargin+=(int)event.getRawX()-prevX;
          prevX=(int)event.getRawX();
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_UP:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          par.leftMargin+=(int)event.getRawX()-prevX;
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_DOWN:
          {
          prevX=(int)event.getRawX();
          prevY=(int)event.getRawY();
          par.bottomMargin=-2*v.getHeight();
          par.rightMargin=-2*v.getWidth();
          v.setLayoutParams(par);
          return true;
          }
        }
      return false;
      }
  });

这篇关于如何使用Android上看法正确的拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆