android-Customizing View Creation Overview

Source: Internet
Author: User

1. Create a class that inherits view

1  Public classMyViewextendsview{2 3      PublicMyView (context context, AttributeSet Attrs) {//Constructors4         Super(context, attrs);5         //TODO auto-generated Constructor stub6     }7     8     protected voidOnDraw (canvas canvas)9     {Ten         Super. OnDraw (canvas);  OneCanvas.drawcolor (Color.Black);//draw a black background APaint paint=NewPaint ();//Create a brush - Paint.setcolor (color.red); -Canvas.drawrect (10,10,110,110, paint);//Draw a rectangle theCanvas.drawtext ("This is the string", "Ten,", "paint");//Write -RECTF rf1=NewRECTF (10,130,110,230);//Define a rectangle -Canvas.drawarc (Rf1, 0, 45,true, paint);//draw an arc, clockwise -Canvas.drawline (All-in-ten, Max, +, paint);//Draw Line +RECTF rf2=NewRECTF (150,130,250,230); -Canvas.drawoval (Rf2, paint);//Draw a circle +          A     } at  -}

The above is an example.

2. To invoke this myview, it needs to be called on Layout.xml, as

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" />    <Com.example.myview.myviewAndroid:id= "@+id/myview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        /></Relativelayout>

Com.example.myview is the package that myview this class, and when XML is called, remember to add the package to the front.

This is another example of custom view

1  PackageCom.example.myview;2 3 ImportAndroid.content.Context;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 ImportAndroid.graphics.Canvas;7 ImportAndroid.graphics.Color;8 ImportAndroid.graphics.Paint;9 ImportAndroid.util.AttributeSet;Ten ImportAndroid.view.View; One  A  Public classMyview_pictureextendsview{ - Bitmap MB; - paint paint; the  -      PublicMyview_picture (Context context, AttributeSet attrs) { -         Super(context, attrs); -         //TODO auto-generated Constructor stub +          This. Initbitmap ();//Initialize picture -     } +      A      Public voidInitbitmap () at     { -Paint=NewPaint (); -mb=Bitmapfactory.decoderesource (Getresources (), r.drawable.indexgundom); -     } - @Override -     protected voidOnDraw (canvas canvas) in     { -         Super. OnDraw (canvas); toPaint.setantialias (true);//turn on anti-aliasing +Paint.setcolor (Color.White);//Set Brush Color -Paint.settextsize (15);//I guess the font size is set theCanvas.drawbitmap (MB, 10,10, paint);//Drawing *Canvas.drawtext ("Width of the Picture:" +mb.getwidth (), "A", ",");//Write $Canvas.drawtext ("Height of picture" +mb.getheight (), 150, 220, paint);Panax Notoginseng     } -      the  +}

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.