Adding variable-size labels on top of MapView - android

I need to add 'balloon'- like markers to my map view. The difficult thing here - these markers include a variable-length text labels so these balloons have variable sizes. Actually this is exactly what implemented in native google-maps application to show labels.
Any idea what is optimal way to do this?

You need to implement custom overlay. This article will be a good start.

You are probably already using itemized overlays. Your OverlayItem class has a method that returns a Drawable; you can create your own Drawable class that your OverlayItem will return. As for the variable size, you could handle that two ways: either your Drawable could determine its own size (using Paint.measureText() ), or, possibly, you could create a View class to take advantage of a View's ability to figure its own layout, and have your Drawable's draw() method delegate to the View's draw() method. Someone who understands Views better than I do can probably improve on that suggestion.

Related

How to create a custom view in android

I want to learn, how to create custom view. I have gone through multiple tutorial and didn't get what i want(I mean concept).
What i have learned
I am sharing my concept,if i am wrong please correct me.
If you want to make a minor change to the standard view then inherit itself(e.g extends seekbar) and customize it.
But if you want to make difficult or multifunction view, We should inherit View Class
When we inherit a View.
Two methods are neccessary
onDraw(Canvas canvas) // it call each time it and also can be call using invalidate.
onMeasure // it measure the actual width and size and set our viewit accordingly
Whatever you want to draw on View can be done using canvas;(Am i right).
To define additional attributes create an attrs.xml file in your res/values folder. and add <declare-styleable name="YourCustomView"> and add attributes to it.
get these attribute in the constructor of the View using TypedArray call recyle method.
My Confusions
can we draw something without using Canvas
can we draw ImageView without converting it to bitmap.
I simply want to create a custom imageView which can be rotated like
a dialView but it should rotate half(semi-circle) and also want to
draw its progress outside it. using canvas.drawArc()
What i want to achieve
I want to create my own custom knob, I have searched our google but didn't find what i want see my questions here
First Question
Second Question
Thanks in Advance....
1)You can use openGL. Otherwise you need a canvas. The canvas defines where you're drawing to, without it (or the openGL equivalent) we don't know where to draw. You might not want to draw directly to the screen, its common to draw to bitmaps in many cases.
2)Your question doesn't make a lot of sense. Why would you want to draw an ImageView, other than by letting it draw itself?
3)Easiest way to do that is to use a normal image view and call setRotation to rotate it as needed.

What is the best way to draw arbitrary objects in Android?

I've recently started building my second android app and I've hit a wall. What I want to know in general is this. In almost all other programming languages that I've used there is a simple way of drawing arbitrarily on screen. Usually there is some sort of Scene or Canvas type class and another GraphicObject type class. So If, say, I want to draw a car, I create a class that descends from GraphicObject and define they it draws itself and then add an instantiation of the Car class (the object) to the Scene. If I want a second car, you just instantiate another and put it wherever you like, by setting some kind of coordinate function. If this is a program where what is drawn depends on user input, everything must be done programatically and hence it does not depend on anything other than the code written.
How do you achieve this in Android? As far as I can muster the only object which you can define the way it is drawn is a View. However I have not been able to simply take my custom view (that draws a rectangle in my case) and instantiate two of them a put them in the screen where I want them, and do this in a programtic simple way....
Thank you for any advice.
Well if I want to draw lets say 10 rectangles then I wont instantiate my custom View 10 times. Instead I will add logic in my custom View to be able to draw 10 or more rectangles. Anyway if you want to draw again what was drawn in a View then you can call setDrawingCacheEnabled(true); on your View and then call getDrawingCache() to get Bitmap to draw it somewhere else.
Its almost same in android also,
All the widgets are inherited from class View, by extending this class you can actually create whatever you want limiting by only yours imagination, you have access to canvas here, use it along with touch controls exposed from class itself to override all the behaviours you want as per your need,.
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/graphics/Canvas.html

How to draw a shape in Android as one of other layout objects?

I've been working on this for a while but can't find anything that exactly addresses my question (at least not something easy to understand).
I have a main layout XML file where I define various layout objects like a Button or a TextView (and I know I can add SurfaceView, View, and view and other things too). I want to draw a shape (in my case it's an arc) in just one of these objects so it doesn't take up the whole screen and so I can position it relative to other things.
(In my case it will ultimately re-draw the arc kind of like a circle with a gap in a different position every time I call a method depending on a value I pass to the method, but that's separate from my basic question.)
I know the answer will have something to do with a canvas, an onDraw method, maybe Paint, probably a view. I have been able to draw a circle from a custom View object by setting the main java file's layout as that View (as opposed to R.layouts.main), but that takes up the whole screen, and I'm unsure how I might be able to have that dynamically draw with modifications.
A really clear explanation or better yet an actual example would just be awesome.
As i see it u need to draw a specific shape on widget and not on complete screen. Try using layer List.
you can refer this link for sample Link

Android - How to overlay images in a WebView?

I'm trying to overlay several images[to form a building plan with possible zoom].
The point is to choose which images to averlay each time depending on selected filters[staff, equipements ...].
After doing researches, I implemented the zoom using a WebView but I didn't find a way to make this webview take different images at once [which is possible with ImageView using LayerDrawable].
Is there any way to achieve this ? or should I just stick the ImageViews and find a way to implement Zoom in it ?? I hope my question is clear and not too stupid since I'm just a beginner :).
Thanks for your help!!
While you can stack several ImageViews on top of each other, if you want to make this efficient and feature rich (pinch zoon, panning) you need to create your own component, either based on View or based on SurfaceView.
Personally I think SurfaceView is an overkill here, subclassing simple view should be sufficient. You need to manually draw your layers from bitmaps on canvas on onDraw() method of the view.
Then you can use GestureDetector or ScaleGestureDetector to add zoom by scaling your canvas via affine transformations.
Use a RelativeLayout for starters.
You could use the ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params) or a variant along with ViewGroup.removeView(View view) or ViewGroup.removeViewAt(int index).
This would obviously require you to inflate the views manually using LayoutInflater but you could keep a global reference to each after inflating and just flip between the two

Add a layer or a background on a mapView

I have a particular need with a mapView and i can't find any good solution.
I have a Mapview with some markers displayed on it, and it works perfectly.
My need is that i want to put a kind of layer/background on the screen, in order to "mask" the map, but i always want to see the markers, and keep all the mapview functionnalities (zoom, move, etc.)
Is there any solution available to do it?
I hope you understand my need.
Many thanks in advance.
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 (areas, colors and opacity).
To ensure that markers are always visible, you need to add the overlay with markers after you add the overlay that mask map to the mapView.getOverlays.add().
Enjoy it.

Categories

Resources