使用屏幕导航按钮移动位图对象

时间:2016-08-28 09:18:29

标签: java android android-studio bitmap

我需要一些帮助。

我在屏幕上放置了一个位图图像和4个导航按钮(向上,向下等)。现在我想使用这些按钮移动该图像,当然,只要按下按钮,图像就会继续移动,并且只有在用户松开手指后图像才会停止。现在我不确定下面的代码是否按预期工作,所以这就是我来到这里的原因。

以下是代码:

这是图像:

//SRC-Rect Values
        int playerLeft, playerTop, playerBottom, playerRight;

这是图片在屏幕上的位置:

//DEST-Rect Values
        int destBottom, destRight, destTop, destLeft;

以下是按钮:

//Arrow Buttons to move the player
        up = (Button)findViewById(R.id.buttonUp);
        right = (Button)findViewById(R.id.buttonRight);
        down = (Button)findViewById(R.id.buttonDown);
        left = (Button)findViewById(R.id.buttonLeft);

        //Up Button
        up.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                while (true){

                    velocityY++;

                    destTop += topMove;
                    destBottom += bottomMove;
                }
            }
        });

        //Right Button
        right.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                while (true){

                    velocityX++;

                    destRight += rightMove;
                    destLeft += leftMove;
                }
            }
        });

        //Down Button
        down.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                while (true){

                    velocityY--;

                    destTop -= topMove;
                    destBottom -= bottomMove;
                }
            }
        });

        //Left Button
        left.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                while (true){

                    velocityX--;

                    destRight -= rightMove;
                    destLeft -= leftMove;
                }
            }
        });

速度X和Y是移动时图像的速度, 移动按钮值是触摸按钮时需要移动的像素数量。

//Arrow Buttons
private Button up, right, down, left;

//Movement Button Values
public static int topMove = -20;
public static int rightMove = -20;
public static int bottomMove = 20;
public static int leftMove = 20;

//Movement speed
public static int velocityX = 0;
public static int velocityY = 0;

这是XML布局:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonUp"
    android:background="@drawable/up"
    android:layout_above="@+id/buttonLeft"
    android:layout_toRightOf="@+id/buttonLeft"
    android:layout_toEndOf="@+id/buttonLeft" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonLeft"
    android:background="@drawable/left"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonRight"
    android:background="@drawable/right"
    android:layout_alignTop="@+id/buttonDown"
    android:layout_toRightOf="@+id/buttonDown"
    android:layout_toEndOf="@+id/buttonDown" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonDown"
    android:background="@drawable/down"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/buttonUp"
    android:layout_alignStart="@+id/buttonUp" />

3 个答案:

答案 0 :(得分:1)

使用Canvas呈现您要移动的位图。它们可用于在您指定的新位置重绘位图。

请参阅: https://makingthechain.wordpress.com/2013/12/17/how-to-move-a-bitmap-using-android-canvas/

希望有所帮助:)

答案 1 :(得分:0)

我建议看一下Canvas。 Canvas in Android Studio。在屏幕上移动像这样的物体是非常有帮助的。另一个有用的地方是适用于Canvas的Android文档。但是,以下链接是将对象绘制到画布上并移动它的教程。只需使用onClickListener通过添加按钮移动值来更新位置与“if”语句检查。 Tutorial on the Canvas

//Movement Button Values
public static int topMove = -20;
public static int rightMove = -20;
public static int bottomMove = 20;
public static int leftMove = 20;

我希望有所帮助。如果您尝试的不仅仅是简单的快速交互,我还建议您使用游戏引擎。 Unity3D是一个非常受欢迎的引擎,有大量文档和免费教程。

答案 2 :(得分:0)

我为你写了一个例子 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">
    <Button
        android:id="@+id/move_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="left"/>
    <Button
        android:id="@+id/move_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="right"/>
    <Button
        android:id="@+id/move_down"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="down"/>
    <Button
        android:id="@+id/move_up"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="up"/>
</LinearLayout>
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <ImageView
                android:id="@+id/image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:src="@android:drawable/sym_def_app_icon"/>
    </FrameLayout>

</LinearLayout>

片段类

public class DebugFragment extends Fragment implements 
View.OnTouchListener {

 public static final int PERIOD = 30; //move view every PERIOD milliseconds until ACTION_UP event is coming

public static final int STEP = 10; //dx pixels

private View mImage;

private Handler mHandler = new Handler(Looper.getMainLooper());

private boolean isNeedMoving;

enum MOVE {
    LEFT, RIGHT, DOWN, UP
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_debug, container, false);

    mImage = rootView.findViewById(R.id.image);

    View leftMoveBtn = rootView.findViewById(R.id.move_left);
    View rightMoveBtn = rootView.findViewById(R.id.move_right);
    View downMoveBtn = rootView.findViewById(R.id.move_down);
    View upMoveBtn = rootView.findViewById(R.id.move_up);

    leftMoveBtn.setOnTouchListener(this);
    rightMoveBtn.setOnTouchListener(this);
    downMoveBtn.setOnTouchListener(this);
    upMoveBtn.setOnTouchListener(this);


    return rootView;
}


@Override
public boolean onTouch(View v, MotionEvent event) {


    int maskedAction = event.getActionMasked();

    final MOVE actionMove = getActionMove(v.getId());

    switch (maskedAction) {

        case MotionEvent.ACTION_DOWN:
            isNeedMoving = true;
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    moveView(actionMove);
                    if (isNeedMoving) {
                        mHandler.postDelayed(this, PERIOD);
                    }
                }
            });
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            isNeedMoving = false;
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            break;

    }
    return true;
}

private MOVE getActionMove (@IdRes int id) {
    switch (id) {
        case R.id.move_left:
            return MOVE.LEFT;
        case R.id.move_right:
            return MOVE.RIGHT;
        case R.id.move_down:
            return MOVE.DOWN;
        case R.id.move_up:
            return MOVE.UP;
        default:
            return MOVE.LEFT;
    }
}

private void moveView(MOVE action) {
    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mImage.getLayoutParams();
    switch (action) {
        case LEFT:
            layoutParams.leftMargin -= STEP;
            break;
        case RIGHT:
            layoutParams.leftMargin += STEP;
            break;
        case DOWN:
            layoutParams.topMargin += STEP;
            break;
        case UP:
            layoutParams.topMargin -= STEP;
            break;

    }
    mImage.setLayoutParams(layoutParams);
}
}

因此,如果您需要通过手指滑动来移动图像,那么您可以实施拖放事件http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html