How to draw Custom View Extending Relative Layout to a canvas - java

I have my main activity which creates an instance of SurfaceView. In the SurfaceView I am creating several simple objects from bitmaps. I pass a canvas element to them (objectX.draw(canvas)...etc) from a render method in the SurfaceView. all of this is working as i would like, however I would like to do the same with a custom RelativeLayout.
For example i would like to create a custom class extending RelativeLayout and fill it with various objects such as ImageView etc and then from the SurfaceView call a method which draws this to the canvas.
I have been looking for tutorials to help me understand how to do this but cannot find anything. I understand that I have to draw a bitmap of the RelativeLayout and then in turn draw that to my canvas which the surface view is passing in to it. I think a lot of my issues may be coming from the fact that i am not trying to ever add the Custom RelativeLayout to the screen, instead I would like to create it by inflating an xml layout and simply drawing to a passed in canvas.

Related

Drawing line or connection between Views in Canvas

I am trying to achieve something like in below image. I have several Buttons around a View and would like to connect them using Canvas on their background. I am able to place the Buttons and View in a manner required for my project, but I've no clue how to place a Canvas behind it and connect all of the Buttons to my View.
I've no clue how to place a Canvas for such requirement and how to find the Button's & View's center co-ordinates in Canvas so that I can draw line between View's Coordinate and Button's Coordinates.
For this, you will need a custom ViewGroup class (please refer to this doc).
You could also just start right away with Linear or RelativeLayout as a superclass, this way you will have all the chil-positioning logic ready for you.
You will need to override the dispatchDraw method. Inside dispatchDraw(), iterate through your children and draw your lines on the canvas. Please tale care to draw the children after you draw the lines.

Can I lay a fixed layer over a custom canvas?

In Android Studio, I'm developing a game in which I draw my animation at 60fps using a SurfaceView object. I use a semitransparent image as the top layer to hide the edges of the playing field -- sort of a "fogs of war" effect so that the player does not know what to expect beyond the shadows.
My problem is that I need to draw this shadow image for every frame, even though the shadow never changes or moves, even when the playing field scrolls right/left/up/down. And it's a full-screen image, so it slows the game to a crawl.
Is there a way to create a one-time overlay ONCE on top of the animated custom canvas? I also want to interact with the canvas beneath it, as if the overlay was not even there.
Have you tried putting a full screen ImageView over your surface view and setting the "fog of war" image to this, should draw it once (this is assuming the background to your "fog of war is transparent").
If you need anything fancier then ViewOverlay should cover you, but my understanding is this is used when individual views wish to draw outside their bounds for animations, as you are using full screen surface view this should be unnecessary
https://developer.android.com/reference/android/view/ViewOverlay.html
Also no reason why you can't stick a surface view over a surface view. One for the fog of war / screen effects, one for the game surface.
Based on the response from Thomas Stevens, I researched my options for overlaying my SurfaceView with an ImageView. My solution is to add a FrameLayout that contains the needed ImageView. (That way, I can add additional views to the overlay simply by updating the layout XML.)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//temporarily set the view to the overlay layout to gain access to the frame
setContentView(R.layout.overlay_game);
FrameLayout overlayLayout = (FrameLayout)findViewById(R.id.gameOverlayFrame);
//set the final view to the active game surface (a Java-coded SurfaceView object)
setContentView(new GameSurface(this));
//add the overlay view on top of the game surface view
addContentView(overlayLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}
I don't claim this to be THE solution...just the one that works for me.
I created two GameViews in the same layout, one for the scenery and one for the objects

How to create a custom Android view? Using XML objects or a drawing API?

I'm trying to create a new view that would:
load an image
allow the user to zoom and rotate the image with two fingers while a "cropping box" stays translucent over the top while the image extends beyond the "cropping box"
with the end goal of being able to mark the rotation and position of the main image (so that later i could crop out or show the area within the cropping box)
I'm wondering if I can do this with an ImageView holding the image in the background and another ImageView holding the crop box on top of it, then using TouchEvents to move the image, would this work?
Or do I need to use some drawing API on a 2D surface?
I'm just really new to creating custom objects in Android that aren't just customized out-of-the-box Views.
What's the standard way of creating something like this?
Thanks!
Create a Compound View by extending RelativeLayout with two overlapping ImageViews in it. Then override onTouch event or override onGestureDetector interface in that newly created View.
Official Documentation: http://developer.android.com/guide/topics/ui/custom-components.html
Another Tutorial: http://www.vogella.com/tutorials/AndroidCustomViews/article.html

Should I draw using the View or Canvas class in Android?

I'm developing for Android and am a little confused about what is the best way to write 2d full-screen games. On my desktop PC version of my game I create a class which extends the Canvas class and go from there, overloading the update() method to draw to the screen. My intention is to port it to Android.
However I've noticed some online tutorials don't use Canvas, and use View instead. I'm used to using Canvas and drawing with Graphics objects using drawImage(), for example.
Is there a best (i.e. fastest, most accepted) choice out of the two (Canvas or View) or doesn't it matter. Perhaps one extends the other anyway?
A view is your base widget, think of it sort of like a blank panel to which other widgets can be added, or with which you can implement your own widgets. A view has a draw method you can override which takes a canvas as a parameter. You do your rendering in that method. The draw operators you are looking for should be available from the Paint class, which draws to a Canvas.

Combining a Canvas and OpenGL ES Renderer?

I have an app that uses a Class with an extension SurfaceView to draw to a canvas, but i'm looking at moving over to openGL. I noticed that if I merely change the SurfaceView extension to a GLSurfaceView, the app still runs the same. I've also been able to start basic drawing in another app using openGL ES.
I took it the next step and instantiated another class for the renderer in GLSurfaceView, and I instruct the render to draw a triangle. This all compiles and runs fine, but it runs exactly like the original App, with no triangle rendered, but the rest of the canvas draws properly.
I can't think of why it's not rendering, or why it wouldn't render. Or maybe it is rendering, but it's simply being overridden by the canvas?
I know it seems odd to try to use both methods, Since my app is live, I would rather implement switching the rendering over to GL at my own pace so that it doesn't take me a month to get the next update out.
So Anybody ever tried do do this? Run a Gl renderer OVER a canvas?
You cannot use both OpenGL and a Canvas to render on a single SurfaceView. You can however put another View on top of the SurfaceView to achieve the desired effect.
Try the following:
addContentView(SurfaceView);
addContentView(GLSurfaceView);
or create a relative layout with both views.
Here my sample code when adding canvas over the OpenGL view, thanks to #Audrius Butkevicius !
mGLView = new MyOpenGLSurfaceView;
// SET OpenGL View
setContentView(myOpenGLView);
mCanvasOverlayView = new View(context)
mOverlayViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// ADD Canvas view overlay
addContentView(mCanvasOverlayView, mOverlayViewParams );

Categories