Is there a way to use vector graphics to be google maps marker? I know that google maps takes a bitmap for a marker, but I have a vector graphics (coming from server) and I am not sure how to display that as marker?
Thank you
Technically, if you are able to render your vector image as a Drawable, you can use it as a map marker. Vector drawables on Android are complicated but maybe you already solved that problem.
You need to programmatically create a writable Bitmap with the desired size (specify your constant size in dips to adjust it to the screen density), create a Canvas based on that Bitmap, set the bounds on your Drawable then draw it on the Canvas. Your Bitmap will then contain the rendering of your Drawable and you can create a Marker from it.
but I have a vector graphics file and I am not sure how to achieve that?
Open it in your vector drawing app and export as bitmap (PNG) to use on Android. This is the less painful way than anything else.
Related
Hi,
I'm trying to understand how to make custom view (with green background) on Android like represented on attached image. Please suggest, should I use Path for this or something else?
A good starting point would be http://tips.androidhive.info/2013/09/android-layout-rounded-corner-border/ . Then you can play with the attribute values to get the desired background. Hope this helps.
You have two good options for accomplishing this.
Bitmap
Create the desired image in Photoshop, Gimp or something similar. Export it as a png file then import it as a bitmap resource into your Android application.
This is by far the simpler and faster of the two methods, but here are two significant problems. First, you should supply bitmaps for each screen density bucket. Second, each bitmap resource you add increases the install size of your app. This can get out of hand quickly.
Path
As you mentioned in your question, you can draw the desired shape directly onto a canvas using a Path. More specifically, you'll use the cubicTo() method to create bezier curves.
I recommend using your image editor's "path" tool to learn how the control points affect the curve. Once you've drawn the shape in the image editor, write down the coordinates for every point on your path as well as the corresponding control points. Then do a little math to convert the points to ratios of the image's overall width and height.
You can implement the shape in your app as a View or as a Drawable. In either case, you'll be drawing to a Canvas. The important step will be to set up the path by multiplying the ratios with the size of your drawing area. After you draw the fancy edge, be sure to draw the straight edges via lineTo() and to close the shape with close().
How can I add styled round photo pinpoints similar to the pinpoints shown in the screenshot?
I'm building something like this now, but I don't like the number of "hacks" to achieve this.
Open source library or code sample will be helpful.
With v2 of the map library your possibilities are limited, since the custom drawing code basically has to be executed before you add markers to the map, even if those are not yet visible, while the previous overlay model allowed for a delayed drawing when a certain pin came into view. So in general you have to be very careful about memory, i.e. cache drawn bitmaps, and you have to look out / test for leaks in the v2 maps library when adding / clearing many custom markers.
The actual way I went down to create custom markers was the following:
create a custom drawable that encapsulates the drawing code for each of your markers in its void draw(Canvas) method
draw each marker drawable to a bitmap by creating a new bitmap using the calculated width / height of the drawable, attaching the bitmap to a Canvas and calling the draw method of the drawable with that canvas
create a bitmap descriptor via GMap's BitmapDescriptor.fromBitmap(bitmap);
add the bitmap to the MarkerOptions of the marker
Hope that helps.
I have an SVG file format in which there is 5-6 icon and I need to pick icon according to the requirement not the whole Image.
In Simply you can parse the .SVG onto your ImageView Or Canvas but this time I need to choose particular icon from the among.
Is this possible to do with the coding or i need to draw all icon separately ?
Somebody suggest me the way, how to make it happen ?
I did not try it but you may :
Create a Bitmap with the size of your icon
Create a new Canvas associated to this Bitmap
Draw your svg into the Canvas with an offset
-- If your icon coordinates are (X1, Y1)
-- Then when you draw an element substract SVG (X1, Y1)
Draw the Bitmap on screen.
It is a little bit homemade solution... maybe there is some libraries doing those king of things for you.
You could set a clip and translation on the canvas before drawing to hide all but the icon you want, but I think the best course of action is to use separate image files.
You don't say which SVG library you are using. If you are using AndroidSVG, you could add <view> elements to your SVG (one for each icon) and select the appropriate icon to draw using renderViewToPicture() etc.
I would like to show a zoomable and scrollable map-like SVG in my app. The only way to do this without writing your own library etc. is to use an existing library all of which seem to render the SVG to a Bitmap, which can be assigned to an ImageView, for example.
The underlying bitmap quickly gets very large which may result in an out-of-memory exception. How do I draw only part of a possibly large and/or zoomed SVG to a Bitmap? Scaling up a small bitmap looks bad and is not an option.
I am going to answer this question myself because i have been looking for an answer for a while, found it scattered all over the web (but not here) and think that it may be helpful for others.
Can you not use Google's svg-android library? You can scale the Canvas to the correct size before rendering.
Would this be like how Google maps works? They appear to tile and increase/decrease detail as you zoom in and out.
I have done something similar in the past, not sure if it is how Google did it but my scheme worked exceptionally well for me. What I did was break my map into rectangular regions, I would then calculate which regions had any portion that was visible and pull in data only from those regions. Each rectangular region was sub-divided to provide more detail if the user zoomed in. I kept subdividing down to the level I wanted, I stopped at three, because I used a 10 X 10 grid so each level was effectively a magnification of 10 - at level three I was viewing 100 times the detail I was viewing at level one which was sufficient for my needs.
I was even able to animate this so that when you zoomed in you appeared to smoothly "fly" closer to the terrain.
First of all, I am using libsvg-android which requires the NDK but which seems to be very fast, especially if you have to redraw your SVG frequently. Actually, I am using this modified version of the library which enables you to query the width and height of the SVG as specified in the SVG document.
My first approach was this:
long svgID = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(svgID, svgString);
SvgRaster.svgAndroidSetAntialiasing(svgID, true);
Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.RGB_565);
SvgRaster.svgAndroidRenderToAreaUniform(svgID, new Canvas(bitmap), -300, -200, 1500, 1500);
First, the SVG source is parsed. Then, a Bitmap is created that will be assigned to an ImageView later on. Next, the SVG is rendered (zoomed to 1500x1500 pixels in size) to the bitmap. Note the translation (-300, -200) applied because the user has scrolled the image view.
This does not work. It seems that negative offsets are not supported.
Instead, the canvas has to be translated like this:
Canvas canvas = new Canvas(bitmap);
canvas.translate(-300, -200);
SvgRaster.svgAndroidRenderToAreaUniform(svgID, canvas, 0, 0, 1500, 1500);
This works as expected. The translation of the SVG is applied to the canvas; the scale is applied by adjusting the size of the rendered SVG. In this way, you can have a large SVG document and render the proportion you are interested in to a much smaller bitmap.
I've written an ImageView subclass which is scrollable in both the x and the y directions and zoomable. Its image resource is a low resolution raster image of the SVG. When the user scrolls or zooms the image, this version of the SVG is shown. When the user stops interacting with the image, I render the currently visible part of the SVG to a Bitmap the size of my image view and draw this instead of the low resolution image.
In my application I would like to mark different spots on a map. What I'm now wondering is if there's a way to use a simple 9-patch image and add the spot's name as text to it or would I need to draw everything myself (including the text) in the draw() method of ItemizedOverlay?
As per this Q&A:
Drawing Nine Patch onto Canvas (Android)
you should be able to load the 9-patch using Resources.getDrawable, set the drawing bounds using Drawable.setBounds, and finally draw to the canvas provided in onDraw using Drawable.draw.
If you plan on reusing the drawable, you should keep an eye out for memory leaks, per this article.