Android Custom View self-drawing controls

Source: Internet
Author: User

First of all, I have a clear understanding of the custom view is not very deep, recently chewed a few days Guolin blogger wrote about the custom view of the blog, speak very good, but related to the source and the bottom of some things, I myself confused forced, At present just will be about the simple use of custom view, but still want to write out, as practice, haha ~ For some have not contacted beginners, hope will help and grow together;

By type, custom view can be divided into three types: self-painted controls , composite controls , inherited controls , and for these three types, I will write three blogs to introduce and use separately.
There are three very important methods in customizing view, namely:
Onmeasure (): If you need to change the size of the view, you need to override the Onmeasure () method.
OnLayout (): If you need to change the location of the view (in the parent control), you need to override the OnLayout () method.
OnDraw (): If you only need to draw the image of the view, you need to override the OnDraw () method. (This is also the most common way of rewriting.) )

1. self-Drawing Control: You draw the control by the OnDraw () method to display the content, mainly related to the parameters of the paint brush and the canvas canvas.
Common methods for canvas drawing are:
Drawcolor () Fill Color
DrawLine () Drawing lines
DrawLines () Draw lines
DrawOval () Draw a circle
DrawPath () Drawing a path
Drawpicture () Drawing pictures
Drawpoint () Draw point
Drawpoints () Draw point
Drawrgb () Fill Color
DrawRect () Draw a rectangle
DrawText () Drawing text
Drawtextonpath () drawing text on a path
2. combined CONTROLS: A combination of controls that combine the native controls of the system to form a new control, such as the title bar in the app
3. inherit control: The inheritance control refers to customizing a view and inheriting an existing control (such as inheriting Iamgeview), the benefit is not only can preserve ImageView original function and property, but also can add some new function according to the requirement.

First, the custom view of the self-Drawing Control, the demo will draw a circle, rectangle and a line of text, because it is simply to draw, so rewrite OnDraw () This method is enough, wow haha ~ ~ ~

First Look at:

The first step is to create a new class, inherit from view, add a constructor, initialize the brush inside the constructor, rewrite the OnDraw () method, and finish drawing through the brush and canvas in the OnDraw () method;

1 Package Com.example.wgh.customview;2 3 import Android.content.Context;4 import Android.graphics.Canvas;5 import Android.graphics.Color;6 import Android.graphics.Paint;7 import Android.util.AttributeSet;8 import Android.view.View;9 Ten /** One * Created by 080249 on 2016/9/9. A  */ -  Public classMyView extends View { -  the     PrivatePaint Mpaint =NULL ; -      PublicMyView (Context context, AttributeSet attrs) { - Super (context, attrs); -         //Initializing brushes +Mpaint =NewPaint (paint.anti_alias_flag); -     } +  A @Override at     protected voidOnDraw (canvas canvas) { - Super.ondraw (canvas); -         //Set Brush Color - Mpaint.setcolor (color.blue); -         /** - * Draw a circle through canvas in * 5 parameters correspond to each other: - * 100f: Right x axis coordinates to * 300f: Upper y axis coordinates + * 400f: Left x axis coordinates - * 600f: Lower y-axis coordinates the * Mpaint: Brushes *          */ $          //DrawOval () This method may be reported red after writing, the reason is to ask Minsdkversion for 21, go to Gradle file to modify it just finePanax NotoginsengCanvas.drawoval ( -, -, -, -, mpaint); -         /** the * Canvas Draws a line of words +          */ AMpaint.settextsize ( -); the Mpaint.setcolor (color.red); +Canvas.drawtext ("Custom View", $, $, mpaint); -         /** $ * Canvas Draws a rectangle $ * The corresponding parameters and draw circle are the same, are right, top, left and bottom corresponding coordinates -          */ - Mpaint.setcolor (Color.cyan); theCanvas.drawrect (0,0, $, -, mpaint); -     }Wuyi}

In the second step, after the drawing is finished, add the custom view to the layout file you need;

1<?xml version="1.0"encoding="Utf-8"?>2<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent">5     //note here to write the full path Oh6<Com.example.wgh.customview.MyView7Android:layout_width="wrap_content"8android:layout_height="wrap_content"/>9</RelativeLayout>

Android Custom View self-drawing controls

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.