[Android] custom View, Canvas and Paint brush, androidcanvas

Source: Internet
Author: User

[Android] custom View, Canvas and Paint brush, androidcanvas

Android custom View is actually very simple. This View can use Java code to generate a series of components, just like "Android" uses Java code layout and buttons to add and click events "(click to open a link. You can also use it with Canvas and Paint.

The following is an example. For example, there is a custom Layout View with blue square and red text drawn from Canvas and Paint.


In res \ layout \ activity_main.xml, you can directly use this custom component just like placing an inherent Android component. There are blue square and red text. This simple example shows how to use a custom View. For some views placed by multiple inherent components, they can be reused multiple times. Here, com. define_view.MyView is the class I constructed using Java code.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.define_view.MyView        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></FrameLayout>
That is to say, to customize a view, you must first create a Java file in src.


This Java file is named MyView. java and inherits android. view. View.


After MyView. java is created, Eclipse immediately reminds you to create a constructor.


If the custom View needs to be operated by Java code later, the first constructor is required, if the custom View is only statically placed in the xml file, you only need the second constructor. If both are required, you can create these two constructor methods at the same time ...... The second one is used here.

Then, right-click the blank space-> Source-> Override/Implement Methods and use the onDraw method of the parent class from Eclipse to rewrite it.


Rewrite the View. java code as follows:

Package com. define_view; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. util. attributeSet; import android. view. view; public class MyView extends View {private Paint paint; public MyView (Context context, AttributeSet attributeSet) {super (context, attributeSet); // initialize the Paint brush, no initialization should be performed in the onDraw method. Otherwise, Eclipse will receive a warning that memory consumption is too high. // The constructor is used to initialize the paint = new Paint ();} of various tools ();} @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); paint. setColor (Color. BLUE); // set the paint brush color to BLUE canvas. drawRect (10, 10,100,100, paint); // draw a rectangle of X in the upper left corner (10, 10. setColor (Color. RED); // set the paint color to RED paint. setTextSize (24); // set the text size to 24 canvas. drawText ("I am drawn", 10,120, paint); // draw text in (10,120 }}
Then modify the application name in res \ values \ strings. xml, and the app is complete. The custom View is arranged in res \ layout \ activity_main.xml above.
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> Custom view </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> </resources>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.