I have app consisting of RelativeLayout, TableLayout and some buttons. Now I would like to draw a circle under point where user pressed on screen. One way to go is to extend View and add all the functionality inside, but I am wondering if there is any more elegant (general) solution?
I was thinking in way of transparent overlay, which you add to layout and it captures MotionEvent, draw circle on point of touch and passes event to underlying View component (so buttons are still clickable).
That way solution would be more reusable, as you can easily plug it in any project, but unfortunately I have no idea if it is doable that way. Any hints?
I'm pretty sure you can achieve what you want using GestureOverlayView.
Resources and examples are available:
http://developer.android.com/reference/android/gesture/GestureOverlayView.html
http://developer.android.com/resources/articles/gestures.html
http://www.vogella.de/articles/AndroidGestures/article.html#example
I dont know if my suggestion is the best solution,but I would do the following:
Create a new class that extends Drawable.
Override the onDraw method and in there draw your circles on the canvas.
Add a. addCircle(int x, int y); method to the drawable class which you will call each time you want to add a circle.
Then in you activity create a new objectof the above mentioned class and set it with context.getWindow().setBackgroundDrawable(your object) as your WindowBackground. Then where you handle your motionevent call this objects .addCircle method and your circle will be added.
Like this in your activity will be only several lines of code and it will be reusable.
Related
I want to have 2 views. My first one will be a GoogleMap and then in the four corners I want to add some graphics. I want when the users moves the map the images to stay in the four corners. I coouldn't find any tutorials on that and I do not know where to start searching so if you have any links or any proposal on what to do, inform me and I will come back with code.
extend the MapView class and override the dispatchDraw() method.
It gives you a canvas and you can draw on it as you please. Afterwards just send it to super() and you have what you want.
Propably you could use an overlay class. You can find many tutorials on overlay with Google. And just position the images like you want.
I have already created a circle with the use of a View and have not used SurfaceView at all. I want to create buttons which when clicked on show images from the drawables. But I have read on the net that a SurfaceView is required to allow UI elements to be placed on top. Is this true, can someone please help me, as I am confused on this.
Thanks.
It's not very clear what you want to do, but if you want to place UI elements on top of each other without using SurfaceView you can you a RelativeLayout, this layout allows you to have views on top of each other, do you can have an ImageView with a drawable appearing over a button for example.
If you just want to change the background/src images of a button when clicked (for example to create a 3d effect of clicking), you can check out selectors, these allows you to specify different drawables for pressed/normal states.
If you want to create buttons on a SurfaceView, I suggest you render Bitmaps that will represent buttons. You will have to programmatically check if the touch coordinates are in the bounds of that bitmap tough, to register a button click.
I hope this helps.
This is a snapshot from an app called "Noom Weight Loss Coach":
I was stunned by this circular view in this app. It can have some buttons (six in this snapshot but they can be more or less) and they can be rotated and have different colors.
I have a couple of questions:
Is there an existing library that provides this circular view?
If not, from where you would start if you want to build one? I am just interested in the circular look of the buttons. The rotation is not important.
I would recomend to use custom buttons. Derive your class from View, redefine onDraw() for drawing buttons state changes and listen for onTouch events.
I have a conceptual question to ask:
I created a custom dialog (extends Dialog) and I want to draw a chart (dynamic data, not static) in the top third of the dialog.
What's the best (only?) way to approach this?
A) Get a canvas to the dialog and draw to it? Seems like I need access to the dialog's draw, yes, or can I do this outside of the draw?
B) Subclass a view within the dialog layout (e.g. LinearLayout) and override it's draw and draw the chart?
C) Other? I've read that one approach would be to draw to a bitmap and then blt (or equivalent) to the canvas. This sounds closer to what I want to do, as once I create the chart, I have no need to alter it (no direct user interaction).
I haven't yet found any good sample code that deals with custom drawing in a dialog, so if I'm missing something, an example would be great.
Thanks much,
Rich
Solved.
My solution was a hybrid of B/C above. Since I needed access to the view's draw() method, I created my own subclass of an ImageView (e.g., MyView).
From within the draw(), I can get the dynamic size of the ImageView as it appears in the dialog. Given the size, I can now perform draws scaled to the custom ImageView size within the dialog.
I had to remember to use the proper custom view XML syntax in the dialog layout (e.g. "com.avaliant.dialogtest.MyView" to replace "ImageView"). And, of course, in my dialog class, I had to set to view to the proper view class:
MyView test = (MyView)dialogView.findViewById(R.id.test);
Quite easy once I understood WHY I had to do what I had to do :).
Rich
I've got a standard RelativeLayout laying out my Buttons and TextAreas. What I want to do now is be able to draw various sparks, flying cows etc. at arbitrary places on the screen on top of the whole thing. What's the best way to do this? Should I override onDraw() on the containing View and draw after calling super.onDraw()? Or is there some better way of drawing a layer on top?
You'll probably want to put your Relative layout and a custom view which isn't focusable and doesn't consume motionevents inside a FrameLayout, then override OnDraw in that custom view. That way you can call invalidate() on that view without making Android redraw everything else.