2011-01-24 5 views
4

キャンバスの上にUI要素を設定するにはどうすればよいですか?Android:キャンバスレイヤー上のUI要素

私はキャンバスを使用してカスタム表示にグラフィックを配置した簡単なタッチゲームを持っています。しかし、私の全画面パネルがsetContentView()にあるので、私はprogressBarやロゴのようなUI項目を追加することはできません。私はいくつかのオブジェクト(progressBar、ロゴ)で表示された全体のキャンバスレイヤーを "浮動"にしたいと思います。

メインクラス:

public class GameClass extends Activity implements OnGestureListener { 

Panel panel; 

public void onCreate(Bundle savedInstance) { 
    super.onCreate(savedInstance); 

    panel = new Panel(this, 1, 2); 
    setContentView(Panel); 
    ... 
} 

Panelクラス:コードが示すように

public class Panel extends View { 

    public Panel(Context context, int Var1, int Var2) { 
    super(context); 
    ... 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    ... 
    } 
} 

、タッチがGameClass内部処理され、アニメーションの変化は、パネル内部で扱われます。

または、おそらく..オーバーレイアクティビティのボタンと元のアクティビティのオブジェクトの両方が表示されるように、プログレスバーとボタンで新しい透明アクティビティを開始することは可能でしょうか?透明なアクティビティのボタンを使用して、すべてのレイヤー(透明なアクティビティ、Panel、GameClass)を閉じる方法が必要でした。複雑ですか? :D

答えて

3

私は同じ問題を持っているし、ここで私はそれが私のgame.xmlに追加されたのLinearLayoutに追加するパネルにcontentviewを設定するので

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.game); 

    drawView = new DrawView(this); 
    drawView.requestFocus(); 
    LinearLayout upper = (LinearLayout) findViewById(R.id.LinearLayout01); 
    upper.addView(drawView); 
    ImageButton menu = (ImageButton) findViewById(R.id.ImageButton01); 
    menu.setOnClickListener(new OnClickListener() 
    {   
     @Override 
     public void onClick(View v) 
     { 
      finish(); 
     } 
    }); 
    ImageButton reset = (ImageButton) findViewById(R.id.ImageButton02); 
    reset.setOnClickListener(new OnClickListener() 
    {   
     @Override 
     public void onClick(View v) 
     { 
      drawView.resetGame(); 
     } 
    }); 
} 

代わりにそれを解決する方法であり、ここで私を行く

game.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:background="@drawable/game_background"> 
<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="350dip" /> 
<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/LinearLayout01"> 
    <ImageButton 
     android:id="@+id/ImageButton01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/menu"></ImageButton> 
    <ImageButton 
     android:id="@+id/ImageButton02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/reset"></ImageButton> 
</LinearLayout> 
<LinearLayout 
    android:layout_below="@+id/LinearLayout01" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:id="@+id/layout_ad" 
    android:layout_height="wrap_content" /> 

+0

この場合、イメージボタンの下にキャンバスを縦に描画します。私は今、私のアプリで効果をチェックしています。私が達成したいのは、イメージボタンやプログレスバーをオーバーレイキャンバスに半透明にすることです。今これを使用する方法をテストします。 – yosh

+0

あなたはハンドラのコンセプトを知っていますか?http://developer.android.com/reference/android/os/Handler.html – ingsaurabh

+0

これを少し変更し、xmlを使わずにRelativeLayout内の2つのUIElementsを使用しました。このように、後者は上にあります:) – yosh

関連する問題