Using ViewGroup.addView() to add a GestureOverlayView to a SurfaceView game panel - android

After much help from the good people here, I finally almost got my little game panel (thats a surfaceview class) up and running with a GestureOverlayView. Through the use of addView I finally was able to add the GestureOverlayView and get it to work, the trouble is that calling this:
/** create the login form */
private ViewGroup _createGameStuffs() {
GestureOverlayView gestures = new GestureOverlayView(this);
gestures.setOrientation(gestures.ORIENTATION_VERTICAL);
gestures.setEventsInterceptionEnabled(true);
gestures.setGestureStrokeType(gestures.GESTURE_STROKE_TYPE_MULTIPLE);
gestures.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Panel p = new Panel(this); //my game view
panel.addView(p,0); //add it to the group
panel.addView(gestures,1); //add the gestureoverlayview i made to the group
return panel;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.myspells);
// create the panel to enclose everything
View mainPanel = _createGameStuffs();
// show the panel on the screen
setContentView(mainPanel);
//setContentView(p);
}
Depending upon which index I give which view takes presedence. This leads me to believe my game panel has to be added as a child view to the GestureOverlayView, is this correct? If so, how do I get at the children views of the GestureOverlayView? Or is it the other way around, GestureOverlayView becomes a child of my main view?

Related

Draw a circle partially inside on a button dynamically android

My class Triangle extends from View and its method onDraw consists of:
... onDraw(){
...
drawCircle(anyx, anyy, anyradius, anypaint);
}
And the Activity in which I want to create the Button and the Circle, both created dynamically.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newexample);
RelativeLayout viewGroup = (RelativeLayout)findViewById(R.id.visiont);
Button b1 = new Button(this);
viewGroup.addView(b1);
b1.setX(140); // Position of the button
b1.setY(140);
ViewGroup.LayoutParams params = b1.getLayoutParams();
params.height=80; // Size of the button
params.width=80;
b1.setLayoutParams(params); // Deleting this does not change the behaviour of what I am getting (only changes the height and width)
Circle c = new Circle(this);
c.setColor(Color.DKGRAY);
c.setCircle(150,130,80);
viewGroup.addView(c);
}
An example of what is the desired result, the rectangle is the button.
What I am getting now is the opposite, the button is over the circle.
I tried to switch Button to ImageButton since I wanted to have a custom image in the button (Actually I wanted a clickable component)
It appears that, with only switching Button to ImageButton, works as desired, so, for me it's a suitable solution.

Adding form components to a custom android view

I have a custom view (extends View) and I would like to add controls (buttons/text boxes etc) in the OnCreate function to add these components to the view at runtime:
public Section(Context context) {
super(context);
this.setBackgroundColor(Color.argb(255, 242, 242, 242));
this.setOnTouchListener(mSectionOnTouch);
LinearLayout l = new LinearLayout(this.getContext());
l.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this.getContext());
btn.setId(1);
btn.setText("btn1");
l.addView(btn);
Button btn2 = new Button(this.getContext());
btn2.setId(2);
btn2.setText("btn2");
l.addView(btn2);
} // Section
but this does not seem to do anything... Could someone tell me what im doing wrong?
Many thanks
FR
You never add l to your view. It should look like this:
public Section(Context context)
{
// setup linear layout
addView(l);
}
A slightly simpler way to do this would be to have your custom view extend LinearLayout. Then you can just add the views directly to your custom view and you don't have to nest another container, which is better for performance.

Gesture recognizer and AndEngine (Android)

