首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在手指触摸和弹出移动之间拖动弹出距离

在手指触摸和弹出移动之间拖动弹出距离
EN

Stack Overflow用户
提问于 2015-04-16 11:38:22
回答 1查看 64关注 0票数 0
代码语言:javascript
复制
public class PopupDragUtility implements OnTouchListener {

ReaderLaunchActivity readerAct;
PopupWindow targetWindow;
private float dX = 0;
private float dY = 0;

public PopupDragUtility(ReaderLaunchActivity readerAct, PopupWindow targetWindow) {
    super();
    this.readerAct = readerAct;
    this.targetWindow = targetWindow;
}

@Override
public boolean onTouch(View p_v, MotionEvent p_event) {
    switch (p_event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        dX = p_event.getRawX();
        dY = p_event.getRawY();
        break;
    }
    case MotionEvent.ACTION_UP: {
        break;
    }

    case MotionEvent.ACTION_MOVE: {

        if (readerAct.deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            float newX = p_event.getRawX() - 390;
            float newY = p_event.getRawY();

            System.out.println("newX    " + newX);
            System.out.println("newY   " + newY);

            if (newX > -390 && newY > 75 && (newX + targetWindow.getWidth()) < readerAct.readerLayout.getWidth()
                    && (newY + targetWindow.getHeight()) < readerAct.readerLayout.getHeight()) {

                targetWindow.update((int) newX, (int) newY, -1, -1, true);

            }

        } else {
            float newX = p_event.getRawX() - 140;
            float newY = p_event.getRawY();

            System.out.println("newX    " + newX);
            System.out.println("newY   " + newY);

            if (newX > -140 && newY > 75 && (newX + targetWindow.getWidth()) < readerAct.readerLayout.getWidth()
                    && (newY + targetWindow.getHeight()) < readerAct.readerLayout.getHeight()) {

                targetWindow.update((int) newX, (int) newY, -1, -1, true);

            }
        }

        break;
    }
    }
    return true;
}

我正在尝试在屏幕边界内拖动一个矩形图标。但是我面临着一个问题,那就是我的手指接触

代码语言:javascript
复制
 and the pop up movement.I have temporary resolved this issue with the help      of some constants.But Can anyone suggest a solution without constants.  Here target window is the reference of the popup and reader layout is the
代码语言:javascript
复制
 reference of the entire screen.
EN

回答 1

Stack Overflow用户

发布于 2015-04-16 13:01:38

代码语言:javascript
复制
@Override
public boolean onTouchEvent(MotionEvent event) {
    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    ObjectAnimator animation1, animation2;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "y", touchY);
        animation1.setDuration(2000);
        animation1.start();

        animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "x", touchX);
        animation2.setDuration(2000);
        animation2.start();
        break;
    case MotionEvent.ACTION_MOVE:
        animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "y", touchY);
        animation1.setDuration(2000);
        animation1.start();

        animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "x", touchX);
        animation2.setDuration(2000);
        animation2.start();
        break;
    case MotionEvent.ACTION_UP:
        animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "y", touchY);
        animation1.setDuration(2000);
        animation1.start();

        animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
                "x", touchX);
        animation2.setDuration(2000);
        animation2.start();
        break;
    }
    return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29664913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档