Android _ graphics Basics

Source: Internet
Author: User

Android provides a complete two-dimensional local graphics library in its android. graphics package. Including Color and Canvas graphics.

1. Color class
The color in Android is represented by four numbers. Transparency, Red, Green, and Blue (Alpha, Red, Green, Blue, and ARGB) are all numbers. Because each number has 256 (8 bits) possible values, a color is usually expressed as a 32-bit integer.

Android Code uses integers instead of Color instances to represent colors. Transparency 0 indicates full transparency, and 255 indicates full opacity.

How to Create a color object:

[Html]
Int color = Color. BLUE; // solid blue, which uses the BLUE static constant of the Color class.
[Html] view plaincopy
Color = Color. argb (127,255, 0,255); // Translucent purple, known for transparency and red, green, and Blue values. This method can be used (static sampling)
If possible, it is best to define all colors in an xml resource file.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resource>
<Color name = "mycolor"> # 7fff00ff </color>
</Resource>
Code call Method

[Html]
Color = getResources (). getColor (R. color. mycolor); // The getResources () method returns the ResourceManager class of the current activity. The getColor () method requires the resource manager to find a Color Based on the Resource ID.
2. Paint class
The Paint class is one of the most important classes in the Android graphics library. It contains the information required for style, color, and drawing of any image (such as bitmap, text, and ry) www.2cto.com.

You can use the Paint. setColor () method to set the color when drawing a solid color on the screen.

[Html]
CPaint. setColor (Color. LTGRAY); // use a pre-defined Color value of light gray.
3. Canvas class
A canvas that can be drawn on it. Various methods in the Canvas class can be used to draw lines, rectangles, gardens, and any other images on the Canvas.

The display in Android is dominated by Activity objects. Activity Objects Reference View objects, while View Objects Reference Canvas objects. You can override the View. onDraw () method to draw a graph on the specified canvas. The only parameter of the onDraw () method is the canvas on which the image is to be drawn.

Example:

[Java]
Public class Graphics extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (new GraphicsView (this ));
}
Static public class GraphicsView extends View {
Public GraphicsView (Context context ){
Super (context );
}
@ Override
Protected void onDraw (){
// Write code here
}
}
}

4. Path class
The Path class contains a set of vector plot commands, such as draw lines, draw rectangles, and draw curves.

Define a circle

[Java]
Circle = new Path ();
Circle. addCircle (150,150,100, Direction. CW); // indicates the Center Coordinate x = 150, y = 150 radius 100 pixels
 

From Click Here

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.