Android的:如何拖动(移动)PopupWindow? [英] Android: How to drag(move) PopupWindow?

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

问题描述

我希望能够移动PopupWindow触摸拖动。 我不希望用户界面更新在触摸释放。我想PopupWindow按照我联系。

I want to be able to move PopupWindow on touch dragging. I don't want UI to update on the release of the touch. I want PopupWindow to follow my touch.

这是一些我在做什么:

mView = mLayoutInflater.inflate(R.layout.popup,
                null);
mPopupWindow = new PopupWindow(mView,
               LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);
mPopupWindow.showAtLocation(parentView, Gravity.CENTER, -5, 30);

mView.setOnTouchListener(new OnTouchListener() {
        private int dx = 0;
        private int dy = 0;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    dx = (int) motionEvent.getX();
                    dy = (int) motionEvent.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int x = (int) motionEvent.getX();
                    int y = (int) motionEvent.getY();
                    int left =  (x - dx);
                    int top =   (y - dy);
                    Log.d("test", "x: " + left + " y: " + top);
                    mPopupWindow.update(left, top, -1, -1);
                    break;
            }
            return true;
        }
    });

会发生什么事,我拖了弹出式窗口,它闪烁来回在原来的位置,并在我的手指。

What happens is, as I drag the popup window, it flickers back and forth on the original location and where my finger is.

闪烁logcat的结果:

Flickering Logcat Result:

x: -44 y: 4
x: -43 y: 37
x: -46 y: 4
x: -46 y: 38
x: -48 y: 4
x: -47 y: 38
x: -50 y: 4

但是,如果我删除(注释掉)mPopupWindow.update(左,上,-1,-1);,它返回正确的结果。 (但显然UI将不会更新):

But if I remove (comment out) "mPopupWindow.update(left, top, -1, -1);", it returns correct result. (But obviously UI won't update):

x: -33 y: 0
x: -37 y: 0
x: -41 y: 0
x: -43 y: 3
x: -46 y: 3
x: -50 y: 3
x: -54 y: 4
x: -57 y: 4

我将如何正确地移动PopupWindow?

How would I move a PopupWindow correctly?

推荐答案

在Eclipse中,它说:信息getX(INT)的第一个指针指数。我假定这应该是只用于设置第一个x和y ACTION.DOWN时。当使用ACTION.MOVE,getRawX()和getRawY(),蚀表示:在屏幕上的事件的原始位置。使用这种方式,弹出窗口将不会闪烁和移动,直到它被解雇后,将保持在它的位置。

In eclipse, when looking at the info on getX(), it says: getX(int) for the first pointer index . I am assuming that this should be the only used when setting the first x and y for ACTION.DOWN. When using ACTION.MOVE, getRawX() and getRawY(), eclipse says: original location of the event on the screen. Using it this way, popup window will not flicker and will remain at its location after moving until it's dismissed.

更新code:

case MotionEvent.ACTION_DOWN:
    dx = (int) event.getX();
    dy = (int) event.getY();
    break;

case MotionEvent.ACTION_MOVE:
    xp = (int) event.getRawX();
    yp = (int) event.getRawY();
    sides = (xp - dx);
    topBot = (yp - dy);
    popup.update(sides, topBot, -1, -1, true);
    break;

这篇关于Android的:如何拖动(移动)PopupWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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