Android custom view Method

Source: Internet
Author: User
Tags getcolor

Android custom view Method
1. Where does the custom view start! Where can I start painting? View has several methods.
One of them is known as OnDraw (). However, if you want to call and refresh at any time, what should you use? If you have done win api development, you will know the inVidate () method. This is the method for the notification to start repainting. After invaidate is executed, ondraw () is restarted ().
2. If you want to add custom attributes.

If custom views do not have their own unique attributes, they can be directly used in xml files. If they contain their own unique attributes, you need to obtain the attribute file attrs In the constructor. custom attribute names in xml and default values as needed are not defined in xml files. If you use custom attributes, you need to add a new schemas to the xml file of the application. For example, xmlns: my = "http://schemas.android.com/apk/res/package name". "my" after xmlns is the prefix of custom attributes, and "res" is the package where our application is located.

The usage of custom attributes is as follows:
Step 1: Add a custom attribute name in attrs. xml and the format is as follows:
  
  
 
        
    

Step 2: xmlns: tri = "http://schemas.android.com/apk/res/package name" must be added to the layout attribute of the outermost layer or the view attribute.

Step 3: write in the added custom attributes
   
    
        />

Step 4: In the onDraw method, call:
Context. obtainStyledAttributes (attrs, R. styleable. TriangleViewAttr );
Here I re-propose a method:
 private void getAttr(Context context, AttributeSet attrs)
    {
        array = context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
        maincolor = array.getColor(R.styleable.TriangleViewAttr_tricolor, maincolor); 
// TriangleViewAttr_tricolor is TriangleViewAttr. tricolor
        array.recycle();
    }
Array. recycle (); is very important. Remember to recycle it!

Since the specified property value has been obtained.
Step 5:
Perform the following operations based on the custom property value you have obtained!


OK! So I will paste my code!
1. First, attr. xml:

     xml version="1.0" encoding="utf-8"?>
<resources>




<declare-styleable name = "TriangleViewAttr">
<attr name = "tricolor" format = "color" />
declare-styleable>

resources>

2. Code of the TriangleView class:

PackageCom. commons. widget. views;

ImportAndroid. content. Context;
ImportAndroid. content. res. TypedArray;
ImportAndroid. graphics. Canvas;
ImportAndroid. graphics. Color;
ImportAndroid. graphics. Paint;
ImportAndroid. graphics. Path;
ImportAndroid. util. AttributeSet;
ImportAndroid. util. Log;
ImportAndroid. view. View;
ImportCom. jsdx. zqysypt. R;

/**
* Created by aa on 2015/1/12.
*Draw a triangle with three points1.Top left corner2.Bottom left3.Center on the right;:
*@*****
******@
*@*****
*
*Usage
* Xmlns: tri = "http://schemas.android.com/apk/res/ Package name "
* Android: layout_width = "12dp"
* Android: layout_height = "12dp"
* Tri: tricolor = "@ color/ios_blue"/>
*/
Public classTriangleView ExtendsView {
// The default color is transparent.
Int Maincolor= Color. TRANSPARENT;
// Equals Triangle or not
Boolean IsEquilateral= False;


// Tri: tricolor = "@ color/ios_blue"
// TypedArray Is Context. obtainStyledAttributes Array of the obtained Properties
// You must call Recycle Method
// The property name is Styleable Name in + "_" + Attribute name
TypedArray Array= Null;

PublicTriangleView (Context context ){
Super(Context );
}

PublicTriangleView (Context context, AttributeSet attrs ){
Super(Context, attrs );
GetAttr (context, attrs );
}

PublicTriangleView (Context context, AttributeSet attrs, IntDefStyleAttr ){
Super(Context, attrs, defStyleAttr );
GetAttr (context, attrs );
}

Private voidGetAttr (Context context, AttributeSet attrs)
{
Array= Context. obtainStyledAttributes (attrs, R. styleable. TriangleViewAttr);
Maincolor= Array. GetColor (R. styleable. TriangleViewAttr_tricolor, Maincolor); // TriangleViewAttr_tricolor Yes TriangleViewAttr. tricolor
Array. Recycle ();
}

@ Override
Protected voidOnDraw (Canvas canvas ){
Super. OnDraw (canvas );
// Create paint brush
Paint p = NewPaint ();
P. setColor ( Maincolor); // Set red
P. setStyle (Paint. Style. FILL); // Set to fill

// Returns the number of vertices.
IntWidth = getWidth ();
IntHeight = getHeight ();
IntLoc_x = getLeft ();
IntLoc_y = getTop ();
Log. D( "TriangleView", Width + ""+ Height );


// Draw this triangle , You can draw any polygon.
Path path = NewPath ();
Path. moveTo (0, 0 ); // This is the starting point of a polygon.

If( IsEquilateral)
Path. lineTo (height/2) * 1.73205f, height/2 ); /// Used here * 1.73205f This is because if you want to draw an equi-edge triangle, you need to use this proportion. The three sides of the root number are the other sides. 1.73205f Times.
Else
Path. lineTo (width, height/2 );

Path. lineTo (0, height );
Path. close (); // Make these points form a closed polygon
Canvas. drawPath (path, p );

}


/** Reset color
* @ Param Color
* (0xe96f4a) Invalid (0xffe96f4a) In this way, you can
*/
Public voidShowColor ( IntColor)
{
Maincolor= Color;
This. Invalidate ();
}

}
3. layout xml:

<com.commons.widget.views.TriangleView
xmlns:triattr="http://schemas.android.com/apk/res/com.jsdx.zqysypt"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
triattr:tricolor="@color/mainact_lefttopblue" />














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.