【问题标题】:Moving a bitmap object using onscreen navigation buttons使用屏幕导航按钮移动位图对象
【发布时间】:2016-08-28 09:18:28
【问题描述】:

我需要一些帮助。

我在屏幕上放置了一个位图图像和 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;
                }
            }
        });

Velocity 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" />

【问题讨论】:

    标签: java android android-studio bitmap


    【解决方案1】:

    使用Canvas 来渲染您想要移动的位图。它们可用于在您指定的新位置重绘位图。

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

    希望对你有帮助:)

    【讨论】:

      【解决方案2】:

      我建议您查看 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 是一个非常受欢迎的引擎,拥有大量文档和免费教程。

      【讨论】:

        【解决方案3】:

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

        <?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

        【讨论】:

          猜你喜欢
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          • 2021-07-02
          • 2021-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-12
          相关资源
          最近更新 更多