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

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.

Related

How to create an interactive image in Android?

I want to put a photo in Android app that you can touch it in some parts and can edit it with an EditText, Check Box, etc.
You should look into the Canvas class as a basis for this. You can draw bitmaps, text, and perform image manipulations on the Canvas.
CanvasView is a nice simple API that builds on Canvas allowing you to draw/erase and add text/images onto a Canvas easily.

Create doodling feature like Snapchat

I'm developing an android app in which I want to integrate a feature using which user can take screenshot, doodle something on it and then send it via mail.
How to implement this? I just need a pen for doodling and an eraser to erase last doodle.
Take a look at this library :
ActiveDoodle
You can display two ImageViews on top of each other: the bottom one containing the photo, the top one containing an initially transparent Bitmap. In the top ImageView's onTouchEvent() method you can transform touch events into strokes of pen applied to the bitmap using a Canvas. You might want to read up on MotionEvents and Canvases.
After the user is done with doodling, you can load the screenshot to a Bitmap, rescale the doodle bitmap to match the screenshot's size, and draw in on top, again using a Canvas. Reading about Porter/Duff algebra will probably prove useful with applying an XferMode to a Paint.

Embed canvas into an activity

I'm writing a app for a tablet. I have a activity that contains text and buttons and an area where I want to put something that is drawn on a canvas. When buttons are pressed I want to draw a grid that will contain lines, text and images, built dynamically.
What I can't figure out is after I draw this on the canvas how to put/embed the canvas into the activity in a certain area(ImageView?). I have a prototype app where I can draw the grid, but I can't figure out how to put the canvas into the activity.
Any help or point me to an article would be really appreciated. Thanks
First you get a bitmap from your canvas then you can use it in your ImageView via ImageView.setImageBitmap(mCanvasBitmap)

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)

Categories

Resources