Original address: http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/
Android Development Notes-rounded corners and borders
In the development of the Android interface, we often want it to be as graceful as possible and professional. So you looked at other applications, wow, lots of borders and rounded corners. Do you also want to add borders and rounded corners to your app? Uh...... How do we do that? If you are running from the Web front to Android, then I think you must have thought of three different solutions. If you use a picture instead, with CSS3 definition, with JS painting. In Android, there are similar uses, this article will briefly introduce the two Android rounded corners and the implementation of the border.
1 pic
In Android, setting a background for a control (or view) is primarily background:xxx done through attributes. The background parameter is generally a drawable resource. Drawable can be a normal picture, or it can be a 9 patch picture, or it can be an XML file. The simplest way to set a border for a control is to set the background as a pre-designed background chart with rounded corners and borders. For example, the following picture:
But you will soon find a drawback: the flexibility is poor ! Yes, fixed-size images are hard to resize based on the contents of the control. It was determined the day it was made! In other words, it's hard to use this diagram only to deal with controls that have the same style but vary in size. In order to add rounded corners and borders to all controls, you have to carefully calculate their size and then create a background image one by one! God, this is stupid. Once you encounter a variable-sized control, this method breaks the dish. Also, a large number of background images will make your installation package swell quickly. Uh...... And how do you deal with Android devices with a wide range of resolutions?
So, you need ... A different method.
The comparison of a solution for the general public is ninepatch. It's no exaggeration to say that 9 patch is a powerful tool to solve adaptive problems in Android. Introduction and use You can look here and here.
There are a number of benefits to using 9 patch images, such as lightening the artwork pressure, reducing the amount of UI code, and reducing memory usage ... Summed up is: Save time and effort, Dick burst.
So you might do that when you give a fillet and a border.
Of course, 9 patches can do much more than this, such as making an adaptive dialog box or something.
2 XML definitions
I think most programmers like to use code to solve problems. The reasons are as follows:
- More cool with the code.
- I do not art, I will say go?
OK, here's the good stuff.
2.1 Basic rounded corners, borders
In addition to supporting the original image resources, Android is a good thing is to use XML files to define some simple graphics. This is a bit like web CSS, but it's not as powerful as the XML implementation of Css3,android, for example, the border is either around or not around (we'll talk about it later). The portal for XML drawable is here.
It's easy to draw a graph with a gray border and rounded corners, and add an XML under the drawable Resource directory:
<?xml version= "1.0" encoding= "Utf-8"? ><!--shape If you do not declare a shape, the default is square-->< Span class= "NT" ><shape xmlns:android= "http://schemas.android.com/apk/res/ Android "> <corners android:radius=" 5.0DP "/> <!--rounded corners, you can also set different values for different angles--<solid android:color= "#FFFFFF" /> <!--the shape's fill color-- Span class= "NT" ><stroke android:width= "1DP" android:color=< Span class= "s" > "#CCCCCC" /> <!--border width and color--></shape
In the place where you need to use this, like a view, set the background on the line.
2.2 "Free border"
The current version of the Android SDK does not give the stroke the bottom, left, and right properties, which means that you cannot use it to make the rectangle border less than 4 bars. Oh, that's a pity. What do we do? Someone thought about the layer List hack. There are many such tricks on the StackOverflow.
To implement only the left,right and top borders, we can write this:
<?xml version= "1.0" encoding= "Utf-8"?><layer-listXmlns:android="Http://schemas.android.com/apk/res/android"><item><shapeAndroid:shape="Rectangle"><strokeandroid:width= "1DP" android:color= "@color/ Card_stroke "/> </shape> </item> <item android:left= "2DP" android:right=< Span class= "s" > "2DP" android:top= "2DP" > < !--in practical use I found that 1DP does not reach the display effect, and 2DP can display the border--<shape android:shape= "rectangle" > <solid android:color= "@color/solid_white" /> </shape> </item ></LAYER-LIST>
The principle is almost like this:
Weird is theoretically as long as the offset as long as 1DP can display the 1DP broadband border, but I in the ListView experiment to find no, replace 2DP. Can some students explain?
If you want to fillet the shape, you only need to add a
<corners android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" />
It is worth noting that the radius of the two shape is set to make sure that the front layer does not block the back.
3 Summary
To achieve rounded corners and borders in Android, it's a simple way to do this: pictures, XML, that's pretty much how it's done. It is also useful for Java code to call the Draw method, but I have not studied it. They each have their own merits. With pictures, can control things more, with the code to modify to compare the other. The final word is the efficiency of the two methods. On this issue, I have doubts and no special comparisons. But the intuitive feeling is ... Well, there's nothing to feel.
"Turn" Android development notes-rounded corners and borders