Sixth chapter a piece of white paper good painting-canvas canvas (3)

Source: Internet
Author: User

6.4 Canvas-Drawn helper classes

Before facing the introduction of canvas, we understand that canvas can do a lot of things, drawing graphics, transformations, of course, in the mobile phone world we see far from the simple graphics can be full of performance, there are colors, fonts, and other elements of a variety of components, specialized work to the special class to deal with. Here we introduce some of the auxiliary classes commonly used by canvas.

6.4.1 Brush Android.graphics.Paint

In a canvas-drawn auxiliary class, the Brush class-paint is the most frequently used. There is a parameter in the canvas's drawing method, paint. This parameter is the brush, and the paint class contains styles and colors for information about how to draw geometry, text, and bitmaps. Canvas is a canvas in which specific text and bitmaps are displayed, which is defined in the Paint class.

Let's look at the main methods in the paint class to see what it can do, see table 6-4 for details.

Method

return value

Description

SetColor (int color)

void

Sets the brush color.

Setargb (int A, int r, int g, int b)

void

Sets the brush's a (transparency), R (red), G (green), B (blue), Value (0X00000000-0XFFFFFFFF). The single value range is 0-255.

Settypeface (Typeface Typeface)

Typeface

Set the font, through the typeface can load the internal Android fonts, generally for the song body for Chinese, some of the ROM can be added themselves, such as Jache and so on.

SetStyle (Paint.style Style)

void

Set style, Paint.Style.FILL fill, or Paint.Style.STROK concave, hollow effect.

Setstrokewidth (float width)

void

Sets the width of the border.

Settextsize (float textSize)

void

Sets the font size.

SetTextAlign (Paint.align Align)

void

Sets the text alignment.

Setshader (Shader Shader)

Shader

Sets the shadow, the shader class is a matrix object, and if NULL clears the shadow.

Setunderlinetext (Boolean Underlinetext)

void

Whether the underline is set.

Setantialias (Boolean AA)

void

If set to True, the antialiasing occurs.

Setpatheffect (Patheffect effect)

Patheffect

Sets the path effect.

Table 6-4 Some of the main methods of the Paint class

6.4.2 Font Android.graphics.Typeface

Many times we would like to see different text effects applied to other applications, so here we will use the font (Typeface) class, by setting the font (Typefacesettypeface (Typeface Typeface)) method to set.

In Android, there are only three fonts "sans", "serif" and "monospace" in their own way. Sometimes, the font that comes with the system does not meet our special needs, then we need to refer to other fonts. You can put the downloaded font file in the assets directory, and then refer to it.

There are 2 ways to set a font:

1) Set the font in the Android XML file

<textview android:text= "Hello, world! Hello

<!--android:typeface for specifying fonts--

Android:typeface = "Sans"

Android:textsize= "20sp"/>

2) Set the font in the code

TextView TV = (TextView) Findviewbyid (r.id.your_textview_id);

Gets the resource from the Assert, using Getaserts (), obtained by giving the relative path below the assert/.

In actual use, the font library may also exist on the SD card, you can use CreateFromFile () to replace the Createfromasset.

Typeface face = Typeface.createfromasset (Getassets (), "Fonts/xxx.ttf");

Tv.settypeface (face);

Experience Sharing:

1. The system comes with the font, the default adoption of Sans, English fonts are different, most of the mobile device's Chinese font is not different. At the same time, in many mobile devices, the setting of Chinese fonts in bold or bold and so on is not effective.

2. Custom fonts cannot be directly in the XML file, and you need to write the source code.

3. The use of other fonts, will consume the program space, which is to be very careful.

4. Android is not compatible with all TTF fonts, especially in Chinese special font support will be problematic, for incompatible fonts, Android will not error, but not normal display. In general, we will use the fonts provided by the system default. File naming more need to note that the file is a Chinese name, there will be an error. And these fonts sometimes don't give you exactly the text you need.

As an example, omit the way. When there is too much text, you can omit the following by ellipsis, and the ellipsis is used as a font, which can be set by the "Android:ellipsize" property. If we need to use the ellipsis feature, we need to make sure that the font has an ellipsis. In addition, in order to ensure the consistency of the length, Android will be filled processing, in addition to a character replacement to omit the match, the following character will be replaced by a special Unicode character, ' ZERO WIDTH no-break SPACE ' (U+feff). This character takes up any visual position, but guarantees that the string has the same length. Not all fonts support this special character and may cause some garbled behavior.

6.4.3 Color Android.graphics.Color

With brushes, fonts, then we will give the brushes and fonts to use their favorite color. There's also a class in the virtual world of Android that shows color, and that's The Color class (Android.graphics.Color) We're going to introduce here. The color of Android is ARGB color, i.e. a (transparency), R (red), G (green), B (blue).

There are several ways to set the interface background and text color in Android, the following is an introduction to the 3 ways to set colors in Android:

1) set directly in the layout file:

<!--set to white--

Android:backgound= "#FFFFFFFF"

<!--set to black--

Android:textcolor= "#000"

<!--set to transparent--

Android:backgound = "@ Android. R.color.transparent "

2) Extract the settings from the resource file:

First, the color is extracted to form a resource, placed under the resource file (Values/drawable/color.xml).

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<drawable name= "White" > #FFFFFFFF </drawable>

<drawable name= "Black" > #FF000000 </drawable>

</resources>

It is then used in the layout file.

android:backgound= "@drawable/white", android:textcolor= "@drawable/black"

3) Set by Java code:

Set to Black

TextView. SetTextColor (Getresources (). GetColor (Android. R.color.black));

Set to White

Textview.setbackgound (0xFFFFFFFF);

Experience Sharing:

From the example above we have seen Android. R.color.transparent this color. This is the color of the Android system, commonly used are transparent (transparent), black, white, and so on. Set the method to refer to the above code.

Note the prefix for setting the color in the XML file must be a "#".

When setting the color, it is usually represented by a 8-bit 16-digit number (0xffff0000 is red), allowing the omission of a (transparency) (0xff000) or simplified write (0xf00). You can also change the number of 16 into other binary values, it is generally recommended to use 16, which is for the readability and specification of code.

Sixth chapter a piece of white paper good painting-canvas canvas (3)

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.