Several enumeration/constant values of Android Paint and ShadowLayer shadow effect

Introduction to this section:

The method parameters of Paint we have come into contact with such things: Paint.Style, Paint.Cap, Paint.Join, etc. These are some enumeration values ​​in Paint. We can set specific methods by setting these enumeration values. Effects such as: Style: brush style, Join graphics combination method, etc. In this section, we will walk into the source code of Paint, and we will introduce these enumeration values ​​one by one. In addition, we will also talk about the ShadowLayer setting Paint with shadow effects! Open the source code of the Paint class, we can see the following enumeration values:

Well, no BB, let's start this section!


1. Get enumeration usage:

I don’t know if you are unfamiliar or familiar with enumeration, here is the calling code related to Paint.Style (enumeration with parameter construction method), so that everyone can experience it:

public enum Style { 
    //Define enumeration, assign values ​​through brackets 
    FILL (0), 
    STROKE (1), 
    FILL_AND_STROKE (2); 
    //construct method with parameters 
    Style(int nativeInt) { 
        this.nativeInt = nativeInt; 
    } 
    final int nativeInt ; 
} 
//Method of setting brush Style 
public void setStyle(Style style) { 
    native_setStyle(mNativePaint, style.nativeInt); 
} 
//JNI method of setting brush style, here we don't need to pay attention to 
private static native void native_setStyle(long native_object, int style);

Let's explain the role of these enumeration values ​​one by one!


1.Paint.Style

Function: The style of the brush Optional values:

  • FILL : fill inside (default)
  • STROKE : stroke only
  • FILL_AND_STROKE : Fill interior with stroke

Method call: setStyle(Paint.Style style)  corresponding effect:


2.Paint.Cap

Function: stroke style, set the shape of the beginning and end of the brush (the first point and the last point that the brush starts to draw) Optional values:

  • BUTT : The stroke is rectangular and does not exceed the path (default)
  • ROUND : the stroke is a circle
  • SQUARE : the stroke is a square

Method call: setStrokeCap(Paint.Cap cap)

Corresponding effect: Usually we directly draw the first one, the other two will be a little more than the normal outside area, the second is a rounded corner, and the third is a rectangle!


3.Paint.Join

Function: Set the state of the joint, for example, your line is made up of multiple small lines, and the shape of the joint can be selected as follows:

  • MITER : Acute corners at junctions (default)
  • ROUND : The junction is an arc
  • BEVEL : the junction is a straight line

Method call: setStrokeJoin(Paint.Join join)

Generally, circular arcs are used a lot, you can refer to the display of the Wipe Out Beauty Clothes Demo

In addition, there is a setStrokeMiter (float miter) to set the inclination of the stroke, miter > = 0; For example: the pencil used when I was a child, the effect of the pen tip is different when it is sharpened obliquely and vertically. It is mainly used to set the style of the connection of the stroke. It can be compared with setStrokeJoin().


4.Paint.Align

Function: Set the alignment method of drawing text, which is an optional value relative to the [x, y] starting coordinates of drawing text:

  • LEFT : Draw text to the left of the starting coordinates
  • RIGHT : Draw text to the right of the starting coordinates
  • CENTER : Draw text centered on its actual coordinates

Method call: setTextAlign(Paint.Align align)

Corresponding effect: In addition, setTextSize() can be called to set the size of the drawn text~


5.Paint.FontMetrics和Paint.FontMetricsInt

Font properties and measurement, the other two methods are the same, but the value obtained by the latter is an integer, here we choose FontMetricsInt to explain to you, there are the following five constant values, the reference point here is: The position of the underline ( Baseline )

  • top : the distance from the highest character to the baseline, that is, the maximum value of ascent
  • ascent : the distance from the highest point of the character to the value of the baseline
  • descent : the distance from the underline to the lowest point of the character
  • bottom : the distance from the underline to the lowest character, that is, the maximum value of descent
  • leading : the distance between the descent of the previous line of characters and the ascent of the next line

Let's look at a few pictures to help understand:

Then we draw a string of letters at random and print out these values:  canvas.drawText("abcdefghijklnmopqrstuvwxyz", 400, 400, mPaint1);
Log.e("HEHE", mPaint1.getFontMetricsInt().toString());
run , we can see that the printed Log is as follows:

After reading it, think about it and draw a picture, it should not be difficult to understand! Here we just know it, if you want to study in depth, you can refer to the following article: Android string advanced three: font attributes and measurements (FontMetrics)


6.ShadowLayer sets the shadow effect

We taught you to set the shadow effect for the text of TextView in the section of TextView, and Paint actually provides an API for setting the shadow effect: setShadowLayer(float radius, float dx, float dy, int shadowColor)

Parameters: radius is the angle of the shadow, dx and dy are the distances of the shadow on the x-axis and y-axis, and shadowColor is the color of the shadow. We can write a very simple sentence to verify:

mPaint1.setShadowLayer(5,0,0,Color.BLACK); 
canvas.drawText("After all, God~", 400, 400, mPaint1); //Draw text

The effect is as follows :

In addition, we can also call clearShadowLayer() to clear the shadow layer~

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131790669