2017-08-18 9 views
0

私はバーチャルジョイスティックのコードをwebsiteから使用しています。 正常に動作しますが、私のアプリケーションでは機能しません。 私は何らかのアクションの実装に問題があります。 JoyStick.classでは、あらゆる方向の範囲を制御できます。範囲が特定の角度と距離の間にある場合、各方向のint値を返します。バーチャルジョイスティック - 1つのアクションではなく複数のアクション

public int get4Direction() { 
    if(distance > min_distance && touch_state) { 
     if(angle >= 225 && angle < 315) { 
      return STICK_UP; 
     } else if(angle >= 315 || angle < 45) { 
      return STICK_RIGHT; 
     } else if(angle >= 45 && angle < 135) { 
      return STICK_DOWN; 
     } else if(angle >= 135 && angle < 225) { 
      return STICK_LEFT; 
     } 
    } else if(distance <= min_distance && touch_state) { 
     return STICK_NONE; 
    } 
    return 0; 
} 

私は自分のアプリケーションのために適切ではないものを表示するためのSystem.out.printlnを使用。私は、その後、例えば、特定の方向に左スティックを入れた場合、それはそのような出力を生成します。

08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.017 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.037 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.117 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.137 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.157 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.167 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.187 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.207 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.217 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.317 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.337 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.357 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.377 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.387 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.407 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.427 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.437 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.457 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.477 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.487 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.507 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.527 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.537 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.557 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.577 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 
08-18 14:02:10.587 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.607 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.627 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.637 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.657 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.677 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.687 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 
08-18 14:02:10.707 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 

複数の出力が他の方向についても同様です。もっと複雑なアクションがアプリやアプリをクラッシュさせると非常に遅くて警告が出るので、私にとっては問題です。 The application may be doing too much work on its main thread 一度だけ出力を得る方法はありますか?スティックが左方向の範囲にある場合は、それだけで1行をgetenerating:

08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ LEFT 

場合UPなど

08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 I/System.out﹕ UP 

それはすべてのif/else文、そうで角度や距離を確認することです私はすべての方向または同様のものに対してこの範囲を1回だけチェックできる解決策を探しています。私はラジオボタン、チェックボタン、変数を使っていましたが、役に立たなかったし、1行の出力を得ることができませんでした。

フルコード:

JoyStick.class

Source website

MainActivity:

public class MainActivity extends Activity { 
RelativeLayout layout_joystick; 
ImageView image_joystick, image_border; 
TextView textView1, textView2, textView3, textView4, textView5; 

JoyStickClass js; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textView1 = (TextView)findViewById(R.id.textView1); 
    textView2 = (TextView)findViewById(R.id.textView2); 
    textView3 = (TextView)findViewById(R.id.textView3); 
    textView4 = (TextView)findViewById(R.id.textView4); 
    textView5 = (TextView)findViewById(R.id.textView5); 



    layout_joystick = (RelativeLayout)findViewById(R.id.layout_joystick); 

    js = new JoyStickClass(getApplicationContext(), layout_joystick, R.drawable.redball2); 
    js.setStickSize(150, 150); 
    js.setLayoutSize(500, 500); 
    js.setLayoutAlpha(150); 
    js.setStickAlpha(100); 
    js.setOffset(90); 
    js.setMinimumDistance(50); 


