Android custom view Basics

Source: Internet
Author: User

This is a simple custom View Control Room, which can be displayed after being registered in XML.

Import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. align; import android. graphics. paint. style; import android. util. attributeset; import android. view. view; public class diyview extends view {public diyview (context) {super (context); // todo auto-generated constructor stub} public diyview (context, attributeset attrs) {super (context, attrs) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); // defines paint = new paint (); // sets its attribute paint. setstrokejoin (paint. join. round); paint. setstrokecap (paint. cap. round); paint. setcolor (color. red); // draw line canvas. drawline (0, 0,300, 0, paint); paint. setcolor (color. green); paint. setstyle (style. stroke); // draw a circle canvas. drawcircle (20, 80, 20, paint); paint. setstyle (style. fill); paint. setcolor (color. blue); // rectangular canvas. drawrect (0,200, 50,300, paint); paint. setcolor (color. gray); paint. settextsize (20); paint. settextalign (align. center); // text canvas. drawtext ("test", 150,150, paint); // image Bitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. ic_launcher); canvas. drawbitmap (bitmap, 300,300, paint); paint. setcolor (color. black); paint. setstrokewidth (40); // draw point. It looks like a circle here. It is mainly set to setstrokewidth, so it is larger than int colorfuls [] = new int [] {color. blue, color. green, color. red, color. magenta, color. black}; For (INT I = 0; I <200; I + = 40) {paint. setcolor (colorfuls [I/40]); canvas. drawpoint (200,200 + I, paint);} canvas. save ();}}

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: 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.