Basic tutorial for Android -- 8.3.14 Paint enumeration-Constant Value and ShadowLayer shadow effect

Source: Internet
Author: User
Tags drawtext

Basic tutorial for Android -- 8.3.14 Paint enumeration-Constant Value and ShadowLayer shadow effect
Basic tutorial for Android -- 8.3.14 Paint several enumeration/constant values and ShadowLayer shadow effect

Tags (separated by spaces): basic Android tutorial

This section introduces:

In the Basic tutorial for Android -- 8.3.1, the three drawing tool classes explain the method parameters of the Paint.
The following are some enumeration values in Paint. Style, Paint. Cap, and Paint. Join.
By setting these enumerated values, we can set specific effects such as Style: paint brush Style and Join image combination,
In this section, we will go to the source code of the Paint. We will introduce these enumeration values one by one. In addition, we will also explain this ShadowLayer.
Set the Paint with shadow effect! Open the source code of the Paint class and we can see the following enumerated values:

Okay, no BB. Start this section!

1. get enumeration usage:

I don't know whether you are familiar with enumeration. Here I will paste the Call Code related to painting. Style (enumeration with parameter constructor)
To let everyone know:

Public enum Style {// defines enumeration, and assigns FILL (0), STROKE (1), FILL_AND_STROKE (2) through parentheses; // The construction method Style (int nativeInt) {this. nativeInt = nativeInt;} final int nativeInt;} // set the method public void setStyle (Style style) {native_setStyle (mNativePaint, style. nativeInt);} // method for setting the paint brush style in JNI. Here we do not need to pay attention to private static native void native_setStyle (long native_object, int style );

Next we will explain the functions of these enumerated values one by one!

1. Paint. Style

Purpose: paint brush Style
Optional values:
-FILL: Internal filling (default)
-STROKE: Stroke-only
-FILL_AND_STROKE: Fill in the interior and stroke
Method call:SetStyle (Paint. Style style Style)
Effect:

2. Paint. Cap

Purpose: set the stroke style and the image at the beginning and end of the paint brush (the first and last points of the paint brush)
Optional values:
-BUTT: Stroke is a rectangle and cannot exceed the path (default)
-ROUND: The stroke is circular.
-SQUARE: Stroke is square
Method call:SetStrokeCap (Paint. Cap cap)
Effect: We usually draw the first image. The other two will be a little more out of the ordinary area, and the second one.
It is a rounded corner, and the third is a rectangle!

3. Paint. Join

Purpose: set the state of the Joint. For example, if your line is spliced by multiple small lines, the shape of the joint is
Optional values:
-MITER: The joint is acute (default)
-ROUND: The joint is an arc
-BEVEL: Straight Line at the Joint
Method call:SetStrokeJoin (Paint. Join join)
Generally, the arc is much used. For more information, see the Demo of wiping beautiful clothes.
There is anotherSetStrokeMiter (float miter)Is to set the slope of the stroke, miter> = 0;
For example, when a pencil is used as a child, the oblique effect is different from that obtained in vertical cutting.
It is mainly used to set the style of the stroke link. It can be compared with setStrokeJoin.

4. Paint. Align

Purpose: set the Method of Drawing text, that is, relative to the starting coordinate of [x, y]
Optional values:
-LEFT: Draw text on the left of the starting Coordinate
-RIGHT: Draw text on the right of the starting Coordinate
-CENTER: Draw text centered on actual coordinates
Method call:SetTextAlign (Paint. Align align)
Effect: You can also use setTextSize () to set the text size ~

5. Paint. FontMetrics and Paint. FontMetricsInt

Font attributes and measurements. The other two methods are the same, but the value obtained by the latter is an integer,
Here we will use FontMetricsInt to explain the following five constant values. The reference points here are:
Underline position (Baseline)

Top: The distance from the highest character to baseline, that is, the maximum value of ascent. Ascent: The distance from the highest character to the baseline value Descent: The distance between the underline and the lowest character Bottom: The distance between the underline and the lowest character, that is, the maximum value of descent. Leading: The distance between the descent of the last line of characters and the ascent of the next line

Let's take a look at several figures to help us understand:

Then we draw a string of letters at will to print these values:
Canvas. drawText ("abcdefghijklnmopqrstuvwxyz", 400,400, mPaint1 );
Log. e ("HEHE", mPaint1.getFontMetricsInt (). toString ());
Running, we can see that the printed Log is as follows:

After thinking and thinking, it is easy to draw a picture! Here we can understand it. If you want
For more information, see the following article: Android string advanced 3: FontMetrics)

6. Set the ShadowLayer effect.

In the TextView section, we have taught you how to set the shadow effect for TextView text. In fact, painting also provides settings.
Shadow effect API:SetShadowLayer (float radius, float dx, float dy, int shadowColor)
Parameter: radius indicates the shadow angle, dx and dy indicate the distance between the shadow on the X and Y axes, and shadowColor indicates the shadow color.
We can write a very simple sentence for verification:

MPaint1.setShadowLayer (5, 0, 0, Color. BLACK); canvas. drawText (after all ~, 400,400, mPaint1); // draw text

The effect is as follows::

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.