0

as in the title, I have annoying problem with Android canvas. I draw text on point 0, 0 next I save image to external memory but on my image almost all of text is out of image.

My code:

    btn_calc.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Bitmap bmp = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        Canvas canv = new Canvas(bmp);
        //canv.
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        canv.drawPaint(paint);
        paint.setTextSize(14);
        canv.drawColor(Color.WHITE);
        paint.setColor(Color.BLACK);
        canv.drawText("Testy", 0, 0, paint);

        try {
            File f = new File(Environment.getExternalStorageDirectory() + "/test_image.png");
            f.createNewFile();
            FileOutputStream out = new FileOutputStream(f);
            bmp.compress(CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
});

Image: Imgur

Thanks for any help. Regards Paweł

Miziak
  • 1
  • 2
  • Can you fix the link? I have a hunch but I need to see the image to confirm it. – RB-Develop Mar 31 '14 at 13:30
  • 1
    read about the baseline http://en.m.wikipedia.org/wiki/Baseline_(typography) – pskink Mar 31 '14 at 13:33
  • The text is drawn with origion being the bottom left of the text. Measure the height of the text first, then draw at the modified origion. http://stackoverflow.com/questions/4909367/how-to-align-text-vertically – IAmGroot Mar 31 '14 at 13:34
  • Thanks for comments, for future generations solution is so simple: String str = "Testy"; Rect bounds = new Rect(); paint.getTextBounds(str, 0, str.length(), bounds); canv.drawText(str, 0, (bounds.height() - paint.descent()), paint); – Miziak Mar 31 '14 at 14:20

0 Answers0