Android: using 9-patch and text for ItemizedOverlay on MapView - android

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.

Related

Android canvas: Is it possible to add images from gallery?

In some paint apps or note taking apps, they will allow you to add text and images in addition to drawing stuff.
Now I know that I can create a custom view and override on touch events and build paths and draw them on canvas.
But what I don't know is this: In apps like mentioned above I am confused if they add images as image views or they add images directly drawn on canvas.
I mean do they create new imageview and set the gallery image to it, or they simply add the image directly to the canvas.
Any thoughts?
Thanks.
They draw it directly to the Canvas. Notice the Canvas class (which is how Views draw) has a drawBitmap and drawText functions. Generally in a drawing app the entire area you draw on will be just 1 view.

Drawing to a new Bitmap and then using that in my onDraw

I would like to display several rings with different coloured sections (see below). The colour of the sections, however, cannot be know in advance, so I will need to draw these dynamically.
I know I could draw directly to the canvas but, once I have them, I would like to animate these rings, rotate them, have them overlap etc. It seemed, therefore, that the easiest and possibly least expensive approach would be to create them in advance, in memory, as transparent pngs and then just draw them in onDraw.
My problem is the only methods I can find to do this are setPixel. Is there not a way I could use drawing tools, like in Canvas, to draw to an empty bitmap, once, then use that bitmap with my canvas in onDraw?
I feel like I am missing a piece in the puzzle. Any help would be greatly appreciated.
You can create a Bitmap that is the size you want the ring to be and then create a Canvas the same size. Call setBitmap() on the Canvas and it will draw on to that for you. Then you can build your circle and have a bitmap to hold onto and use just like any other resource.

Best method to dynamically draw static image without pre-defined size

I am writing an App that need to render an image if a user selects an option. The image will read a data file and according to the contents of the data file render an appropriate amount of rectangles, lines and text (I'm visualizing data). I read this site Drawables or Canavs and if I understood it correctly it says I should use drawables. But what I don't understand (haven't found where or missed it) is where I define the space to draw shapes in. As I do not know the number of shapes and thus the size of the entire visualization until I read the data file, i do not know how large of a composite image (size required to render all of the shapes and lines) I need.
So I have three questions.
Should I use drawables to accomplish what I need to do?
Does the mDrawable.setBounds method define the 'size' of the visualization I am working in
Can I draw lines and insert text with drawables?
To show a Drawable you would use an ImageView with the source set to a drawable, or some other UI component with the background set to the drawable, or override the onDraw method of a custom UI View of your own and use the Drawable#draw(Canvas) call to draw the Drawable to the canvas and thus the view that is using that canvas to draw itself. The Drawable#setBounds method defines where on the canvas the Drawable#draw method draws to.
Generally, in your situation, Drawable wouldn't be used at all. Rather you would just subclass View, override onDraw(Canvas), and make your drawing calls directly on the Canvas object. This object has drawBitmap, drawLines, drawText, etc. methods:
http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String,%20float,%20float,%20android.graphics.Paint)

Android: Custom masking 2 bitmaps layered one above the other

easy now.
What I want eventually:
I want to have 2 bitmaps overlayed on a view. Same bitmaps with the one above have higher brightness than the below one.
Now when the user strokes(with touch event (like paint brush)) on the upper bitmap, I want those parts of the upper bitmap to go invisible.
For those who are familiar with adobe photoshop perhaps this will make more sense:
I want to draw a mask on an image being display so that only the unmasked parts remain visible. But the mask can be drawn from a brush with variable hardness/size.
How do I achieve this functionality? Direct me in in the line where I should research or give sample code.
Also, is it possible to draw strokes on an imageview with a brush which has variable hardness? I know that I can drawPath and drawArc on a canvas, but I do not know how to achieve the different brush strokes/styles.
Please pardon me if I haven't phrased my question right, or wasn't able to find similar duplicates.
You can use FrameLayout to overlay one image over other in Android and for Custom Masking search FingerPaint on google.
I think the best way is to do your own off-screen compositing, then render the composited image using an ImageView or perhaps a subclass with custom interaction. See this sample code for an example of how to do such compositing using the Porter-Duff transfer modes.

improve draw background performance

I'm drawing a background (a grid) in a View, basically all the screen filled with triangles (note I need to draw the triangles), but it seems to get the application slower.
Is there any configurations I can do in order to optimize the application performance?, for example; some configurations to draw my background with less quality.
thanks
After lots of trial and error the solution I found was doing the custom drawing on the OnSizeChanged event of a FrameLayout instead of OnDraw so that the code is only exectued once. In the OnSizeChanged event I draw the background in a temporary canvas, set it into a bitmap, created a BitmapDrawable for it and call setBackgroundDrawable with the BitmapDrawable. After that I do the custom drawing and paint it into a bitmap to which is set to a custom ImageView which can be zoomed and scrolled while performance is not affected. Custom ImageView is similar to the example I posted here.

Categories

Resources