2016-08-28 5 views
0

私はいくつかの助けが必要です。オンスクリーンのナビゲーションボタンを使用してビットマップオブジェクトを移動する

私は、画面上に配置されたビットマップイメージと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" /> 

答えて

0

私はキャンバスをチェックアウトすることをお勧めします。 Canvas in Android Studio。このようなオブジェクトをスクリーンの周りに動かすことは非常に役立ちます。もう一つの便利な場所は、CanvasのAndroidドキュメントです。ただし、以下のリンクは、オブジェクトをキャンバスに描画して移動するチュートリアルです。 onClickListenerを使用して、ボタンの移動値を追加することで、if文と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は、数多くのドキュメンテーションと無料のチュートリアルを備えた非常に一般的なエンジンです。

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

をドロップすることができます
関連する問題