Android: user interface elements on top of the canvas layer

How to set some user interface elements over (over) a canvas?

I have a simple touch screen game that has graphics placed on a custom canvas view. However, since my full-screen panel is in setContentView (), I cannot add user interface elements such as progressBar or logo. I would like the entire canvas layer to be visible with some objects (progressBar, logo) floating above it.

Main class:

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 Class:

 public class Panel extends View { public Panel(Context context, int Var1, int Var2) { super(context); ... } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); ... } } 

As the code shows, touches are processed inside the GameClass, and changes to the animation are processed inside the Panel.

Or maybe one could start a new transparent activity with a progress bar and a button so that both buttons on overlay activity and objects on main activity? And I may need a way to close all layers (transparent activity, panel, GameClass) using the transparent activity button. Difficult ?: D

+4
source share
1 answer

I have the same problem and this is how I solved it

 @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(); } }); } 

instead of setting the contentview to the panel by adding Linringayout added to my game.xml and here is my

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" /> 

+3
source

Source: https://habr.com/ru/post/1336778/


All Articles