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.
Related
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
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
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.
How should I create this custom View? The actual slider doesnt need to slide, it is set once programatically and then just stays in that position. I was thinking that even if I extended ProgressBar I would still need to do some maths in order for the arrow to appear directly over a notch, so maybe drawing each Rect onto a canvas and then the thumb above in the correct place would be a better approach? Thanks in advance
Suppose I have an Android View that is being partially masked by another View. For instance, imagine that the masking view has a transparent rectangular "window" in the center of it, and my view is centered beneath the window and possessing dimensions that are larger than the dimensions of the window. In such a case, some rectangular portion of the view will be visible, with the rest being obscured by the mask.
So my question is, is there any straightforward way to retrive the position and dimensions of the visible area without having any access to the masking view itself (so without knowing how big the mask's "window" itself is)?
I tried calling getLocalVisibleRect(), which sounded promising, but that only seems to return the view's layout rectangle and not the rectangle that is actually visible.
This is pretty late to the game, but I suspect that getGlobalVisibleRect does what you want.
I have used getWindowVisibleDisplayFrame method to determine if view is partially visible or not to determine if a soft keyboard is open. You can try it out.
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
Does this not work: http://developer.android.com/reference/android/view/View.html#getDrawingRect(android.graphics.Rect)
The documentation says that it does what you want.