Draw bitmap as line on canvas


Vasily Kuragov

Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quickly, the coordinates slow down and the app draws it as a gap . Does anyone know how to draw lines from point to point using bitmaps?

Cephalosporium

Create an ArrayList here first to save the points/coordinates

ArrayList<PointF> drawPoints=new ArrayList<>();

Then, create this method

public void draw1(float x,float y){
    PathMeasure pm=new PathMeasure(path1,false);
    float fSegmentLen=pm.getLength()/pm.getLength();
    Matrix m=new Matrix();
    int ox=-pattern.getWidth()/2;
    int oy=-pattern.getHeight()/2;

    for (int i=1;i<pm.getLength();i+=increment){
            pm.getMatrix(fSegmentLen*i,m,PathMeasure.POSITION_MATRIX_FLAG);
            m.preTranslate(ox,oy);
//change pattern below to the bitmap you want to draw
            drawCanvas.drawBitmap(pattern,m,drawPaint);
        invalidate();
    }
}

Then call the above method in onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event)  {
    float touchX=event.getX();
    float touchY=event.getY();

    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            firstPointX=touchX;
            firstPointY=touchY;
        case  MotionEvent.ACTION_MOVE:
                drawPoints.add(new PointF(touchX,touchY));
                if (drawPoints.size()>=2) {
                    PointF pointF = drawPoints.get(drawPoints.size()-2);
                    path1.moveTo(pointF.x, pointF.y);

                    PointF next = drawPoints.get(drawPoints.size() - 1);
                    path1.quadTo(pointF.x, pointF.y, next.x, next.y);
                    //path1.quadTo(firstPointX, firstPointY, next.x, next.y);
                    draw1(touchX,touchY);
                }
                invalidate();
                path1.reset();
            break;
        case MotionEvent.ACTION_UP:
            drawPoints.clear();
            path1.reset();
            break;
        default:
            return false;
    }
    invalidate();
    return true;
}

If you want to use it in Kotlin, you can automatically convert its code to Kotlin in Android Studio, hope it helps, please let me know if it works

Related


Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap to canvas with alpha gradient

Cactus I want to draw one Bitmapon Canvas, with a (linear) alpha-gradient applied. The important point is that I don't want to overwrite the image with any other colors. The background (from the back I've drawn it to ) should just "shine". To illustrate this,

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

Draw bitmap to canvas with alpha gradient

Cactus I want to draw one Bitmapon Canvas, with a (linear) alpha-gradient applied. The important point is that I don't want to overwrite the image with any other colors. The background (from the back I've drawn it to ) should just "shine". To illustrate this,

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

Draw a bitmap along a path on the canvas

Salman Khakwani I'm trying to create a drawing application that will be able to draw different brush textures along a touch path on the screen. What I've done so far: Here's the code for my "custom view": public class TestDrawingView extends View{ private Bit

Draw bitmap on canvas with custom shape

username I want to draw an image of the following shape on a canvas: Black has to be replaced with my image. I currently draw the image as a whole. I just don't know how to get this shape? canvas.drawBitmap(header,0,0,mPaint); Can someone help me? usernam

Draw a bitmap along a path on the canvas

Salman Khakwani I'm trying to create a drawing application that will be able to draw different brush textures along a touch path on the screen. What I've done so far: Here's the code for my "custom view": public class TestDrawingView extends View{ private Bit

Struggling to make a canvas with a bitmap and draw on it

