Android - Make make surfaceview larger than parent - android

I have a 'Portrait mode' tabbed based application that contains a SurfaceView (with a camera preview on it) within one of the tabs. I have been creating my camera code from the Android API Demos and I have it all set-up working correctly bar one thing. The resulting camera preview is stretched and scaled making the preview look off.
Ideally I want to render the SurfaceView at the native preview size defined by the camera within the tab. However that would involve making the contents of the tab larger than its parent! (Or rather larger than the FrameLayout the tab activities are contained in).
How can I go about Achieving this?
Image of problem:

Make the FrameLayout the root object, then pile the camera view in it, then header and the tab widget. You may have to do some fancy work with gravity settings and stacking the header/footer in linear layouts but it definitely is possible.
aka:
frame.addView(cameraPreview);
frame.addView(header);
frame.addView(footer);
This will draw the objects in layers.
EDIT: If you don't want to use your current frame layout, just make another one. Stack the header, frame layout, and footer inside a linear layout. Create a new frame layout, add the linear layout to it, then add the preview onto it to draw over it.

Related

How do I add standard scroll bars to a SurfaceView?

I got a SurfaceView, in which I display a bitmap that is much larger than the actual area of the SurfaceView, so I have implemented a way for the user to slide their finger on the bitmap and move it up and down. When doing so, I would like to be able to display a vertical scroll bar, preferably the standard Android scroll bars, instead of drawing something custom.
The thing is, I would like my SurfaceView to stay the size of the screen, that is I don't want to scroll the SurfaceView itself, I just want to create the illusion that the user is scrolling the contents, and therefore display the scrollbars.
I tried setting android:scrollbars = "vertical" in the layout's XML, I tried mSurfaceView.setVerticalScrollBarEnabled(true); and I tried awakenScrollBars(); whenever the user touches the SurfaceView, however none of those displays the scrollbars.
So, is it possible to display the standard Android scrollbar on a SurfaceView?
short answer is: no!
everything that the SurfaceView ever draws on the screen is whatever you directly call to be drawn using the whole lockCanvas and unlockCanvasAndPost that you know about.
Putting those parameters in the XML make it possible for you to read them in Java via the AttributeSet in the constructor, but that's only for configuration, it won't drawn anything.

android app with opengl navigation element, background image and video

i have done a view apps with setContetnView and the xml layouts... Actually I do some steps in opengl. Now i want to have one background image, a video area an the opengl 3d navigation element beside the video area to control the videos.
Is it necessary to do the whole work in opengl, or is it possible to put the opengl part over an existing view?
Where can I find more infos on this?
You can place the OpenGL view over top of the other views. I worked on an app that placed a OpenGLView over top of a VideoView. There may be some performance issues, so I would recommend adding the hardware acceleration attribute to your AndroidManifest.xml (under the application element):
android:hardwareAcceleration="true"
One thing that you will need to know to get the layout you expect is that the SurfaceViews do not conform to the normal view hierarchy, so you will want to set the z-order of the view programmatically, using the method setZOrderOnTop:
mVideoView.setZOrderOnTop(false);
mOpenGLView.setZOrderOnTop(true);

Which Android layout and view to use for simple image-based game?

I would like to make a simple Android game where a large background image is displayed and some other images are displayed in specific locations over it, where the other images may be clickable.
Here's a quick sample image of what I'm talking about:
The user should be able to tap the soccer player or the moose (ah, the classic "soccer player moose problem"!)
How should I render this screen (which layouts and views?) so the user can interact with it and it will scale properly on different devices?
I would use a RelativeLayout.
You can set the you background image to the layout (fill_parent for height and width).
You can then put your ImageViews, containing your moose and soccer player down on the layout relative to the top or sides of the sceen, or relative to each other (making sure to specify "dp" units for everything). Set the backgrounds of your ImageViews to be transparent, and there won't be a bounding box problem (and/or you can also set your ImageViews alignment to be relative to each other, ensuring they don't overlap).
I think this is the simplest way to do this - it is then super easy to attach onClickListener to your ImageViews in your Activity, and you are done.
This type of layout will work the same on all devices and screen sizes.
There are some small gotcha's with RelativeLayouts, but they are pretty simple once you get into them, and provide fast rendering (since the view hierarchy is usually shallow). Good Luck.
ImageView for the clickable elements seems like a fine choice to me. For the background I would just set your image as the background of the parent layout i.e. RelativeLayout
SurfaceView for the whole thing (with your field as a background) and regular *ImageView*s for added elements. You can easily recover the click coordinates from the SurfaceView and thus know what element has been touched.
SurfaceView might offer you additional possibilities anyway.
For most images, I'd use an ImageView for each one, like FoamyGuy said.
If they're close enough for overlapping bounding boxes to be an issue, you can still use an ImageView for each, but with a variation of this answer, testing alpha for each ImageView in range.
I would agree with both FoamyGuy and Booger that if your only goal is to place static images onto the screen that do something when you click them, RelativeLayout and ImageViews all the way.
But...
If you are looking to randomly spawn multiple images onto the screen in intervals and have them move around for the player to interact with while explosions are going off and maidens are being kidnapped, you should look into SurfaceView, Canvas, Drawable, TouchEvents, and FrameBuffers.

How can a SurfaceView be put into a layered hierarchy of Views?

I want to create a hierarchy of Views in layers one on top of the other that is, roughly:
FrameLayout {
SurfaceView
View
View
SurfaceView
View
SurfaceView
}
I want each SurfaceView's draw events to appear in the correct position in the hierarchy. For example, the top surface should appear in front of everything. The bottom one should appear behind everything.
Unfortunately, I can make each surface appear in front of everything using setZOrderOnTop(true) and I can make each surface appear at the bottom by NOT doing this, but I can't make one appear in the middle. How would one do this?
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
I suggest you use a LinearLayout or RelativeLayout instead.
At the time of writing, this appears to be impossible.

Android Canvas only take up only part of screen

I am trying to draw 3 lines real time, whose co-ordinates are constantly updating. In this link, How to draw a line in android, it is explained how to draw lines, but they take up all of the screen. How do I get it to take up only part of the screen, and let my layout main file take up the rest. Is this possible?
Thanks,
Rokky
If you wish to dynamically create the DrawView, as your example shows, 1cCreate a view (perhaps a FrameLayout) within your layout xml which will hold your canvas, and use standard Android layout techniques to make the FrameLayout be the right size.
Alternatively you can create the DrawView statically by placing it directly in your layout; How to add CustomView to Layout fromXML? shows how to do that.

Categories

Resources