Android learns the use of shape by drawing a dashed oval border background with code

Source: Internet
Author: User

In the development of Android program, we often use shape to define a variety of shapes, shape can draw rectangular rings and ellipses, so only need to use an ellipse, In the use of the control, such as ImageView or TextView high-width set to the same is the positive circle, solid represents a far fill color, the stroke represents a far border line, so the two can be combined to achieve the edge of the circle, of course, you can also directly add a size control high width. So I'll show you first what labels are in shape and what they mean:

Shape Properties:

Rectangle: Rectangle

Oval: Oval

Line: Thread, need stroke to set width

Ring: Annular

Solid Properties:

Color: Fill Color


Stroke Properties:

Color: Border Color

Width: Border Width

Dashwidth: The width of the dashed box

Dashgap: The interval of the dashed box


Corners Properties:

Radius: radius of Four Corners

Toprightradius: The radius of the upper right corner

Bottomleftradius: Radius in lower right corner

Opleftradius: The radius of the upper-left corner

Bottomrightradius: The radius of the lower left corner


Gradient properties:

StartColor: Actually the color

Centercolor: Middle Color

EndColor: End Color

CenterX: The relative x-coordinate of the middle color (0--1)

CenterY: The relative y-coordinate of the middle color (0--1)

Uselevel: (true/false), whether used as a levellistdrawable flag

Angle is a gradient angle and must be an integer multiple of 45. 0 from left to right, 90 bottom to top, 180 right to left, 270 top to bottom

Type: Gradient mode. Default linear gradient, you can specify a gradient of radial (radial gradient) or sweep (like a radar scan)

Gradientradius: Gradient radius, radial gradient specifies the radius.


Padding properties:

Left: Inside margin

Top: Upper inner margin

Right: Inside margin

Bottom: Lower inner margin


Size property:

Width: Wide

Height: High


Now let's take an example and draw five different shapes to learn more about the use of shape. Examples are as follows:

1, draw the oval dotted border background, the resource file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >   <!--rounded Corners-   <corners       android:bottomleftradius= "8DP"       android:bottomrightradius= "8DP "       android:radius=" 15DP "       android:topleftradius=" 8DP "       android:toprightradius=" 8DP "/>   <!-- Stroke--   <stroke       android:dashgap= "4DP"       android:dashwidth= "4DP"       android:width= "2DP"       Android:color= "@color/ellipse_dashed_line_color"/></shape>

2, the picture solid line transparent border background, the resource file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >   <!--rounded Corners-   <corners       android:bottomleftradius= "6DP"       android:bottomrightradius= "6DP "       android:radius=" 10DP "       android:topleftradius=" 6DP "       android:toprightradius=" 6DP "/>   <!-- Stroke--   <stroke       android:width= "1DP"       android:color= "@color/ellipse_dashed_line_color"/> </shape>

3, Draw solid line fill color border background, the resource file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >   <!--rounded Corners-   <corners       android:bottomleftradius= "6DP"       android:bottomrightradius= "6DP "       android:radius=" 10DP "       android:topleftradius=" 6DP "       android:toprightradius=" 6DP "/>   <!-- Stroke--   <solid       android:width= "1DP"       android:color= "@color/ellipse_dashed_line_color"/></ Shape>

4, Draw solid line transparent half ellipse border, the resource file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android >   <item>       <shape android:shape= "Rectangle" >           <stroke               android:width= " 1.2DP "               android:color=" #669df3 "/>           <solid android:color=" #00000000 "/>           <corners               android:bottomrightradius= "10DP"               android:toprightradius= "10DP"/>           <padding               android:bottom= " 8DP "               android:left=" 12DP "               android:right=" 12DP "               android:top=" 8DP "/>       </shape>   </item></layer-list>

5, Draw solid line fill color half ellipse border, the resource file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android >   <item>       <shape android:shape= "Rectangle" >           <solid android:color= "#669df3"/ >           <corners               android:bottomleftradius= "10DP"               android:topleftradius= "10DP"/>           <padding               android:bottom= "8DP"               android:left= "12DP"               android:right= "12DP"               android:top= "8DP"/>       </shape>   </item></layer-list>

As follows:



If you want to source, please support, share to friends Circle, send to share to the circle of friends screenshots and mailbox to this public number, small series will strive for the first time to send the source to you.



Number:smart_android (← Long press copy)

Introduction: Non-famous programmers, the word Zhong straight right, number astringent lang, love to Machine, love programming, is crawling in the mobile internet in a code-maker!

Personal Number: LOONGGG

Micro-blog: astringent Lang

QQ Group: 413589216 More information and source code to do in QQ group files

Today's headline: Search for "non-famous programmers" subscription more information

Work: Focus on the development and research of mobile Internet, this number is dedicated to sharing it technology and procedures ape work experience. Welcome everyone to pay attention to and reprint.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android learns the use of shape by drawing a dashed oval border background with code

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.