0

I am currently doing such type of project. My requirement is how to find total x,y position used in writing text in canvas.

For example :

show in image, display on alphabets, I want to find total x,y points used to make A.

enter image description here

Matthieu
  • 16,103
  • 10
  • 59
  • 86
Hemantvc
  • 2,111
  • 3
  • 30
  • 42

2 Answers2

2

I don't know why would you need to do that, but here's some rude solution:

Map<Integer, Integer> result = new HashMap<Integer, Integer>();
for (int x = 0; x < bitmap.getWidth(); x++) {
    for (int y = 0; y < bitmap.getHeight(); y++) {
        int color = bitmap.getPixel(x, y);
        if (color != BACKGROUND_COLOR) {
            result.put(x, y);
        }
    }
}

You just loop through your bitmap and compare color of each pixel to background color. If it's different, you add current pixel to result map.

See this post if you don't know how to retrieve Bitmap from Canvas

Community
  • 1
  • 1
Yury Pogrebnyak
  • 4,093
  • 9
  • 45
  • 78
0

Create custom class

public class CustomCanvas extends Canvas {

    private Rect rect;


    @Override
    public void drawPicture(Picture picture, Rect dst) {
        rect =dst;      
        super.drawPicture(picture, dst);
    }

    public Rect getRect() {
        return rect;
    }


}

Call CustomCanvas rect from

@Override
    public boolean onTouchEvent(MotionEvent event) {
        canvas.getRect().intersect((int)event.getX(),(int)event.getY(),0,0);
        return super.onTouchEvent(event);
    }
Yahor10
  • 2,123
  • 1
  • 13
  • 13