简体   繁体   中英

Android Canvas Co-ordinate

I am developing a android application for my college library. I want to draw the map of my library. Map should display the book shelf number along with its location. I am trying to achieve this by drawing lines on Android Canvas. Is there any better way of drawing the map of my library?

Secondly , it is very troublesome for me to work on Canvas co-ordinate system. Is there any tool available by which I can draw the Canvas(For eg. Paint like thing) and generate co-ordinates from it?

You could draw the map as a file (png, jpg, svg) and then draw the picture on the canvas. I would suggest using svg-android ( https://code.google.com/p/svg-android/ ) then you can easily change the scale of the picture for different devices. You can draw the SVG in InkScape or Illustrator and use those programs to work out the pixel distances to the coordinates you want.

Put the SVG (eg. library_map.svg) in your res/raw folder and call it with:

    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.library_map);

Create a view and in the onDraw() put:

      canvas.translate(map_x_position, map_y_position); 
      canvas.drawPicture(svg.getPicture());
      canvas.translate(-map_x_position, -map_y_position);

      canvas.translate(marker_x_position, marker_y_position); 
      canvas.drawPicture(marker.getPicture());
      canvas.translate(-marker_x_position, -marker_y_position);

This function is useful for supporting different screen sizes:

float getPxFromDp(float dpValueToConvert) {
    // Converts dp into its equivalent px
    Resources r = getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValueToConvert, r.getDisplayMetrics());
    return px;
}

One thing to watch out for with svg-android is that it does not support hardware acceleration. You can put android:hardwareAccelerated="false" into the application section of your AndroidManifest.xml to ensure the svg displays fine in newer devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM