Android Graphics those things (up)

Source: Internet
Author: User

(i) overview
Although, has learned the content of the Android drawing, but always feel very vague, today, carefully comb the next idea! Is purely a reading notes, sorting out the things you didn't know before, OK to start: (This section mainly introduces some common methods of drawable and their XML definitions, if you are already familiar with it, skip it)

(ii) Android screen adaptation issues
Android screen adaptation and compatibility, has always been a very headache problem, how can the different screen size of the mobile phone tablet effect display is not distorted! Now look at the relevant parameters of the slag on the screen:

———-screen-related parameters

1) Screen Size
Refers to the length of the screen diagonal, usually using "inch" to measure, see the 4.7-inch mobile phone, 5.5-inch mobile phone;

2) Resolution
Refers to the number of cell phone screen, such as: 720x1280 refers to the screen resolution, refers to the width of 720 pixels, and high there are 1280 pixels.

3) PPI
The number of pixels per inch is also called DPI, which is obtained by dividing the diagonal pixels by the size of the screen;

No.2 ———-System screen density
As a lot of domestic manufacturers, to adapt to the screen size of each cell phone, it is almost impossible, so the system defines a number of standard DPI values, as the fixed dpi value of the phone, so you need to adapt to these standard dpi almost can! A lot of happy moments, the standard screen density is as follows:

No.2 independent image number density DP
It is because of the different screen density, resulting in the same size of the length of the different density of the screen display length is different, it is easy to understand the length of the same, the density of a large number of pixels on the unit area, on the contrary, will be less;
The Android system uses the MDPI-density 160 screen as the standard, on this screen 1px = 1 DP, other screens can be converted by scale; As in the following, the direct conversion ratio of each resolution :

No.3 Unit conversions
All types of conversions are here, you can save it in the project to use;

ImportAndroid.content.Context;ImportAndroid.util.TypedValue;/** * Created by Eillot on 2016/7/18. * * DP, SP converted to PX Tool class */ Public  class displayutil {    /** * Convert PX value to dip or DP value, ensure size unchanged * <p/> * (attribute density in displaymetrics Class) * * @param cont Ext * @param pxvalue * @return * *     Public Static int Px2dip(Context context,floatPxvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Pxvalue/scale +0.5f); }/** * Convert dip or DP value to PX value, ensure size unchanged * <p/> * (attribute density in displaymetrics Class) * * @param  context * @param dipvalue * @return * *     Public Static int dip2px(Context context,floatDipvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Dipvalue/scale +0.5f); }/** * Convert PS value to SP value, ensure size unchanged * <p/> * (attribute scaleddensity in Displaymetrics Class) * * @param context * @param pxvalue * @return * *     Public Static int PX2SP(Context context,floatPxvalue) {Final floatFontscale = Context.getresources (). Getdisplaymetrics (). scaleddensity;return(int) (Pxvalue/fontscale +0.5f); }/** * Convert SP value to PX value, ensure size unchanged * <p/> * (attribute scaleddensity in Displaymetrics Class) * * @param context * @param spvalue * @return * *     Public Static int sp2px(Context context,floatSpvalue) {Final floatFontscale = Context.getresources (). Getdisplaymetrics (). scaleddensity;return(int) (Spvalue/fontscale +0.5f); }/** * Where density is the conversion ratio mentioned above. * This is used to convert using the formula conversion method, * at the same time the system also provides the Typevalue class to help us convert, * code as follows * /    /** * * dp2px * @param dp * @return  * *    protected int dp2px(intDP) {return(int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, DP, Getresources ().    Getdisplaymetrics ()); }/** * * sp2px * *    protected int sp2px(intSP) {return(int) (Typedvalue.applydimension (typedvalue.complex_unit_sp, SP, Getresources ().    Getdisplaymetrics ()); }}

(iii) 2D drawing basics
In fact, canvas and paint very good understanding, I believe that you are familiar with the following this stuff!

In contrast, very good understanding, all kinds of properties are properly understood!
First look at the properties of the paint (brush):



So much, I pick a few commonly used to say, other use when Google ~

The following shows various effects:
Code Template:

/** * Created by Eillot on 2016/7/18. * * Public  class drawshape extends View {    /** * * Color * *    Private intMclor = color.red;/** * * Brush * *    PrivatePaint Mpaint; Public Drawshape(Context context) {Super(context);    Initview (); } Public Drawshape(context context, AttributeSet attrs) {Super(context, attrs);    Initview (); } Public Drawshape(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Initview (); }Private void Initview() {Mpaint =NewPaint ();        Mpaint.setcolor (Mclor); Mpaint.setstrokewidth ( $f);//Border thickness degreeMpaint.setstyle (Paint.Style.FILL);//Solid       //Mpaint.setstyle (Paint.Style.STROKE); //Hollow}@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);//Add code hereCanvas.drawrect (0,0, -, -, Mpaint); }}

The following changes OnDraw () in the canvas.drawxxx () can be;
1) Mpaint.setstyle (Paint.Style.FILL);//Solid

2) Mpaint.setstyle (Paint.Style.STROKE);//Hollow
The thickness of the width is not set, the default value is used

In addition, please consider the following:
Why do I put canvas.drawrect (0, 0, +, mpaint); instead of canvas.drawrect (+, +, +, +, mpaint); the drawing is a rectangle, not a square, supposedly, the edge Should the appearance wait for the square?
My XML file:

    <com.mydraw2.DrawShape        android:id="@+id/my_views"        android:layout_width="match_parent"        android:layout_height="100dp" />

Let's continue here:
For drawing points:
Canvas.drawpoint (n, Mpaint);

To draw a line:
Canvas.drawline (0,0,300, mpaint);

Draw Multiple lines:

float[] pts={50,50,400,50,                400,50,400,600,                400,600,50,600,                60,600,50,50};        canvas.drawLines(pts ,mpaint);

Will eventually be drawn into a vertical rectangle;

To draw a rounded rectangle:

        new10102102101515 ,mpaint);

Draw a circle:

 canvas.drawCircle(100,100,50,mpaint);

Draw arcs:

        new0,0,100,1000270true ,mpaint);
Paint.Style.STROKE + userCenter(true-----的情况就像下面

Paint.Style.STROKE + userCenter(flase);-----的情况就像下面

Paint.Style.FILL + userCenter(flase);-----的情况就像下面

Paint.Style.FILL + userCenter(true);-----的情况就像下面

To draw an ellipse:

new20 ,20,200,300 ) , mpaint);

Draw text:

 canvas.drawText("H e  l  l   o" ,50,50,mpaint);

Draw text along the path:

Path PATH = new Path ();Path. MoveTo( -, -);Path. LineTo( -, -);Path. LineTo( $, $);Path. LineTo( -, -);Path. Close();Canvas. Drawtextonpath("H e l l o", Path, -, -, Mpaint);

(d) Android XML drawing
No.1--bitmap
The use of bitmap in XML is very simple, and the label is changed to <bitmap /> add the SRC element in it to reference the image; it's like this.

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android">    android:src="@drawable/ic_launcher"</bitmap>

No.2--–shape
Use shape to draw in XML like this: (Don't need to die remember, check it out when you use it)

No.3--–layer Layers
The XML definition is very simple, just like this: (I took it directly)

No.4 ——-Selector (Nothing to say, just a variety of property configurations)

OK, draw all kinds of XML definition to here, probably have an impression on it, do not need to rote, use when back check OK, the next section said Xu Dashen bring us the drawing skills!

Android Graphics those things (up)

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.