Monday, March 29, 2021
Android draw line on imageview

Android draw line on imageview

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm trying to create a canvas, and use the drawcircle function within canvas to set the location of the circle. And then draw that canvas to my imageview in some way.

I cannot get this to work, is there a trick to this? Or is my method fundamentally wrong? How would i go about drawing circles to the ImageView without recreating my entire layout? I had the same challenge and came to the conclusion that overwriting onDraw will at least in the general case not work. My blog explains the reasons. What worked very well for me is the following:. I think that a better approach would be to create a custom ImageView and Override the onDraw method.

Something like:. It would be a good idea to implement some logic that checks if there is a fresh bitmap to acquire via the update method, so that the code inside onDraw won't execute every time and put overhead to the system.

And then use your custom view wherever you need it. If you have a xml with layout with all your elements arranged in vertical orienttion. You can achieve what you want by making a class in your package which extends Class View and override its onDraw method.

Check the following link for 2D graphics. Its a great tutorial to read. There are a couple of ways to do what you want, but using an ImageView the way you describe isn't one of them.

One possibility is to have the ImageView display an animated Drawable. You can then focus on having your Drawable implementation draw the circles.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. For drawing the line the user actually drew you have to override the dispatchTouchEvent.

From that event you can get the coordinates of the line and draw them in the onDraw as shown by george. Take a look at the ApiDemos sample FingerPaint. Learn more. How to draw a Line in ImageView on Android? Ask Question.

android draw line on imageview

Asked 9 years, 1 month ago. Active 3 years, 6 months ago. Viewed 25k times. I'd Like to know how to draw a Line on ImageView as user swipe their finger? Could any body explain this? Or perhaps any Link to get start on this. Felipe Mosso 3, 11 11 gold badges 31 31 silver badges 57 57 bronze badges. Active Oldest Votes. You must have your own ImageView and override onDraw function. George George 1, 11 11 silver badges 25 25 bronze badges. Hi george, did you mean with calling the update method on onTouch event?

I'm sorry for asking to much, i'm new in this. Yes, I mean it.When you need to display static images in your app, you can use the Drawable class and its subclasses to draw shapes and images. A Drawable is a general abstraction for something that can be drawn. The various subclasses help with specific image scenarios, and you can extend them to define your own drawable objects that behave in unique ways. There are two ways to define and instantiate a Drawable besides using the class constructors:.

Note: You might instead prefer using a vector drawable, which defines an image with a set of points, lines, and curves, along with associated color information.

This allows vector drawables to be scaled for different sizes without a loss of quality. For more information, see Vector drawables overview. You can add graphics to your app by referencing an image file from your project resources. App icons, logos, and other graphics, such as those used in games, are well suited for this technique. Once in your project, you can reference the image resource from your code or your XML layout. Either way, it's referred to using a resource ID, which is the file name without the file type extension.

For example, a true-color PNG that doesn't require more than colors may be converted to an 8-bit PNG with a color palette.

How to draw a line in Android?

This results in an image of equal quality but which requires less memory. As a result, the image binaries placed in this directory can change at build time. The following code snippet demonstrates how to build an ImageView that uses an image created from a drawable resource and adds it to the layout:. In other cases, you may want to handle your image resource as a Drawable object, as shown in the following example:.

Note: Each unique resource in your project can maintain only one state, no matter how many different objects you instantiate for it. For example, if you instantiate two Drawable objects from the same image resource and change a property such as the alpha for one object, then it also affects the other. When dealing with multiple instances of an image resource, instead of directly transforming the Drawable object you should perform a tween animation.

For more information about using project resources, see Resources and assets. Note: When using image resources as the source of your drawables, be sure the images are the appropriate size for various pixel densities. If the images are not correct they will be scaled up to fit, which can cause artifacting in your drawables. For more information, read Support different pixel densities.

Simple one

If there is a Drawable object that you'd like to create, which isn't initially dependent on variables defined by your code or user interaction, then defining the Drawable in XML is a good option. Even if you expect your Drawable to change its properties during the user's interaction with your app, you should consider defining the object in XML, as you can modify properties after it's instantiated.

Any Drawable subclass that supports the inflate method can be defined in XML and instantiated by your app. Each drawable class that supports XML inflation utilizes specific XML attributes that help define the object properties. The following code instantiates the TransitionDrawable and sets it as the content of an ImageView object:. A ShapeDrawable object can be a good option when you want to dynamically draw a two-dimensional graphic.

You can programmatically draw primitive shapes on a ShapeDrawable object and apply the styles that your app needs. ShapeDrawable is a subclass of Drawable. For this reason, you can use a ShapeDrawable wherever a Drawable is expected. For example, you can use a ShapeDrawable object to set the background of a view by passing it to the setBackgroundDrawable method of the view.

You can also draw your shape as its own custom view and add it to a layout in your app. Because ShapeDrawable has its own draw method, you can create a subclass of View that draws the ShapeDrawable object during the onDraw event, as shown in the following code example:.

You can use the CustomDrawableView class in the code sample above as you would use any other custom view. For example, you can programmatically add it to an activity in your app, as shown in the following example:. The ShapeDrawable class, like many other drawable types in the android. Some example properties you might want to adjust include alpha transparency, color filter, dither, opacity, and color.

You can also define primitive drawable shapes using XML resources.Posted by: admin February 23, Leave a comment. And then draw that canvas to my imageview in some way. I cannot get this to work, is there a trick to this?

