在屏幕上和屏幕上移动ImageView

时间:2015-10-24 03:14:55

标签: android

我在Android上制作游戏。在这个游戏中你可以像普通的实体扑克牌那样打牌。

我希望能够如下所述移动imageView:

enter image description here enter image description here enter image description here

首先,我试图以这种方式移动ImageView:

    final View myView = findViewById(R.id.imageView);
        myView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                float down_x=0, down_y=0;
                switch (event.getAction()) {
                    case (MotionEvent.ACTION_DOWN):
                        Log.d(DEBUG_TAG, "Action was DOWN: " + Float.toString(event.getAxisValue(MotionEvent.AXIS_X)));
                        down_x = event.getAxisValue(MotionEvent.AXIS_X);
                        down_y = event.getAxisValue(MotionEvent.AXIS_Y);
                        return true;
                    case (MotionEvent.ACTION_MOVE):
                        Log.d(DEBUG_TAG, "Action was MOVE: " + Float.toString(event.getAxisValue(MotionEvent.AXIS_X)));
                        Log.d(DEBUG_TAG, "down_x: " + Float.toString(down_x) + "new Axis_X: " + Float.toString(event.getAxisValue(MotionEvent.AXIS_X)));
                        if (down_x<event.getAxisValue(MotionEvent.AXIS_X)) {
                            Log.d(DEBUG_TAG, "working");
                            myView.setLeft(myView.getLeft()+(int)(event.getAxisValue(MotionEvent.AXIS_X)-down_x));
                        }

                        return true;
                    case (MotionEvent.ACTION_UP):
                        Log.d(DEBUG_TAG, "Action was UP: " + Float.toString(event.getAxisValue(MotionEvent.AXIS_X)));
                        //Log.d(DEBUG_TAG, Float.toString(event.getAxisValue(MotionEvent.AXIS_X)));


           return true;
                    case (MotionEvent.ACTION_CANCEL):
                        Log.d(DEBUG_TAG, "Action was CANCEL");
                        return true;
                    case (MotionEvent.ACTION_OUTSIDE):
                        Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
                                "of current screen element");
                        return true;
                }
                return true;
            }
        });

然而,它没有按预期工作。

我应该使用哪些工具?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用event.getY()和event.getX()而不是event.getAxisValue()来获取用户手指的位置(以像素为单位)。如果卡的移动是否应该作为原子动作执行并不重要(一旦触发将卡移动到某个方向的手势,则卡在该方向上移动直到某个目的位置而没有来自用户的任何干预),你可以使用动画。

要使用动画,首先在res目录中的anim文件夹中定义它: 在res / anim / anim_name.xml里面:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="200"
    android:duration="250"
    android:fillEnabled="true"
    android:fillAfter="true"  <!-- Use fillEnabled and fillAfter to cause your view to remain in the position where it got to at the end of the animation instead of jumping back to the position where it was before the animation started -->
    />

然后将其加载到Java代码中:

Animation yourAnim = AnimationUtils.loadAnimation(context, R.anim.anim_name);

然后,当发现手势时,启动动画:

view.startAnimation(yourAnim);

如果您想允许用户执行拖放操作,而不是使用onTouch,则可能需要使用Android的拖放框架和OnDragListener。看到: http://developer.android.com/guide/topics/ui/drag-drop.html