Paint
It holds the style and colour information about drawing of geometries, text and bitmaps. Object and text drawing attributes are kept in instance Paint.
It is the most important Moscrif class where the setting of drawing is done. Paint properties can be divided into two groups.
- First group sets the appearance of drawing object and properties are:
Paint.alpha property sets/gets colours' alpha value.
Paint.maskFilter, property holds instance of Maskfilter object. Set NULL to clear any previous maskfilter.
//create mask filter
var maskFilter = MaskFilter.blur(10, #normal, #none); //apply mask filter to paint object
paint.maskFilter = maskFilter; canvas.drawBitmap(bitmap, 0, 0, paint);
Paint.colorFilter property sets the colour filter to grayscale, for example.
//create color filter for grayscale images
var colorFilter = ColorFilter.grayscale(); paint.colorFilter = colorFilter; //draw bitmap converted to grayscale
canvas.drawBitmap(bitmap, 0, 0, paint);
Paint.Shader is a Shader object. If you set NULL to the Paint.shader property, it clears previous shader.
//create new instance of shader class. For more information about parameters see Shader class
var shader = Shader.linear(pts, clrs, pos,0); //create new instance of Paint class
this.paint = new Paint(); //set shader
this.paint.shader = shader;
- Second group of Paint object sets parameters of text and properties are :
Paint.typeface property holds an instance of Typeface object and it identifies which font to use when drawing or measuring text.
Paint.textSize property sets the text size
//text italic pain
var textItalic = new Paint();
textItalic.textSize = 12;
textItalic.typeface = Typeface.fromName("freesans", #italic); //text normal paint
var textNormal = new Paint();
textNormal.textSize = 12;
textNormal.typeface = Typeface.fromName("freesans", #normal); //text bold paint
var textBold = new Paint();
textBold.textSize = 12;
textBold.tbPaint.typeface = Typeface.fromName("freesans", #bold);
Paint.fontAscent, recommended distance above the baseline (will be <= 0).
Paint.fontDescent, recommended distance below the baseline (will be >= 0).
Paint.fontSpacing, Returns the recommended line spacing. This will be Descent - Ascent + Leading
You can measure text with Paint.measureText method. This can be quite handy when you set paint object and you do not know what the absolute size of text is going to be.
var paint = new Paint(); //get text dimensions
var (w, h) = paint.measureText("Moscrif"); //draw text to the midle of the screen. Position of left bottom point is calculations using tet dimension
canvas.drawText("Moscrif", System.width / 2 - w / 2, System.height / 2 - h / 2, paint);
There are properties that you can use for setting of the apperance and text as well. One of these properties is Paint.color.
See Paint class properties and methods in API Docs.