Or is my method fundamentally wrong? How would i go about drawing circles to the ImageView without recreating my entire layout? I had the same challenge and came to the conclusion that overwriting onDraw will at least in the general case not work.

My blog explains the reasons. What worked very well for me is the following:. I think that a better approach would be to create a custom ImageView and Override the onDraw method.

Something like:. And then use your custom view wherever you need it. You can achieve what you want by making a class in your package which extends Class View and override its onDraw method.

Check the following link for 2D graphics. Its a great tutorial to read. One possibility is to have the ImageView display an animated Drawable.

android draw line on imageview

You can then focus on having your Drawable implementation draw the circles. Another is to create a new Bitmap every time you want the image to change and set the ImageView to display the new Bitmap.

The usual way of doing this, though, is to create a custom View subclass. The API Demos sample project has an example of a custom view, and you can find lots tutorials with Google. Tags: androidcanvasimageview. February 24, Android Leave a comment. CoreComponentFactory from [com. Questions: I have two images of the same object and scenery which was taken from two different devices kept as close as possible to each other.

I have an application with an ImageViewer class which ca Questions: I have recycler view in a home fragment. Add menu.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If you want to have a simple Line in your Layout to separate two views you can use a generic View with the height and width you want the line to have and a set background color. With this approach you don't need to override a View or use a Canvas yourself just simple and clean add the line in xml. The example code I provided will generate a line that fills the screen in width and has a height of one dp. If you have problems with the drawing of the line on small screens consider to change the height of the line to px.

The problem is that on a ldpi screen the line will be 0. Sometimes this may result in a rounding that makes the line vanish. If this is a problem for your layout define the width of the line a ressource file and create a separate ressource file for small screens that sets the value to 1px instead of 1dp. This approach is only usable if you want horizontal or vertical lines that are used to divide layout elements.

If you want to achieve something like a cross that is drawn into an image my approach will not work. There are two main ways you can draw a line, by using a Canvas or by using a View.

From the documentation we see that we need to use the following method:. The Paint object just tells Canvas what color to paint the line, how wide it should be, and so on.

Android: Drawing a canvas to an ImageView

If you only need a straight horizontal or vertical line, then the easiest way may be to just use a View in your xml layout file. You would do something like this:. Here is a picture with two lines one horizontal and one vertical to show what it would look like:.

You can draw multiple straight lines on view using Finger paint example which is in Developer android. Just comment: mPath. If you're working with ConstraintLayout you need to define at least 2 constraints for the line to show up. Like this:. Learn more.This is a topic for which I found on the Internet a lot of ideas but they were more workarounds for special situations than general solutions. One of my apps contained an ImageView widget into which I had to load a series of pictures one after the other at runtime.

The challenge was to mark certain areas in the picture with a coloured rectangle dynamically created at run time and adjusted using the coordinates of the original, unscaled image. I am meanwhile quite sure that this works only under certain circumstances, for example when you are sure, your image will not be scaled. The problem lies in the provided canvas. The original onDraw method applies a certain transformation matrix to the canvas before it draws the image into the canvas and restores the canvas in its previous state afterwards.

android draw line on imageview

This matrix is stored in a private member variable of ImageView and for whatever reason it is not always the same matrix as the one returned from getImageMatrix calls. Using the debugger I have seen cases where getImageMatrix returned a non-null matrix where onDraw used a null Matrix and therefore did no do any transformation to the canvas at all. So there is no general reliable way to calculate the coordinates for anything you want to draw on top of what ImageView has drawn.

Create an ImageView for your activity the usual way, there is plenty of sample code for this available on the internet. Think of the canvas as a piece of paper on which you can make a drawing by painting bitmaps, lines, circles etc. As soon as you have attached the Canvas to the ImageView the drawing of the paper appears within the ImageView. You actually attach tempBitmap and not tempCanvas to the ImageView. Your comments however say, that you attach the canvas to the ImageView.

Greatvery helping.

Drawables overview

I just want to know if I am drawing a number of the rectangle on my image view. How do I get click events on those rectangles? Your email address will not be published. Therefore I used a different approach: Create a new image bitmap and attach a brand new canvas to it so that the bitmap and the canvas use the same coordinate system.

Draw the image bitmap into the canvas. Draw everything else you want into the canvas the rectangles in my case. Attach the canvas to the ImageView. Here is the code snippet: import android. Bitmap; import android. Canvas; import android. Paint; import android. RectF; import android. I hope this helps a bit.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more. How to add a horizontal 1px line above image view in a relative layout? Ask Question. Asked 9 years, 7 months ago.

Active 4 years ago. Viewed 61k times. How do I add a horizontal 1px white line above image view in a relative layout? Jonik Active Oldest Votes. Just add the following line in your XML where ever you want it.

RenatoIvancic 1 1 gold badge 11 11 silver badges 27 27 bronze badges. I added this and it works great, thank you! So I had to remove this attribute, but the line is hanging somewhere in the middle of the relative layout. Simpler and works the same. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Tales from documentation: Write for your clueless users. Podcast a conversation on diversity and representation.

Upcoming Events. Featured on Meta. Feedback post: New moderator reinstatement and appeal process revisions. The new moderator agreement is now live for moderators to accept across the….


thoughts on “Android draw line on imageview

Leave a Reply

Your email address will not be published. Required fields are marked *