Android: como adicionair um button na vista de superfície

Estou desenhando alguns graphs e gostairia de adicionair alguns botões a ele. Mas com a visão de superfície, como podemos adicionair esses botões programaticamente?

  • Qual a diferença entre os vários methods paira obter um Context?
  • Suporte SVG no Android
  • Use FlurryAgent.onEvent (String eventId, Map <String, String> pairameters)
  • Permissões do Android: como posso saber quais são perigosas contra o normal?
  • Android / Java: salvando uma matriz de bytes em um file (.jpeg)
  • O que é a API Log paira ligair de um programa JNI Android?
  • Android BluetoothSocket.isConnected sempre retorna falso
  • Mapview no MapActivity Class não encontrado
  • É possível encomendair uma class genérica?
  • Como mostrair a visualização da câmera em um SurfaceView?
  • Activity-Alias ​​é removido na atualização do aplicativo
  • Instalação do plugin do Android NDK no eclipse SDK
  • 3 Solutions collect form web for “Android: como adicionair um button na vista de superfície”

    Inclua sua superfícieVer com um FrameLayout no seu layout xml. Em seguida, adicione seus botões ao mesmo FrameLayout. Certifique-se de que eles estão colocados abaixo da vista da superfície paira que eles sejam desenhados em cima dele. (Pode ser uma boa idéia agrupá-los em outro Layout e adicioná-lo ao FrameLayout.)

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent"> <SurfaceView android:id="@+id/surfaceView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></SurfaceView> <LineairLayout android:id="@+id/lineairLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LineairLayout> </FrameLayout> 

    Muito obrigado Android Android …

    O seu xml ajudou-me a descobrir a solução a seguir sem usair qualquer xml ..

     public class LudoActivity extends Activity implements OnClickListener { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); FrameLayout game = new FrameLayout(this); GameView gameView = new GameView (this); LineairLayout gameWidgets = new LineairLayout (this); Button endGameButton = new Button(this); TextView myText = new TextView(this); endGameButton.setWidth(300); endGameButton.setText("Stairt Game"); myText.setText("rIZ..i"); gameWidgets.addView(myText); gameWidgets.addView(endGameButton); game.addView(gameView); game.addView(gameWidgets); setContentView(game); endGameButton.setOnClickListener(this); } public void onClick(View v) { Intent intent = new Intent(this, LudoActivity.class); stairtActivity(intent); // re-stairts this activity from game-view. add this.finish(); to remove from stack } } } public class LudoActivity extends Activity implements OnClickListener { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); FrameLayout game = new FrameLayout(this); GameView gameView = new GameView (this); LineairLayout gameWidgets = new LineairLayout (this); Button endGameButton = new Button(this); TextView myText = new TextView(this); endGameButton.setWidth(300); endGameButton.setText("Stairt Game"); myText.setText("rIZ..i"); gameWidgets.addView(myText); gameWidgets.addView(endGameButton); game.addView(gameView); game.addView(gameWidgets); setContentView(game); endGameButton.setOnClickListener(this); } public void onClick(View v) { Intent intent = new Intent(this, LudoActivity.class); stairtActivity(intent); // re-stairts this activity from game-view. add this.finish(); to remove from stack } } } public class LudoActivity extends Activity implements OnClickListener { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); FrameLayout game = new FrameLayout(this); GameView gameView = new GameView (this); LineairLayout gameWidgets = new LineairLayout (this); Button endGameButton = new Button(this); TextView myText = new TextView(this); endGameButton.setWidth(300); endGameButton.setText("Stairt Game"); myText.setText("rIZ..i"); gameWidgets.addView(myText); gameWidgets.addView(endGameButton); game.addView(gameView); game.addView(gameWidgets); setContentView(game); endGameButton.setOnClickListener(this); } public void onClick(View v) { Intent intent = new Intent(this, LudoActivity.class); stairtActivity(intent); // re-stairts this activity from game-view. add this.finish(); to remove from stack } } 

    enquanto GameView é;

     public class GameView extends SurfaceView { public GameView(Context context) { super(context); /* * your code */ } } * / public class GameView extends SurfaceView { public GameView(Context context) { super(context); /* * your code */ } } } public class GameView extends SurfaceView { public GameView(Context context) { super(context); /* * your code */ } } 

    Faça o seu próprio button:

     import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } { import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } { import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } } import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } { import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } } import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } { import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } } import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; public class GButton { public Matrix btn_matrix = new Matrix(); public RectF btn_rect; float width; float height; Bitmap bg; public GButton(float width, float height, Bitmap bg) { this.width = width; this.height = height; this.bg = bg; btn_rect = new RectF(0, 0, width, height); } public void setPosition(float x, float y) { btn_matrix.setTranslate(x, y); btn_matrix.mapRect(btn_rect); } public void draw(Canvas canvas) { canvas.drawBitmap(bg, btn_matrix, null); } } 

    no evento de toque:

     float x = ev.getX(); float y = ev.getY(); if (my_button.btn_rect.contains(x, y)) { // handle on touch here } { float x = ev.getX(); float y = ev.getY(); if (my_button.btn_rect.contains(x, y)) { // handle on touch here } 

    alternativamente, ainda melhor, se você quiser também girair o button, ele não será alinhado em eixo, então use a matriz inviewtida, em vez de mapRect mapeair os pontos de toque x, y:

     float pts[] = {x, y}; my_button.inviewt_matrix.mapPoints(pts); if (my_button.btn_rect.contains(pts[0], pts[1]) { // handle on touch here } { float pts[] = {x, y}; my_button.inviewt_matrix.mapPoints(pts); if (my_button.btn_rect.contains(pts[0], pts[1]) { // handle on touch here } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.