Android Custom View Component method

Source: Internet
Author: User

I. Development of custom View componentsThe view component is just a blank area of a rectangle, and nothing is left. For other UI components of Android apps, they inherit from the view component and then draw the appearance on the blank area provided by the view component.    We can derive a custom component by inheriting the view when the UI component provided by the Android system is not sufficient to meet the project's needs. Give an example: implement a small ball that follows your finger. When the user drags the screen through his finger, the program monitors the phone action and passes the position of the finger to the custom UI component and notifies the component to redraw. The development steps are as follows:1. Implement a subclass that inherits from view
Package Com.example.mycustomview;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.view.motionevent;import android.view.view;/** * Project name/version number: mycustomview/v1.0 * Package Name: Com.example.mycustomview * Class Description: Draw a small ball and initialize its position * created by: Jiangdongguo * Created: 2015-5-26 11:18:42 * Blog Address: http://blog.csdn.net/u012637501 */public class Drawview extends View {public Drawview (context context, AttributeSet Attrs) {Super (context, attrs);} private float CurrentX = 40;private float currenty = 50;/** * Define and create brush * */paint paint = new Paint ();p rotected void OnDraw ( Canvas canvas) {super.ondraw (canvas);p aint.setcolor (color.red);//Set the color of the brush canvas.drawcircle (CurrentX, CurrentY, 30, paint);//Specify the location to draw a circle with a radius of 15}/** * Override event handling method for the component's touchscreen event * */public Boolean ontouchevent (Motionevent event) {CurrentX = Event.getx (); Get the current touch screen position and modify Currentx,currentycurrenty = Event.gety (); invalidate (); Notifies the current component to redraw itself (that is, the callback OnDraw method) return true;}}

2. Adding a custom component to a layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:orientation= "vertical"    android:layout_width= "match_parent"    android:layout_height= "match_parent" >    <!--package name. Class Name--   <com.example.mycustomview.drawview       android:layout_width= "match_parent"       android:layout_height= " Match_parent "/></linearlayout>
Effect Demo:
Custom UI component Common overriding method Summary: >onfinishinflate (): After the application loads the component from an XML layout file and uses it to build the interface, the method is called back;    >onmeasure (int,int): Call this method to detect the size of the view component and all the subcomponents it contains;    >onlayout (Boolean,int,int,int,int): Callback The method when the component needs to allocate the position and size of its child components;    >onsizechanged (Int,int,int,int): Callback The method when the size of the component is changed;    >ondraw (Canvas): The method is drawn when the component is about to draw its contents; >onkeydown (int,keyevent): Triggers the method when a key is pressed >onkeyup (int,keyevent): Triggers the method when a key is released >ontouchevent (motionevent) : This method is triggered when a touchscreen event occurs
two . Common XML attributes for view class The view class is the base class for all visual controls, primarily providing methods for drawing and handling controls. The controls that are used to create the user interface inherit from the view, such as EditText, TextView, button, and so on. The properties of the view and its subclasses can be set either in the layout XML file or dynamically in Java code through member methods. ◇android:background: Sets the background color/background image. You can set the background to transparent in the following two ways: "@android: Color/transparent" and "@null"◇android:layout_margin: Sets the margins of the entire component up and down from the rest of the component, padding the blanks in pixels. ◇android:layout_marginleft: Sets the margins of the entire component to the left of other components, padding the blanks in pixels. ◇android:layout_marginright: Sets the margins of the entire component right from other components, padding in pixels. ◇android:layout_margintopt: Sets the margins of the entire component at the top distance from other components, padding the blanks in pixels. ◇android:layout_marginbottom: Sets the margins of the entire component's bottom distance from other components, fills the blanks in pixels◇android:clickable: Whether to respond to click events.
◇android:contentdescription: Set the comment description of the view as an accessibility feature that provides instructions for some view without a text description,◇ndroid:fadingedge: Sets the direction of the gradient of the border when you pull the scroll bar. None (border color unchanged), horizontal (horizontal color dimmed), vertical (vertical color dimmed)
◇android:fadingedgelength: Set the length of the border gradient
◇android:fitssystemwindows: Whether to consider system windows (such as status bar) when setting layout adjustments
◇android:isscrollcontainer: Sets the current view as a scrolling container, Listview/gridview/scrollview does not have to set this property at all, and Edidtext sets Android: The scrollbars can also be scrolled.
◇android:keepscreenon:view Whether the wake-up state is maintained under visible conditions◇android:longclickable: Sets whether to respond to long press events.
◇android:padding sets the margin around the top and bottom, padding the blanks in pixels.
◇android:paddingbottom sets the margin at the bottom and fills the blanks in pixels. ◇android:paddingleft Sets the left margin, in pixels, to fill in the blanks. ◇android:paddingright Sets the right margin, in pixels, to fill in the blanks ...◇android:paddingtop Sets the padding above and fills the blanks in pixels. ◇ANDROID:SCROLLX Sets the offset value of the horizontal scrolling in pixels, which can be seen in the GridView.
◇android:scrolly The offset value for vertical scrolling in pixels◇android:scrollbars Sets the scroll bar display. None (hidden), horizontal (horizontal), vertical (vertical).
◇android:visibility sets whether the view is displayed. Set Value: Visible (default, display), invisible (not shown, but still occupies space), gone (not shown, no space occupied)
◇android:onclick invokes the specified method from the context when clicked. ◇android:drawableright:output a drawable on the right side of the text, slice◇android:autolinkset whether text appears as clickable link when text is a URL link/email/phone number/map◇android:textscalex:control the spacing between words and words◇android:textalign:set the text display position, EditText does not have this property, but TextView has◇Android:autotext:if set, the spelling correction of the input values will be performed automatically. There is no effect here, it works when the input method is displayed and entered. ◇android:editable:whether it can be edited◇android:textcolorhint:sets the color of the message text, which is grayed out by default◇android:textappearanceTextappearancebutton, Textappearanceinverse, Textappearancelarge, Textappearancelargeinverse, Textappearancemedium, Textappearancemediuminverse, Textappearancesmall, Textappearancesmallinversesets the appearance of text. As "? Android:attr/textappearancelargeinverse" here refers to the system comes with an appearance,? Indicates whether the system has this appearance, otherwise the default appearance is used. Reference: http://blog.csdn.net/heng615975867/article/details/8910750


Android Custom View Component method

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.