I have an application with a SurfaceView and a MapView. They are displayed in a single view, like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="XXXXXXXXredactedXXXXXXXXXXXXx"
/>
<class.that.extends.SurfaceView
android:id="#+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
/>
</RelativeLayout>
Note the MapView comes before the SurfaceView. Because of this, the MapView is drawn "under" the SurfaceView, and all that I see is a black background, with the things that I draw on the SurfaceView showing. If I swap the order, and put SurfaceView first in the file and MapView second, the MapView is drawn, with none of the SurfaceView showing.
According to the SurfaceView Javadoc:
The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it.
To me, this means that I should be able to put the SurfaceView first in the file, and have it "punch through" the MapView and display what it has. But, as it is, they seem to be two views, and only one is visible at a time.
After thought: I tried setting the background to #00000000 so that the alpha of the background would be zero (i.e. transparent), but it doesn't have any effect.
The alpha setting alone won't really help in this case.
When you setup your views, set the PixelFormat of the view's SurfaceHolder:
glsView = (GLSurfaceView) findViewById(R.id.surfaceview);
glsView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
I ended up abandoning SurfaceView, despite it's wonderful features for drawing smoothly. Now I'm just using a regular old View, overriding its onDraw method. The MapView underneath is currently being refreshed every time this new View is drawn, but I'm looking in to using the drawing cache feature of Views to simplify that.
Related
I've a surfaceview to do some drawing stuff. My view supports zoom in/out feature. Now when user zoom in/out by finger gesture, I want to resize the surfaceview, I've tried resizing by changing surfaceview's layout params. Although this works, but it's slow, and while doing the resize work, I see some big jumps (lags), Which is no usable. Here, I want to know how can I resize the surfaceview efficiently (without that jump)?
Here sniped of my view structure,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_above="#+id/bottomMenu"
android:layout_height="match_parent">
<MySurfaceView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout> ...
And the params changing code,
ViewGroup.LayoutParams params = surface.getLayoutParams();
params.height = (int) (750*factor);
surface.setLayoutParams(params);
Well in the end I found a method of surfaceHolder to set the canvas size fixed,
holder.setFixedSize(canvasW, canvasH);
With this method I'm able to resize layout with layout params, without any kind of lagging. Here, I need to know the canvas size before setting it up, so I added my SurfaceView from Java code with predefined size and it's working smoothly.
**I'm not still sure, why setting up canvas fixed size done the trick. You are welcome to explain a bit (may be edit this answer).
I am trying to add a 'target' which would be represented as a circle which must always remain at the center of the current map view. Looking through the API I see a lot about adding Markers, Shapes, Ground overlays and Tile overlays but none seem to be adaptable to my needs.
Basically they are all used to draw elements 'on the map' at specified locations. What I need is some sort of overlay for the actual camera view. A good example would be when looking through the lens of the camera where a circle denotes the center of the frame.
You can use a RelativeLayout, which contains the MapFragment plus an additional custom view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:id="#+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
<mypackage.MyCustomView
android:id="#+id/mapOverlay" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#android:color/transparent" />
</RelativeLayout>
Whatever you draw into the custom view, e.g. a circle in the center, is drawn on top of the map, and will stay there, independent of the maps movement.
If you want to adapt the shape to the zoom, tilt, etc. you need to listen to camera changes and adapt the drawing accordingly. Unfortunately the CameraChangeListener is only called, after the change is finished. So you can not immediately follow the zoom or tilt gesture with your drawing.
I tried to match LunarLander's implementation of a SurfaceView style of drawing.
I am starting really simple, and just trying to draw circles underneath the user's touch input, but I can't seem to draw anything! All my app gets is a green screen (which is the background color of the view in the layout xml).
In an attempt to draw something, I have tried drawing a black rectangle on the entire canvas, a circle at 0,0 and a bitmap image. None of these draws succeed. They all get called, and none throw exceptions, but the screen never changes from green.
I am at wits end trying to figure this out so here is my code (minus the game engine, which is irrelevant at this point).
The layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jknight.particletoy.view.GameEngineView
android:id="#+id/gameengineview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF66FF33"/>
</FrameLayout>
EDIT:
Figured it out:
You cannot have a background color property in the layout for a surface view. It was rendering overtop.
I'm developing an app that shows a camera preview on a SurfaceView but I only want to show the top half of the preview.
Is there a way of cropping/clipping the view or will I have to resort to obscuring the bottom half with something like an image,other view or drawing?
regards
Simon
I am currently using this method to do the cropping (basically using the clip child ability of an absolute layout to clip a surfaceview child)
<AbsoluteLayout android:clipChildren="true" android:layout_alignParentRight="true" android:layout_width="240dp" android:id="#+id/absoluteLayout1" android:layout_height="80dp">
<SurfaceView android:id="#+id/swSurfaceView" android:layout_width="240dp" android:layout_height="160dp" android:visibility="visible"></SurfaceView>
</AbsoluteLayout>
Overlaying the SurfaceView with another view is the easiest way to do it... however, it's quite ugly if you ask me.
Why don't just set the SurfaceView height manually? Make it take the 50% of the screen space and it will do what you want. You don't provide code so we'll have to do some guesses: let's suppose you have a LinearLayout, then inside it you put two views: your surface view and another kind of view. Then, you set their height to be 0dip, and their weight to be 1... that should do it.
I have surfaceView class that is modeled after this, I populated it with 6 bitmaps. Is there a way to wrap the surfaceView around the bitmaps, instead of it covering the entire screen.
I have try this in my xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<com.android.customclasses.Panel
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</com.android.customclasses.Panel>
</LinearLayout>
but this does not work. it just fill the parent.
Thanks.
I don't think that SurfaceView has a notion of "wrap_content" (other than perhaps interpreting it to mean "fill_parent"). You would have to override onMeasure in your custom class Panel and call setMeasuredDimension with the appropriate size.
If you think about this it makes sense: your bitmaps are not inside the SurfaceView in the view hierarchy. They are simply being drawn by graphics commands to a SurfaceView which has a predetermined size.