username Here is my code so far. Edit: Added full code. public void enterPreviewMode(byte[] pictureData) { //Stop preview and enter review mode previewPic = true; cam.stopPreview(); //Find buttons and make them visible/invisible Button ta

How to draw 3 bitmap on canvas

Christopher M. I'm trying to combine 3 imageViews into one bitmap, and I'm using canvas, here is the function private Bitmap createSingleImageFromMultipleImages() { Bitmap formBitmap = getBitmapFromImageView(formView); Bitmap idFrontBitmap = getBitmap

Draw bitmap to canvas with alpha gradient

cactus I want to draw one Bitmapon Canvas, with a (linear) alpha-gradient applied. The important point is that I don't want to overwrite the image with any other colors. The background (from the back I've drawn it to ) should just "shine". To illustrate this,

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

Draw bitmap on canvas with custom shape

username I want to draw an image with the following shape on the canvas: Black has to be replaced with my image. I currently draw the image as a whole. I just don't know how to get this shape? canvas.drawBitmap(header,0,0,mPaint); Can someone help me? use

Draw a bitmap along a path on the canvas

Salman Khakwani I'm trying to create a drawing application that will be able to draw different brush textures along a touch path on the screen. What I've done so far: Here's the code for my "custom view": public class TestDrawingView extends View{ private Bit

Struggling to make a canvas with a bitmap and draw on it

username Here is my code so far. Edit: Added full code. public void enterPreviewMode(byte[] pictureData) { //Stop preview and enter review mode previewPic = true; cam.stopPreview(); //Find buttons and make them visible/invisible Button ta

draw a line on the canvas

username XAML <Canvas Name="canvas" MouseDown="canvas_MouseDown" MouseUp="canvas_MouseUp" /> C# Point P1; private void canvas_MouseDown(object sender, MouseButtonEventArgs e) { P1 = e.GetPosition(canvas); } private void canvas_MouseUp(object sender, Mous

draw a line on the canvas

username XAML <Canvas Name="canvas" MouseDown="canvas_MouseDown" MouseUp="canvas_MouseUp" /> C# Point P1; private void canvas_MouseDown(object sender, MouseButtonEventArgs e) { P1 = e.GetPosition(canvas); } private void canvas_MouseUp(object sender, Mous

How to draw a line on canvas?

monkey 334 I've read some tutorials on the internet, but I can't seem to find any way to show me the line Can someone help? i try to do p = Canvas(height = 600, width = 800).place(x=0,y=0) p.create_rectangle(50, 25, 150, 75, fill="blue") Unfortunately, it did

draw a line on the canvas

username XAML <Canvas Name="canvas" MouseDown="canvas_MouseDown" MouseUp="canvas_MouseUp" /> C# Point P1; private void canvas_MouseDown(object sender, MouseButtonEventArgs e) { P1 = e.GetPosition(canvas); } private void canvas_MouseUp(object sender, Mous

How to draw a line on canvas?

monkey 334 I've read some tutorials on the internet, but I can't seem to find any way to show me the line Can someone help? i try to do p = Canvas(height = 600, width = 800).place(x=0,y=0) p.create_rectangle(50, 25, 150, 75, fill="blue") Unfortunately, it did

OpenCV, Android: draw line on bitmap

Tix I currently have an app that onClickwill draw a green bounding rectangle around the battery and blue strips. I also want the button onClickto draw a line between the battery and the strip of paper (as shown in the second image below). Currently I am able t

How to draw part of bitmap through canvas DrawBitmap

XTL I have one : FrameLayout (marked in red) Source ImageView (black) Object (image view) with OnTouchListener (orange) I want to display part of bitmap which is filled on imageview ( source imageview ) through Object with OnTouchListener . So that's not the p

Draw Bitmap on Xamarin.Android Canvas

Pensive Cyborg I have seen many examples of android programming in java, but few related to Xamarin/C#. I am unable to draw this bitmap on my canvas using the following code (I have highlighted what is not working). If something works without compile time erro

How to draw bitmap on top right of canvas

User 1204501 I am trying top right hand cornertoCanvas So far I have done the following: //100x40 dimensions for the bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backbutton); Rect source = new Rect(0, 0, bitm

How to draw bitmap on top right of canvas

User 1204501 I am trying top right hand cornertoCanvas So far I have done the following: //100x40 dimensions for the bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backbutton); Rect source = new Rect(0, 0, bitm

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in html. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n