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.

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. Actually i am new to this android envrmnmt. Can u just demonstrate it? Or does i have to put my question once more with all my code? Jan 12 '12 at It works only 3 changes needed - first add Paint p as a property of the class, then initialize it in your class constructor, then move super. This will allow you to draw over original image.

This is a complete example of how you can draw green rectangle over another image: package CustomWidgets; import android.

Simple one

Context; import android. Canvas; import android.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. 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:.

Subscribe to RSS

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.

Android: Drawing a canvas to an ImageView

How to draw a line in android Ask Question. Asked 9 years, 10 months ago. Active 1 year, 10 months ago. Viewed k times. Can anybody tell how to draw a line in Android, perhaps with an example? Do you want to draw a line into something or do you want to draw a simple line in the layout? Active Oldest Votes. This one draws 2 lines which form a cross on the top left of the screen: DrawView.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.

Android tutorials-24-Animations for an ImageView with translate, fade, rotate

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.Thanx, its very simple to draw line along finger. Instead of drawing a line in case statement MotionEvent. That will draw a thick line along with your finger touch. I'm having a problem that It doesnot draw line on proper mouse point OR touch point. How can i solve this problem?

Is your line doesn't come on the place where you touch or its not coming just smoother. Even my line doesn't come at the place where i touch. There is always some distance between them which is not constant.

android draw line on imageview

I am also having the same problem that Harshal and Killer talks about. Another problem is that the canvas does not cover the whole image area where as the imageview has been set to fill-parent width,height.

Hello friend. Its just happening because we normally move finger faster where as code can't execute at that speed. May this solve your problem. I've done it in such way. Sharad, you still continue to respond to this post? I have a big question. I want draw straight line on image. Mayur, To make something look a like first you have to heading to Maths.

Forrest fenn interview clues

I sorry but I am not aware about that much mathematical measurement. I can help you out in android queries but not in mathematical solutions. I faced the same issue when I was drawing over the screenshot that I took. This occurs because the ImageView is bigger than the Bitmap of the screenshot to maintain aspect ratio, the ImageView doesn't fill the width. You can confirm it by setting a colour background of the ImageView.

So I basically did following the steps. Then in the onTouchI replace: a. Did u solve the problem? I spent so much time on this I found that creating bitmap on Layout change solves the problem. BLACK ; paint. Here's what you need.

android draw line on imageview

In order to plot the missing points you should query for the historical points x,y along with the other points. These are the points that form the intermediate skipped points.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.

Drawables overview

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.

android draw line on imageview

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.

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. I just want to know if I am drawing a number of the rectangle on my image view.

2019 mazda 3 problems

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. Leave a Reply Cancel reply Your email address will not be published.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?

android draw line on imageview

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.

Duramax bad ecm symptoms

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. 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. Learn more. Asked 9 years, 5 months ago. Active 4 years, 10 months ago. Viewed 92k times. I'm new to android programming and what I'm trying to figure out is this; In my layout i have a TextView, ImageView, and Button, all on a vertically oriented LinearLayout.

Kara 5, 11 11 gold badges 46 46 silver badges 54 54 bronze badges. Active Oldest Votes. What worked very well for me is the following: Create a new image bitmap and attach a brand new canvas to it. Draw the image bitmap into the canvas.

Draw everything else you want into the canvas.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. 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.

Henglong rc tank parts

I have an application with an ImageViewer class which ca Questions: I have recycler view in a home fragment. Add menu. What worked very well for me is the following: Create a new image bitmap and attach a brand new canvas to it. Draw the image bitmap into the canvas.

Draw everything else you want into the canvas. Attach the canvas to the ImageView. Here is a code snippet for this: import android. Bitmap; import android. Canvas; import android. Paint; import android. RectF; import android. BLACK ; canvas. That will happen everytime onDraw has received a call i. If you have a xml with layout with all your elements arranged in vertical orienttion.

Please use 'annotationProcessor' configuration inst


Replies to “Android draw line on imageview”

Leave a Reply

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