In the game I'm developing I need this function: being able to draw on the screen and to establish if the drawing is a determined shape (let's say a rectangle, triangle or whatever).
So I wanted to use gestures, which are the easier way to do that; with the gesture builder I made a few shapes and I use them as models. The thing is, I can't make it work with the AndEngine.
I need the GestureOverlayView, which is basically the drawing board, to be placed ON TOP of the scene of the game, so that I can see the scene itself with all the entities attached and I can also draw gesture.
Right now what I tried didn't work, either the AndEngine stuff is shown on screen or the GestureOverlayView is, not both.
I'm new both with android developing and the AndEngine, so I'm kinda stuck here... do you have any ideas on how could I make it work? I looked everywhere but I can't find anything useful...
I'll leave the code of the gesture part below just for example, it won't really work obviously if you don't have any pre-made gestures in the path res/raw of the project:
public class ProvaGesture extends Activity implements GestureOverlayView.OnGesturePerformedListener
{
private GestureLibrary gestureLib;
public GestureOverlayView gestureOverlayView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gestureOverlayView = new GestureOverlayView(this);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load())
{
finish();
}
setContentView(gestureOverlayView);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions)
{
if (prediction.score > 1.0)
{
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
I didn't see AndEngine part in your demo code. Your ProvaGesture activity should be extended from AndEgine's SimpleBaseGameActivity or BaseGameActivity then implements IOnSceneTouchListener.
Base on your idea, I guessed that you try to create a separated overlay view which handles only the gesture, so you must consider:
You have only one activity running at a time in android.
If you want to create an User-defined view overlaping the AndEgine's surface view, you should see Andengine's example: XMLLayoutExample.
The solution may be [suggested]:
public class OverlapLayoutExample extends SimpleLayoutGameActivity {
#Override
protected int getLayoutID() {
//your layout id (xml file in layout folder)
return R.layout.overlaplayout;
}
#Override
protected int getRenderSurfaceViewID()
{
//overlaplayout will contain a SurfaceView with following ID
//this surface is where you render Andgine
return R.id.overlaplayout_rendersurfaceview;
}
}
Don't forget to create your own layout: overlaplayout containing surfaceView and OverlayView

How to add view components dynamically from activity

I am attempting to create a user interface dynamically. I have successfully create a view and loaded my background image. I have created two additional small view items to display on the background. My problem is that I have not been able to find any advice/instruction that tells me how to draw the small views. It seems that it should be a trivial exercise and I am guessing it is just finding the correct referencing. Hope someone out there can point me in the right direction.
Here is my Activity:
public class GhostActivity extends Activity implements OnTouchListener
{
private DrawView ghostView;
public Card mCard1, mCard2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// ToDo add your GUI initialization code here
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ghostView = new DrawView(this);
setContentView(ghostView);
//get the window size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Context context = getApplicationContext();
//create view items with initial positions
Point startPoint;
startPoint = new Point();
startPoint.x = 5;
startPoint.y = 3;
mCard1 = new Card(context, 1, R.drawable.bol_geel, startPoint);
startPoint.x = 5;
startPoint.y = 43;
mCard2 = new Card(context, 2, R.drawable.bol_rood, startPoint);
//now display them on the ghostView *****************HOW?
// set the callbacks
ghostView.setOnTouchListener(this);
mCard1.setOnTouchListener(this);
mCard2.setOnTouchListener(this);
}
and here is the View;
public class DrawView extends View
{
Drawable bg ;
public DrawView(Context context) {
super(context);
//setFocusable(true);
Drawable bg = this.getResources().getDrawable(R.drawable.bubbleblue480x800);
setBackgroundDrawable(bg);
}
#Override protected void onDraw(Canvas canvas) {
// canvas.drawColor(0x0000000); //if you want another background color
//draw on the canvas
}
}
edit: I believe my problem is needing to pass a pointer to the ghostView canvas. what makes me think that is if I create the children within ghostView then call their .draw method they appear exactly as I would expect.
#Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0x0000000); //if you want another background color
//draw the cards on the canvas
mCard1.draw(canvas);
mCard2.draw(canvas);
}
so at this point I am wondering how to get a reference pointer to the ghostView canvas.
To be honest I am finding the whole Activity - View relationship confusing.
Edit: I have taken a different approach based on detail in this tutorial
http://www.kellbot.com/2009/06/android-hello-circle/
It uses a FrameLayout and it seems I can achieve my objective.
To add view dynamically to view your class must extends from ViewGroup or LinearLayout class then you will able to call method addView.
Inside your ghost view first add a layout e.g Linear or Relative. Then only you could able to add views inside that layout you cant simply add a view to a xml file.
Or you can create a dynamic layout then only u can add view inside that layout.
RelativeLayout relative= new RelativeLayout(findViewById(R.id.your relativeLayoutID));
relative.addView(child);
child could be anything button textview and widget.

Adding View to Relative Layout behind an existing view causes screen flicker

I'm trying to insert a View behind another view that is taking up the full screen and then later removing the view in the front to reveal the only remaining view. Functionally, everything is working as expected but the problem is that when I call View.addView() to add the second view, specifying to add it at index 0 so it is behind the first view, the screen flickers. It's almost as if the view is actually getting added in front of the first view for a fraction of a second and then it is hidden again as it is moved behind it.
Here's what I'm doing:
When the Activity is created I add an ImageView to a RelativeLayout and make the RelativeLayout instance the Activity's content view:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
m_layout = new RelativeLayout(this);
m_layout.setBackgroundColor(Color.BLACK);
m_splashImage = new ImageView(this);
m_splashImage.setImageResource(R.drawable.splash);
m_splashImage.setScaleType(ScaleType.FIT_XY);
m_layout.addView(m_splashImage,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
setContentView(m_layout);
}
When the Activity is started, I created and add the GLSurfaceView to the RelativeLayout at index 0, so it is behind the ImageView:
protected void onStart() {
super.onStart();
m_layout.addView(new MyGLSurfaceView(), 0,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
}
Later, after all of the loading is done and the GLSurfaceView is ready to continuously render,
the splash ImageView is removed and cleaned up.
public void hideSplashScreen() {
if (m_splashImage != null) {
m_layout.removeView(m_splashImage);
m_splashImage = null;
}
}
Is there a better way to do this that doesn't require creating the GLSurfaceView before the onStart() is called?
Have you tried using view.setVisibility(View.GONE) on of the view that you adding behind? Of course before you are adding it.

Categories

Resources