Android drawable custom images and bug fixes

Source: Internet
Author: User

A long time to update the blog, so that a lot of things have been used after forgetting, there is no good record down, before the work is also so, used to forget, so I think it is necessary to use some of their own things, solve some of the problems recorded, so try to stick to a week to write a blog, A record of their own problems, but also share with the university, it is recommended that you also write a blog or notes or something, because in the work, the things they touch can not be just the beginning of their own things, such as Android, in fact, in the development of an app or usually work in the company, but also need to use a lot of things , and there may be a period of time to use other languages to develop, if they do not record, it is likely to learn the same as forget, this is not conducive to their own development, so suggest you still have to record their own things.

All right, nonsense, we have to go to the point today, the main thing today is two points, one point is to say a little bit of development, some drawable tips, there are I was in the development, found in 2.x Android inside a drawable bug

First, let's look at a picture


Everyone realizes the above button the first idea is to find the artist mm take a rounded background image, and then access the background can be, this can be, but in a variety of sizes of screen adaptation, so we will add a step, is to use 9path to the background image processing, this looks like doing too much trouble,

In fact, for some of the solid color of the picture or background, we can write an XML under the drawable directory can be, very simple, not only do not have to find art to picture, not to use their own 9path processing, very easy </p><p> Just like the corner button above, is a drawable background, the code is as follows

/drawable/btn_bg.xml


<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "/http Schemas.android.com/apk/res/android "    android:shape=" Rectangle ">        <corners android:radius=" 8DP "/ >        <solid android:color= "#ff0bd38a"/></shape>

Is it very simple and clear,

It's easier to use, just think of it as a picture.

<button         android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android: background= "@drawable/btn_bg"        android:text= "@string/app_name"        android:textsize= "25SP"        android: Textcolor= "@android: Color/white"/>

Now explain what is in the XML, in the above btn_bg.xml inside, in line 3rd, android:shape= "Rectangle", rectangle represents the following is a block, and "oval" oval, "line" lines, " Ring "rings, actually these three are not used much

The next thing is

Corners: Rounded Corners
The Android:radius is the radian of the angle, and the larger the value the greater the rounded.
We can also set the four angles to different angles by:

<corners     android:toprightradius= "20DP"       android:bottomleftradius= "20DP"       android:topleftradius= " 0DP "       

Then there is

Solid--fill.
Solid: Filled is the meaning of filling
ANDROID:COLOR Specifies the color of the fill


There's a couple of useless ones.

Stroke--strokes.
Stroke: Stroke
Android:width= the width of the "2DP" stroke, android:color the color of the stroke.
We can also make the stroke into a dashed form, set in the following way:
Android:dashwidth= "5DP"
android:dashgap= "3DP"
Where android:dashwidth represents the width of a horizontal line such as '-', android:dashgap represents the distance between them.

This is often used, I often use to make some border


This is the case, some need to the border, can write a drawable

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >        <stroke         android:width= "1DP"        android:color= "#ffaaaaaa"/>        <corners android:radius= "8DP"/>    <solid android:color= "@android: Color/white"/>    </ Shape>

The above is the border, there is a need to add a border can be written above a drawable


And there is.

Gradient--corresponds to the color gradient. StartColor, EndColor will not say much. Android:angle refers to the angle from which the change begins.
Gradient: Gradient
Android:startcolor and Android:endcolor are start and end colors respectively, Android:angle is the gradient angle and must be an integer multiple of 45.
In addition, the default mode of the gradient is android:type= "linear", that is, linear gradient, you can specify the gradient to radial gradient, android:type= "radial", radial gradient needs to specify the radius android:gradientradius= "50".

This is very little use, because it is too difficult to control, if the picture has a variety of colors, or to find the artist mm faster, their own tune, there may not be able to adjust, so it is not recommended to use it,

Android:angle: When the gradient, the most primitive, that is, android:angle= "0", is from left to right, according to the beginning color to the end color to render, android:angle= "90" is from the top to the down rendering, android:angle= " 180 "is rendered from right to left, android:angle=" 360 "and android:angle=" 0 "are the same, so here it should be, Rendering is based on the most original rendering swatch (the control inside as a piece can be rotated around the center of the board) around the center of the control to rotate the corresponding degree, that is, the value inside the android:angle is the angle required to rotate, but this angle of rotation must be 45 integer times

This is a bit complicated, or that sentence, not recommended to use, unless it really applies


And there is.

padding-Define the distance from the boundary of the content this is a good understanding, not much to say


Here is a demonstration of how this can be done with a custom progress bar

<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:id=" @android: Id/background "> <shape> <corners android:r adius= "5dip"/> <solid android:color= "#ffffffff"/> </shape> </item> <i  TEM android:id= "@android: id/secondaryprogress" > <clip> <shape> <corners android:radius= "5dip"/> <solid android:color= "#ff00ff00"/> </shape> < /clip> </item> <item android:id= "@android: id/progress" > <clip> <shape&                Gt        <corners android:radius= "5dip"/> <solid android:color= "#ff00ff00"/> </shape> </clip> </item> </layer-list>

Call

<progressbar        style= "? android:attr/progressbarstylehorizontal"        android:layout_width= "Match_parent"        android:layout_height= "2DP"        android:progress= "android:progressdrawable="        @drawable/progress_ Horizontal "/>

Actually, it's mainly

android:progressdrawable= "@drawable/progress_horizontal"

Just give us all the XML we've written, okay?

The effect is as follows



OK, let's talk about this, android2.x inside, this drawable inside a bug

On top of us, we said

Corners: Rounded Corners
The Android:radius is the radian of the angle, and the larger the value the greater the rounded.
We can also set the four angles to different angles by:

<corners     android:toprightradius= "20DP"       android:bottomleftradius= "20DP"       android:topleftradius= " 0DP "       

In fact, this is the source of the bug, in 2.x Android inside Bottomrightradius and Bottomlefttadius is reversed, you can look at the following several contrast pictures

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >        <corners         android:topleftradius= "18DP"        Android:toprightradius = "0DP"        android:bottomleftradius= "18DP"        android:bottomrightradius= "0DP"/>    <solid android: Color= "#ff0bd38a"/>    </shape>


This is the effect shown in the 2.x, you will find that it is around the reverse, you have to write it.

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >        <corners         android:topleftradius= "18DP"        Android:toprightradius = "0DP"        android:bottomleftradius= "0DP"        android:bottomrightradius= "18DP"/>    <solid Android:color = "#ff0bd38a"/>    </shape>

But if you write like this, it will show up at 3.x.


It's a turn, a pit.

Later I was in the Res directory to create a new DRAWABLE-V12 directory, put the correct wording, put it under this directory to be able to

Because after 3.x, will take drawable in the directory under the priority of the picture resources, so this can get the correct wording, the incorrect is placed in the drawable directory, to compatible with 2.x Android

This will give you the following effect.


This is a perfect solution to this bug.


In fact, we can in turn, if we find some of these bugs can be similar to solve the


Well, something a bit more, also a bit messy, if there is anything do not understand, please leave a message, or download the code to see, there is so I mentioned in the text of the XML

Okay, next time we say something about the Android reboot.


SOURCE download


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.