Android Development's Custom view (view) usage detailed _android

Source: Internet
Author: User
Tags drawtext stub

This example describes the custom view (view) usage of Android development. Share to everyone for your reference, specific as follows:

The view class is a super class for Android, which contains almost all of the screen types. Each view has a canvas for drawing, and the canvas can be arbitrarily extended. In the game development often needs the custom view, this canvas's function can satisfy us in the game development the need. In Android, any view class can simply rewrite the OnDraw method to display the interface, and a custom view could be a complex 3D implementation or a very simple text form.

In order to implement a custom view, you need to create a new class and then rewrite the OnDraw method, where you need to be aware that the newly created class MyView to inherit the view base class, along with two constructor methods with parameters MyView (context context) and MyView ( Contextcontext,attributeset attr), otherwise the compile run cannot pass.

In the OnDraw method, initializes a brush object Mypaint, sets the brush color, also has the text size, fills and so on attributes. The drawing of a bar statistic graph is completed by using the parameters canvas the drawing canvas. The specific code is as follows:

Package com.viewtest;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.graphics.Paint.Style;
Import Android.util.AttributeSet;
Import Android.view.View;
    public class MyView extends View {public MyView (context) {super (context);
  TODO auto-generated Constructor stub} public myview (context Context,attributeset attr) {super (context,attr);
  Private Paint Mypaint;
  private static final String myString1 = "Annual investment of China Mobile Internet industry in the first half of 2006-2011";
  private static final String myString2 = "Source: Qing ke Research center 2011.08";
    @Override protected void OnDraw (Canvas Canvas) {//TODO auto-generated Method Stub Super.ondraw (Canvas);
    Mypaint = new Paint (); Draw the title Mypaint.setcolor (Color.Black);
    Set the brush color mypaint.settextsize (18);/Set Text Size Canvas.drawtext (myString1, mypaint); Draw axes Canvas.drawline (M, M, MypainT)//Vertical axis canvas.drawline (m, V, mypaint)//horizontal axis int[] Array1 = new int[]{0, 50, 100, 150, 200, 250, 3
    00, 350};
    Draw ordinate scale mypaint.settextsize (10);/Set Text Size Canvas.drawtext ("Million Dollars", M, Mypaint);
      for (int i = 0; i < array1.length i++) {canvas.drawline (M, 500-array1[i], si, 500-array1[i], mypaint);
    Canvas.drawtext (Array1[i] + "", 500-array1[i], mypaint);
    //Draw the horizontal axis text string[] array2 = new string[]{"2008", "2009", "2010", "2011 First half"};
    for (int i = 0; i < array2.length i++) {canvas.drawtext (Array2[i], Array1[i] +, 520, mypaint); }//Draw bar Chart Mypaint.setcolor (Color.Blue); Sets the brush color Mypaint.setstyle (Style.fill); Sets the fill canvas.drawrect (new Rect, 500-56, s), mypaint); draw a rectangle, the first two parameters are the upper-left corner of the rectangle, and the last two are the lower-right coordinates canvas.drawrec T (new Rect (140, 500-98, 160, Mypaint);/The second rectangle Canvas.drawrect (new Rect (190, 500-207, 210,), mypaint); Three rectangular canvas.drawrect (new RECT (M, 500-318, V, m), mypaint);//Fourth Rectangular mypaint.setcolor (Color.Black); Sets the brush color Canvas.drawtext ("56.32", 500-58, Mypaint)//The number of the first rectangle is illustrated Canvas.drawtext ("98.00", 138, 500-100, MYP
    aint);
    Canvas.drawtext ("207.65", 188, 500-209, mypaint);
    Canvas.drawtext ("318.30", 238, 500-320, mypaint); Draw provenance Mypaint.setcolor (Color.Black);
  Set the brush color mypaint.settextsize (16);//Set Text size Canvas.drawtext (myString2, 560, mypaint);

 }
}

Then add our custom view to the Main.xml layout file, where the background color of the view is white to better show the content. The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" fill_parent "
  android:layout_height=" fill_parent "
  android:o" rientation= "vertical" >
  <textview
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:text=" @string/hello "/>
  <button android:layout_width=" Match_parent "
    android:layout_height= "40dip"
    android:text= "next picture"/>
  <com.viewtest.myview
    android: Layout_width= "Fill_parent"
    android:layout_height= "wrap_content"
    android:background= "#FFFFFF"/>
</LinearLayout>

The initial activity. Java files do not need to be modified. The final effect is shown in the following illustration:

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android Layout layout tips" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.