0

私はiOS開発を経験しましたが、Android開発は初めてで、ここで初心者に質問します...

私は、pngやアニメーションのカスタム描画を多く行い、標準のUI要素をまったく持たないアプリを作成しています。そして、SurfaceViewの道を進むことを選択しました。私はSurfaceViewコードで触れられたもののすべての検出も処理します。

しかし、SurfaceViewコード内からビュー間のナビゲーションをどのように処理するのでしょうか。たとえば、QuizActivityというアクティビティに移動するにはどうすればよいですか?「通常の」ビュー/アクティビティでは、次のようにします。

Intent intent = new Intent(getBaseContext(), QuizActivity.class);
startActivity(intent);

しかし、SurfaceView内からgetBaseContextとstartActivityにアクセスできません。アクセスしたとしても、複数のビューが同時に読み込まれることになりますか?

結論:SurfaceView内からコードにこのナビゲーションを手動で実装するにはどうすればよいですか?

ありがとう
Søren

4

1 に答える 1

2

サーフェスビューから次のように呼び出します。

    Intent intent = new Intent(getContext(), QuizActivity.class);
    getContext().startActivity(intent)

すべてのビューには、実行中のコンテキストへの参照があり、コンテキストは常に新しいアクティビティやサービスを開始したり、リソースを取得したりできます。

編集

サーフェスビューにこれを含めます:

    private SurfaceCallbacks listener;

    public interface SurfaceCallbacks{
       public void onTouch(/* any data you want to pass to the activity*/);
    }

    public void registerSurfaceCallbacksListener(SurfaceCallbacks l){
       listener = l;
    }

    // and then whenever the surface being touched and you want to call something outside of the surface you do:

    if(listener!=null)
       listener.onTouch(/* pass the parameters you declared on the interface */);

そして、表面を保持する活動であなたはこれをします:

    public ActivityThatHoldsSurface extends Activity implements SurfaceCallbacks{

       // that comes form the surface
       @Override
       onTouch(/* your parameters */){
          // do the navigation stuff
       }

       // and immediately after you inflate or instantiate your surface you do:
       mySurface.registerSurfaceCallbacksListener(ActivityThatHoldsSurface.this);

    }

それは意味がありますか?

于 2012-11-08T10:16:50.587 に答える