"Android" Custom view, canvas canvases and brushes paint
Source: Internet
Author: User
<span id="Label3"></p><p><p>Android Custom view is really simple. This view can be like "Android" using Java Code layout, button Add Click event (click to open link), the use of Java code to generate a series of components. You can also work with canvas canvases and brush paint.</p></p><p><p>Here is an example to Illustrate. For example, There is a custom layout view, which is lined with blue squares and red text drawn with canvas and brush paint.</p></p><p><p><br></p></p><p><p>In res\layout\activity_main.xml, just like placing an Android intrinsic component, you can use this I define the component Directly. There are blue squares and red text Inside. There is no ova, but this simple example shows how custom view is Used. Some views can be reused multiple times with several intrinsic components. which, here Com.define_view. MyView is a class I've built with Java Code.</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><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 ></pre></pre>In other words, to customize the view, you must first create a new Java file in Src.<p><p></p></p><p><p><br></p></p><p><p>This Java file is named myview.java, inheriting ANDROID.VIEW.VIEW.</p></p><p><p><br></p></p><p><p>The new myview.java,eclipse will immediately remind you of the need to build a construction method.</p></p><p><p><br></p></p><p><p>If this custom view later needs to be manipulated by Java code, The first constructor is required, and if the custom view is only statically placed in the XML file, then only the second constructor is Required. If both are needed, you can create both of these constructs at the same time ... Use the second one Here.</p></p><p><p>After That,->source->override/implement Methods directly from Eclipse by right-clicking on the space, overwriting the OnDraw method of the parent class.</p></p><p><p><br></p></p><p><p>Rewrite the View.java code after the Following:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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 (con text, Attributeset);//initialization brush, Any initialization should not be in the OnDraw method, otherwise eclipse will appear too memory-intensive Warning//construction method is used to initialize the various tools paint = new Paint ();} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);p aint.setcolor (color.blue);// Set the brush color to blue canvas.drawrect (ten, ten, +, +, paint); Draw the Upper-left corner in (10,10), size 100x100 rectangle paint.setcolor (color.red);//set The brush color to red paint.settextsize (24);// Set the text size to 24canvas.drawtext ("i was drawn", ten, +, paint);//in (10,120) Draw text}}</pre></pre>After changing the application name in res\values\strings.xml, the app is Finished. Custom view has been laid out in the Res\layout\activity_main.xml Above.<br><pre name="code" class="html"><pre name="code" class="html"><?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></pre></pre><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>"Android" Custom view, canvas canvases and brushes paint</p></p></span>
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