Paint paint = new Paint(); paint.setTextSize(24); float ascent = paint.ascent(); // get ascent value canvas.drawText("Hello World!", x, y - ascent, paint); // draw text with adjusted y-coordinate
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStrokeWidth(2); paint.setTextSize(36); paint.setTextAlign(Paint.Align.CENTER); float ascent = paint.ascent(); // get ascent value canvas.drawText("A", x, y - ascent, paint); // draw single character with adjusted y-coordinateThis code creates a new Paint object with various attributes, gets the ascent value, and draws a single character "A" on a canvas with the adjusted y-coordinate. The attributes set the color to red, style to fill and stroke, stroke width to 2, text size to 36, and text align to center. This ensures that the character is properly styled and aligned with the baseline. The android.graphics.Paint class belongs to the "android.graphics" package library, which is included in the Android SDK and provides various classes and interfaces for drawing graphics on a canvas.