How does Android use shape?

Source: Internet
Author: User

 

What I saw when I flipped through the Forum, I always felt that I would use these fragmented things in the future, so I reprinted the stream and saved it ~~~~

Original http://www.eoeandroid.com/thread-71143-1-1.html

Everyone should understand that in Android, shape is often used to define some Display Properties of controls. So how to use it? Today we will look at the use of some shapes, after reading this article, you will have a general understanding of shape. The following is a brief summary. Please watch it carefully:

 

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape=["rectangle" | "oval" | "line" | "ring"] >     <gradient         android:angle="integer"         android:centerX="integer"         android:centerY="integer"         android:centerColor="integer"         android:endColor="color"         android:gradientRadius="integer"         android:startColor="color"         android:type=["linear" | "radial" | "sweep"]         android:usesLevel=["true" | "false"] />     <solid         android:color="color" />     <stroke         android:width="integer"         android:color="color"         android:dashWidth="integer"         android:dashGap="integer" />     <padding         android:left="integer"         android:top="integer"         android:right="integer"         android:bottom="integer" />     <corners         android:radius="integer"         android:topLeftRadius="integer"         android:topRightRadius="integer"         android:bottomLeftRadius="integer"         android:bottomRightRadius="integer" /> </shape>

Java code:

  1. <Shape>
  2. <! -- Solid -->
  3. <Solid Android: color = "# ff9d77"/>
  4. <! -- Gradient -->
  5. <Gradient
  6. Android: startcolor = "# ff8c00"
  7. Android: endcolor = "# ffffff"
  8. Android: angle = "270" type = "parmname" text = "parmname"/>
  9. <! -- Stroke -->
  10. <Stroke
  11. Android: width = "2dp"
  12. Android: color = "# dcdcdc"/>
  13. <! -- Rounded corner -->
  14. <Corners
  15. Android: radius = "2dp"/>
  16. <Padding
  17. Android: Left = "10dp"
  18. Android: Top = "10dp"
  19. Android: Right = "10dp"
  20. Android: Bottom = "10dp"/>
  21. </Shape>

Copy code

Solid: solid, indicating Filling
Android: Color specifies the fill color

Gradient: Gradient
Android: startcolor and Android: endcolor are the start and end colors respectively. ndroid: angle is the gradient angle, which must be an integer multiple of 45.
In addition, the default gradient mode is Android: TYPE = "linear", that is, linear gradient. You can specify the gradient as a radial gradient, Android: TYPE = "RADIAL", and the radius must be specified for the radial gradient Android: gradientradius = "50 ".

Stroke: Stroke
Android: width = "2dp" stroke width, Android: Color stroke color.

We can also draw the stroke into a dotted line. The setting method is as follows:
Android: dashwidth = "5dp"
Android: dashgap = "3dp"
Android: dashwidth indicates the width of a horizontal line like '-', and Android: dashgap indicates the distance between them.

Corners: rounded corner
Android: radius is the Radian of an angle. The larger the value is, the closer the angle is.
We can also set the four angles to different angles:

Java code:

  1. <Corners
  2. Android: toprightradius = "20dp" upper right corner
  3. Android: bottomleftradius = "20dp" bottom right corner
  4. Android: topleftradius = "1dp" upper left corner
  5. Android: bottomrightradius = "0dp" lower left corner
  6. />

Copy code

Note that bottomleftradius is in the lower right corner, rather than in the lower left corner. This is a bit depressing, but it does not affect usage. Remember to make a mistake.
Some people on the Internet said that setting 0dp is invalid, but I found it works during the test. I used 2.2. Maybe this problem has been fixed, if it is invalid, it can only be set to 1dp.

Padding: interval
This is not much to mention. It is often used in XML layout files.

In general, the following is a specific example: Used as the background of the button in selector, which defines the general status of the button, the obtained focus status, and the status when the button is pressed, respectively, the Code is as follows:

Java code:

  1. <Button
  2. Android: layout_width = "wrap_content"
  3. Android: layout_height = "wrap_content"
  4. Android: text = "testshapebutton"
  5. Android: Background = "@ drawable/button_selector"
  6. />

Copy code

Let's take a look at the main. XML code:

Java code:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Selector
  3. Xmlns: Android = "http://schemas.android.com/apk/res/android">
  4. <Item Android: state_pressed = "true">
  5. <Shape>
  6. <! -- Gradient -->
  7. <Gradient
  8. Android: startcolor = "# ff8c00"
  9. Android: endcolor = "# ffffff"
  10. Android: TYPE = "RADIAL"
  11. Android: gradientradius = "50"/>
  12. <! -- Stroke -->
  13. <Stroke
  14. Android: width = "2dp"
  15. Android: color = "# dcdcdc"
  16. Android: dashwidth = "5dp"
  17. Android: dashgap = "3dp"/>
  18. <! -- Rounded corner -->
  19. <Corners
  20. Android: radius = "2dp"/>
  21. <Padding
  22. Android: Left = "10dp"
  23. Android: Top = "10dp"
  24. Android: Right = "10dp"
  25. Android: Bottom = "10dp"/>
  26. </Shape>
  27. </Item>
  28. <Item Android: state_focused = "true">
  29. <Shape>
  30. <Gradient
  31. Android: startcolor = "# ffc2b7"
  32. Android: endcolor = "# ffc2b7"
  33. Android: angle = "270" type = "parmname" text = "parmname"/>
  34. <Stroke
  35. Android: width = "2dp"
  36. Android: color = "# dcdcdc"/>
  37. <Corners
  38. Android: radius = "2dp"/>
  39. <Padding
  40. Android: Left = "10dp"
  41. Android: Top = "10dp"
  42. Android: Right = "10dp"
  43. Android: Bottom = "10dp"/>
  44. </Shape>
  45. </Item>
  46. <Item>
  47. <Shape>
  48. <Solid Android: color = "# ff9d77"/>
  49. <Stroke
  50. Android: width = "2dp"
  51. Android: color = "# fad3cf"/>
  52. <Corners
  53. Android: toprightradius = "5dp"
  54. Android: bottomleftradius = "5dp"
  55. Android: topleftradius = "0dp"
  56. Android: bottomrightradius = "0dp"
  57. />
  58. <Padding
  59. Android: Left = "10dp"
  60. Android: Top = "10dp"
  61. Android: Right = "10dp"
  62. Android: Bottom = "10dp"/>
  63. </Shape>
  64. </Item>
  65. </Selector>

Copy code

Running effect:

Upload at yesterday

Download Attachment(8.53 KB)

Upload at yesterday

Download Attachment(8.55 KB)

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.