Android SurfaceView vs GLSurfaceView for wallpaper-style background - android

I need to create a game for one of my projects. The game is not a grafic game but I still want it to have nice appearance (maybe 3D background look).
I thought I would use a diferent technique than just to load the background through an xml file. I want to create a surface view with a nice texture background (what I would like is one similar to the live wall papers). I know there is the rendererscript but I apparently it doesn't work on apis level lower than 11??? I would like to target api level 7 or 8.
Here is what I planned to do something like that:
mView = new BasicGLSurfaceView(getApplication());
View inflatedView=View.inflate(this, R.layout.title, null);
View inflatedView2=View.inflate(this, R.layout.buttons, null);
setContentView(mView);
this.getWindow().addContentView(inflatedView,
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
this.getWindow().addContentView(inflatedView2,
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
I was wondering which way is the best:
use open GLSurfaceView or just normal SurfaceView and Canvas. Which one is faster and will look nicer?
Could you please advice me on the best solution to implement?
thank you

Canvas is simpler, and it's fast, but it doesn't support 3D drawing. Since you said that you might want to do 3D, you would want to use the GLSurfaceView and OGLES. Canvas can often outperform OpenGL for 2D drawing.

Related

Have multiple content views simultaneously?

Alright, so the concept seems simple enough, but Have searched all over the web and have yet to find a solution. So I am making my first android game, woot. But I want to monetize it with ads. This is where the problem comes in. As I am Not using a layout for my game, but instead a GLSurfaceView, I cannot find anything about setting an ad over that. Every ad tutorial by google uses either a layout, or requires you to create a layout through java. The problem is, that those all still use a SurfaceView, not a GLSurfaceView. How can I implement an ad over the GLSurfaceView? I mean, most games, logically would be made using openGL, not canvas, so there would have to be a way, right?
Any suggestions will help. If you guys need to see some of the code, let me know, but it's pretty standard code from the glSurfaceView Tutorials.
GLSurfaceView is just a SurfaceView with some code wrapped around it to manage threads and the EGL context. They're not very different.
SurfaceView has two parts, the "surface" and the "view". The view is part of the View hierarchy, and is generally just a transparent window used for the layout. All of the View-based UI is drawn on a single layer. GLES rendering is done on the surface part, which is a separate layer composited by the system. By default, the surface layer sits behind the view layer, but you can configure that when the SurfaceView is created.
Since the Surface is behind the View, you can draw your ads with Views on top of your game without interfering with it.
You can find some examples in Grafika. For example, the "record GL app" activity has UI elements (some text and radio buttons) on top of GLES animation. It uses a relative layout and positions the UI elements on top of the SurfaceView.
According to this manual, you can test the code blow:
TextView textView = new TextView(this);
textView.setText("overlay!");
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Obviously, the code is in the Activity.
So I did much researching and finally found a solution that works, thanks to fadden for getting me on the right track btw :)
mGLView = new MyGLSurfaceView(this);
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
// Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("/6499/example/banner");
AdRequest.Builder adRequest= new AdRequest.Builder();
adView.loadAd(adRequest.build());
screenLayout.addView(adView);
screenLayout.addView(mGLView);
setContentView(screenLayout);

3D animation between layouts like the lock screen in jelly bean

So basically I'm trying to replicate this. It will be used like a view flipper. I haven't had much experience in doing this sort of stuff in android yet so any help would be appreciated!
I'm not exactly sure, what are you going to achieve, but I would start with such lecture: Android flip 3D animation. It's quite easy to replace views in the middle of such animation. You may consider writing your own container, if the ViewFlipper is not enough.
If the view is too complex for Android to handle, or your animation is intended to be more interesting than simple flip/scale/move, then you may consider using OpenGL and shaders. Simply draw your views to bitmaps, pass it to OpenGL and do whatever you want to do with them.

OpenGL overlay on camera preview - need to set content views in reverse order?

I am trying to show a dynamic OpenGL over the Android camera preview. I have used the typical framework suggested in other guides/sites to create an object which implements the GLSurfaceView.Renderer interface and use setContentView() and addContentView() after. The problem is that the onDrawImage is occurring behind the surface holder which displays the camera preview.
This is part of a larger application and I have separately tested all components in a side application. The only difference between the two is that the larger application has several activities preceding this one, while the side application uses just this activity as the main one.
Update
The answer is to switch the order of the setContentView and addContentView. If it is the main application, use the order in this post: opengl overlay on camera view; however, if you are coming from another activity, run:
setContentView(theCameraCallbackView)
addContentView(theOpenGLView, new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ))
If anyone can provide some reasoning behind this, it would be greatly appreciated! I spend about eight hours on this two-line swap so it would be great to know what the cause of this was (and hopefully prevent similar issues for others).

Need to use opengl in combination with View

I am new in android. I need to use opengl in combination with View Class to draw something. How to do this. plz give me any hints or suggestions...
You also have the option to combine "normal" android views (Button, ImageView, etc) in a layout with a GlSurfaceView. (i.e. FrameLayout)
By doing so you can combine normal android UI with openGL, you can also make a composite view where the openGL view is just part of the layout.
This is the way ads are usually displayed overlaying 3D aplications.
Be aware that this technique can produce quite weird results in low end devices when dealing with tranasparency.
You probably wanta GlSurfaceView. It's a View that allows you to use Opengl to draw to it.
Put it in a layout file for your app
<GLSurfaceView id="#+id/myView" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
Then in your code
((GLSurfaceView)findViewById(R.id.myView)).setRenderer(new GlSurfaceView.Renderer() {
public void onSurfaceCreated(...)...
public void onSurfaceChanged(...)...
public void onDraw(GL10 gl)...
});
((GLSurfaceView)findViewById(R.id.myView)).setRenderMode(RENDERMODE_CONTINUOUSLY);
And then you draw in your onDraw with your gl calls.
This is the super-quick start answer to your question, if you drop the surfaceview in framelayout and know what your drawing it's pretty quick to get a basic setup running.

display transparent view over existing views

In Android 2.2, I want to display a few sprites and clickable animations over an existing view. I just discovered that SurfaceView cannot be transparent
I tried overriding dispatchDraw() of a LinearLayout but that doesn't seem to be callable via a Runnable Thread.
I also tried to extend Canvas directly, but that quickly turned into a mess when trying to place it in my XML layout.
Do I have to use GLSurfaceView to have a drawing view whose background is transparent?
Optimally, I'd like to just have a Canvas on which I can draw Bitmap at various coordinates, instead of dealing with the details of GL (unless GL is the only way I can do transparency).
Silly Rabbit. Just use Views and move them around wherever you want within a FrameLayout. SurfaceView is not designed for what you're trying to do.
I'm going to try Switching from Canvas to OpenGL and toss in parts of TranslucentGLSurfaceView.
I'm still open to selecting an answer that doesn't require OpenGL, just for the sake of it solving my original question.

Categories

Resources