Drawing over my own custom View - android

I've created my own View (in which I draw on Canvas), and I want to make another Layer (with buttons etc.) on top of it. How can I do this?

Create a FrameLayout and add the views you want there - the FrameLayout allows for Overlaying of controls.
Just be sure that the top one has some opacity set, otherwise you won't see the controls below it - also be careful of the order you add the Views.
FrameLayout layers = new FrameLayout(this);
CustomViewBottom bottom = new CustomViewBottom(this);
CustomViewOverlay overlay = new CustomViewOverlay(this);
//the overlay class must set the ALPHA property
//of the PAINT object that it uses to draw
layers.addView(bottom);
layers.addView(overlay);
this.setContentView(layers);
How to draw an overlay on a SurfaceView used by Camera on Android?

Related

How to make a blur transparent linear layout

I have a transparent layout in android, and behind the layout there is an image. how to make the linear blur ? I found examples to make the image itself blur but I don't want to make whole image blue, just only the part that is behind the linear layout.
Set a semitransparent Blur image to the linear layout or simplest set a color to linear layout and set it to semitransparent by defining alpha
edited solution
do this...
1.) create a blur copy of the image u have on background.
2.) clip the image by using
Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, startX, startY, widthLayout , heightOfLayout);
3.) set this image in the Linear Layout using an image-view with height and width attribute as fill-parent.
I have pretty complex solution, so there won't be any code. So, here is idea, step by step:
Let's assume that your layout have just single custom LinearLayout. No ImageView as a background.
What we going to do, is draw background drawable of LinearLayout by our own, so it will first draw full image and then draw blurred square from the same image on top. Content of LinearLayout might be moved to desired position using paddings.
So, create something like MyLinearLayout and put it to your layout resource. Provide required constructors.
Override onAttachedToWindow() and onDetachedFromWindow() methods. Inside them we should load our background Bitmap and recycle it accordingly. Let's name it mBackground
Override draw() method. Inside it we're going to first draw our mBackground.
Then, you can use Canvas#clipRect() method to crop drawing area of Canvas to some specified rectangle. In your case, this rectangle should be the area below your content. You can figure it out using View#getPadding*() methods. Don't forget to call canvas#save() before clipping drawing area.
Now you can draw your bitmap once again with blur (I don't know which method exactly you're using, so let's assume that you know how to do it... but you still can share it with us :) ). Cool thing is that you can just draw the same Bitmap once again in full scale - since we had called clipRect before, it will be drawn only within this area. Don't forget to call canvas#restore() after drawing background.
Call super.draw() to draw rest of the stuff, that your LinearLayout contains.

RelativeLayout onDraw

I create a subclass of RelativeLayout,the use of it in the layout file.In the layout file also defines some components widget.Overwrite the OnDraw painted a picture,The picture can not draw on the top, but the control block.
Like this:
How do I get the picture to the layout of the above.the picture is and move according to the finger movement..thanks.

Overlap larger than screen image on top of MapView

I have a big image with transparent circle in the center. I want to overlap this over a MapView so that you get to see the map in a circle. The image is bigger than any possible device screen so that it will look basically the same on all sizes. The rounded corners in the image are complex and that's why I need to do this.
How can I position the ImageView so that parts of it are offscreen?
Make the transparent circle a ninepatch, for example: http://i.imgur.com/8wmrQ.png
Save it into res/drawable-hdpi/mask.9.png
Than you can use it as
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = new RelativeLayout(this);
MapView mapView = new MapView(this, "apikey");
mapView.setClickable(true);
rl.addView(mapView);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mask);
imageView.setScaleType(ScaleType.FIT_XY);
rl.addView(imageView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(rl);
}
I suspect that what you want to achive is not exactly what you describe. However, if that is the case the solution posted by #Daniel Fekete is the best approach. To that solution, I just add, that you can also define the ImageView in the same layout file where you are defining the MapView, instead of adding the view programmatically. Of course the ImageView should be defined after the MapView to have it being drawn over the map.
If you need a more flexible solution, where the ImageView can move aligned with the map, or having it covering the map, but not covering other overlays items that you add to the map, then you can use the the following approach:
Create an overlay that mask (cover) the part of the map that you want to cover. In the onDraw() of this oevrlay just draw something that achive your mask requirements (shapes, colors and opacity).
If you want to have other overlay items visible, you need to add the overlay with these items after you add the overlay that mask map to the mapView.getOverlays.add().
Enjoy it.

Using ViewGroup and View on Android draw and make buttons using canvas

I'm trying to make my own custom view, currently all it does is draw an image on a specific x and y coordinate and then draws the similar images repeatedly on different locations.
I want to be able to create a button on each instance of the image that is drawn. if one image is clicked it will have cause something different to happen depending on which image is chosen.
How can I implement this?
would I have to create a different view for each image/button combination and then set an onClick event?
Let me try to be a little more clear
I'm trying to make a map using hexagon (different types of terrains for different players)
I've figured out how to get them to draw (see here - they will have a border to show what terrain is owned by whom)
I just made a custom view class and had the hexagons drawn using a Canvas; however, I'm not sure how to be able to make the hexagons into buttons so that I can differentiate between which hexagon was chosen and how it should react to the opponents spot.
I was thinking of making a ViewGroup called Terrain to contain the Nodes(hexagons) that belong to the player and have a group of Node Views that only draw the hexagon where it should be located.
the question is can i make each node or the entire viewGroup into a button (or do an onTouch ) if a certain hexagon is pressed?
If I understood well, you should do :
One template my_pictures.xml which is a simple button.
Then you create a custom adapter which as a function for each kind of image you wanna create. By this, I mean that you will have to change the background value of your button depending on what you need it to be. Then you do a notifyDataSetChanged(); to refresh the container with the new button.
For the Listener, you just have to add it either in your activity or your adapter when you create your buttonImages, I don't know what's better.
Thanks for your help!
I figured out what I needed to do.
I have a NodeView class and in my GameActivity class, I'm using a relative layout and setting the layout parameters of where I wanted things positioned
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10;
params.topMargin = 10;
params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10+95*x;
params.topMargin = 81+(71*y);
rl.addView(new NodeView (this,0,0,1,1), params);
This helped me add things where I needed them and now all I'm trying to figure out how to scroll around the territories on both the x and y axis (I tried ScrollView but that only allows me to scroll on the y-axis, I'm looking into it though)

Android GLSurfaceView with drawable background

I have a GLSurfaceView with a drawable as background, however only the background is visible when rendered without surfaceView.setZOrderOnTop(true)
I need to avoid using setZOrderOnTop(true) because there are static TextView's being used on top of the GLSurfaceView.
Any suggestions for getting this to work?
GLSurfaceView cannot really have a background. The way a surface view works is by cutting a hole through your Activity's window and showing another surface behind. Setting setZOrderOnTop(true) moves the surface above the Activity's window.
false! You can do it.
Just an example to show how it works.
glRenderer = new OpenGLRenderer(context);
view = new GLSurfaceView(context);
view.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
view.setRenderer(glRenderer);
view.getHolder().setFormat(PixelFormat.RGBA_8888);
view.getHolder().setFormat(PixelFormat.TRANSLUCENT);
view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
Then use framelayout to put a view under glsurfaceview!
Try setZOrderMediaOverlay(true); which should put the gl layer above the background but below the window.
Well, since GLSurfaceView is just another view, why not use a FrameLayout to place the GLSurfaceView on top of the ImageView (or whatever you're using to display the drawable.)

Categories

Resources