0

ユーザーが画面をタップした時点でボール (ビットマップ) がキャンバスに表示されるアプリに取り組んできました。背景は xml レイアウト setContentView(R.layout.newsession) です。キャンバスは黒塗りのキャンバスです。Java 親クラス setContentView(customView) を設定すると、プログラムは正常に動作しますが、カスタム サーフェス ビューを XML レイアウトと setContentView(R.layout.newsession) に追加すると、画面にはキャンバスが表示されるだけで、OnTouch イベントは表示されません。動作しません。私は何か間違ったことをしていますか?私はこれにほぼ1週間取り組んでおり、本当に助けが必要です. XML レイアウトとカスタム surfaceView のコードを以下に掲載します。前もって感謝します!

XML レイアウト (ニュースセッション)

    <FrameLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
     >

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/newSessionPage" 
    >   

    <ImageView 
      android:layout_width="231dp" 
      android:id="@+id/ivStrikeGrid" 
      android:layout_gravity="center" 
      android:layout_height="270dp"
      android:layout_marginTop="18dp" 
      android:src="@drawable/strike_grid"
      android:layout_marginBottom="10dp"
    />

    <appsys.studios.CustomSurfaceViewOne 
     android:id="@+id/customSurfaceViewOne1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
    >

  </FrameLayout>

カスタム SurfaceView

   package appsys.studios;
   public class CustomSurfaceViewOne extends SurfaceView implements Runnable{
       public CustomSurfaceViewOne(Context context, AttributeSet attr) {
          super(context, attr);
          ourHolder = getHolder();
     }
      // Other stuff
  }

次のようにうまく動作します:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(newSeshView);

しかし、次のように XML レイアウトから使用しようとしても何も起こりません。

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(R.layout.newsession);

再度、感謝します!:)

4

2 に答える 2

0

私は同じ問題に遭遇し、答えを探しているときにあなたの質問を見つけました。私はそれを解決しましたが、それが正しい方法かどうかはわかりません。

3つのファイル、メインアクティビティ、サーフェスビュー、およびXMLファイル。

XMLファイルは問題ないように見えます。サーフェスビューが含まれています

<appsys.studios.CustomSurfaceViewOne 
 android:id="@+id/customSurfaceViewOne1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
>

あなたの活動では、追加implements OnTouchListenerし、使用setContentView(R.layout.newsession);し、その後、まだあなたのonCreate()この行CustomViewOne cvo = (CustomViewOne)findViewById(R.id.customSurfaceViewOne1)を追加し、リスナーをそれに設定しますcvo.setOnTouchListener(this);

次に、onTouchを追加します

@Override
public boolean onTouch(View v, MotionEvent event) {
    customSurfaceViewOne1.myOnTouch(event);
    return false;
}

ここmyOnTouch()で、はすべてのontTouchイベントを実行するcustomSurfaceViewOne1クラスのメソッドです。を渡したことに注意してMotionEvent eventください。

繰り返しになりますが、これが適切な方法であるかどうかはわかりません。それが私がそれを機能させる方法です。私がそれをした理由は、表面ビューの上にAdMob広告を表示できるようにするためでした。

于 2012-11-02T17:35:31.030 に答える
0

タッチ リーディング デリゲートのビューの invallidate()呼び出しが欠落している可能性があると思います。

あなたのコードで何が起こっているのか正確にはわかりません。しかし、私が自分のビューを作成し、それを自分のレイアウトに追加するとします。そして、タッチイベントの読み取り時にそれ自体を変更したい場合は、myownビュークラスで自分自身のようなことをします

   @override
    // i dont remem exact signature of this method. google it or see docs
    // motive is to read touch event of view and doing appropriate changes
    // and redrawing the view again
   public void onTouchEvent(MotionEvent me)
   {
      doCalculation(); // change points where to draw the ball next time. Read me
      invalidate();  // tell the view re draw it self

   }

それが役に立てば幸い :)

于 2011-11-06T05:37:19.720 に答える