Android custom View

Source: Internet
Author: User

Android custom View

In the process of Android development, the official Android control sometimes cannot meet our needs. At this time, I need to customize it. Next we will look at the custom View:

Package com. example. myview; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. util. attributeSet; import android. view. view; import android. view. view. onClickListener; public class MyView extends View implements OnClickListener {private int a = 0; private Paint paint; public MyView (Context context) {super (context ); // TODO Auto-generated constructor stub} public MyView (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); paint = new Paint (); // defines a paint brush. setColor (Color. RED); // set the paint color to paint. setStyle (Style. FILL); // set the paint brush to FILL the canvas. drawCircle (100,100,100, paint); // draw a circle painting. setColor (Color. BLUE); // set the color paint for the screen. setTextSize (20); // set the font size canvas. drawText ("small source" + a, 100,100, paint); setOnClickListener (this); // Add a listener to the View} @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stuba ++; invalidate (); // redraw }}

In XML, We need to write the custom control as follows:

 
  
 


This completes a very simple custom View. Work together on the program Road, come on!

Okay, a little progress! It will eventually become a great god!


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.