    layout_joystick.setOnTouchListener(new OnTouchListener() { 
     public boolean onTouch(View arg0, MotionEvent arg1) { 


      js.drawStick(arg1); 
      if(arg1.getAction() == MotionEvent.ACTION_DOWN 
        || arg1.getAction() == MotionEvent.ACTION_MOVE) { 
       textView1.setText("X : " + String.valueOf(js.getX())); 
       textView2.setText("Y : " + String.valueOf(js.getY())); 
       textView3.setText("Angle : " + String.valueOf(js.getAngle())); 
       textView4.setText("Distance : " + String.valueOf(js.getDistance())); 

       int direction = js.get4Direction(); 
       if(direction == JoyStickClass.STICK_UP) { 
        textView5.setText("Direction : Up"); 

        System.out.println("UP"); 

       } else if(direction == JoyStickClass.STICK_RIGHT) { 
        textView5.setText("Direction : Right"); 
        System.out.println("RIGHT"); 

       } else if(direction == JoyStickClass.STICK_DOWN) { 
        textView5.setText("Direction : Down"); 
        System.out.println("DOWN"); 

       } else if(direction == JoyStickClass.STICK_LEFT) { 
        textView5.setText("Direction : Left"); 
        System.out.println("LEFT"); 

       } else if(direction == JoyStickClass.STICK_NONE) { 
        textView5.setText("Direction : Center"); 
        System.out.println("CENTER"); 
       } 
      } else if(arg1.getAction() == MotionEvent.ACTION_UP) { 
       textView1.setText("X :"); 
       textView2.setText("Y :"); 
       textView3.setText("Angle :"); 
       textView4.setText("Distance :"); 
       textView5.setText("Direction :"); 
      } 


      return true; 
     } 
    }); 
} 

}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_margin="10dp" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="X" 
     android:textColor="#444444" 
     android:textSize="20dp" /> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:text="Y" 
     android:textColor="#444444" 
     android:textSize="20dp" /> 
    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView2" 
     android:text="Angle" 
     android:textColor="#444444" 
     android:textSize="20dp" /> 
    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView3" 
     android:text="Distance" 
     android:textColor="#444444" 
     android:textSize="20dp" /> 
    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView3" 
     android:text="Direction" 
     android:textColor="#444444" 
     android:textSize="20dp" /> 
</LinearLayout> 
<RelativeLayout 
    android:id="@+id/layout_joystick" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_below="@+id/linearLayout1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="29dp" 
    android:background="@drawable/image_button_bg" > 
</RelativeLayout> 

+0

'angle'変数をテストする:' get4Direction() 'は常に' STICK_UP'を返します。また、 'get4Direction(angle、min_distance、touch_state) 'のように' angle'、 'min_distance'と' touch_state'が 'get4Direction()'メソッドのパラメータであった場合は、変更された情報がないため、はるかに良いでしょう。 –

+0

たぶん私は十分に具体的ではなかった、出力は、上下左右正しく生成する問題は、私は1行の代わりに複数の行を取得し、それはいくつかのケースで私のアプリをクラッシュさせる – AidanSalvatore

+0

それはすべてのACTION_DOWN '、' ACTION_MOVE'のために複数回。 'ACTION_MOVE'が何度も呼び出されたようです。 'direction!= prevDirection'のときは、' direction'の以前の状態を保存して、何かを一度だけ行うことができます。また、 'direction = prevDirection'の変更後に' direction'を更新することを忘れないでください。 –

答えて

1

それはすべてのACTION_DOWNに一度火災、およびACTION_MOVEのために複数回になるので、それは予想される動作だようです。多くの場合、ACTION_MOVEが呼ばれたようです。グローバル変数を追加してみてくださいすべてのACTION_MOVEイベントのためのプロセスを避けるために:

int prevDirection = 0; 

はその後public boolean onTouch(View arg0, MotionEvent arg1)に条件を追加:

... 
int direction = js.get4Direction(); 

if (direction != prevDirection) { 
    if(direction == JoyStickClass.STICK_UP) { 
     textView5.setText("Direction : Up"); 

     System.out.println("UP"); 
    } else if(direction == JoyStickClass.STICK_RIGHT) { 
    ... 

    prevDirection = direction; 
} 
... 

それは良い解決策はないが、それは例のコードを台無しにしません、これは非常に良いではありません)。理想的にはcustom viewを作成する必要があります。

+0

これは私が探していたものです、ありがとうございました – AidanSalvatore

+0

あなたは大歓迎です! –

関連する問題