Android: Background selector and shape Details

Keywords: Android xml encoding Attribute

This week is the school's innovation and entrepreneurship week, a week without classes, just have time to put Android basic knowledge out of order.
When I first learned Android, all the interfaces were ugly, and then I learned the background selector, which greatly improved the situation... Well, in fact, it's a lot of trouble to write, because in as or eclipse, some content is not prompted and can't be automatically completed, which makes me a little confused that I often knock code according to the prompt...
so... Here's just a summary for pasting and duplicating later... Hey.

The so-called state selector is that the control (view or viewgroup) defines different realistic effects according to different selected states. We can switch the background property of the control in the specified state, so as to achieve the goal of brilliant effect.

Common attributes:

attribute state
android:drawable Place a drawable resource
android:state_pressed Is the current view pressed, such as a button touch or click?
android:state_focused The official explanation is that when the current view gets the focus, for example, the user chooses a text box.
android:state_hovered Whether the cursor hovers or not is usually the same as focused state, which is a new feature of 4.0.
android:state_selected When selected, it is not exactly the same as focus state. For example, when a list view is selected, each sub-component in it may be selected through the direction key.
android:state_checkable Can the current view be checked? For example, RadioButton can be checked.
android:state_checked checked, e.g. a RadioButton can be check ed.
android:state_enabled When the current view can be clicked or triggered, it can accept the touch or click event.
android:state_activated Activated, the official explanation is set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.
android:state_window_focused Whether the application is in the foreground when the form in which the current view is located is in focus, or when a notification bar is pulled down or a dialog box pops up, the application is not in the foreground.

Some need to add android:focusable and android:clickable as true to get focus

button_select.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/play_button"></item>
    <item android:state_focused="true" android:drawable="@drawable/play_button"></item>
    <item android:drawable="@drawable/button"></item>
</selector>  

Default effect of button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#E53B36"/>
</shape>

play_button.xml Click Effect

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#E8B714"/>
</shape>

Common checkbox settings:

<?xml version="1.0" encoding="UTF-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false"
     android:state_enabled="true" 
     android:state_checked="true" 
     android:drawable="@drawable/btn_check_on" />
    <item android:state_window_focused="false"
     android:state_enabled="true" 
     android:state_checked="false" 
     android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" 
    android:state_checked="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/btn_check_off_pressed" />
    <item android:state_focused="true" 
    android:state_enabled="true" 
    android:state_checked="true"
    android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_focused="true" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off_selected" />
    <item android:state_enabled="true"
     android:state_checked="false" 
     android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" 
     android:state_checked="true"
     android:drawable="@drawable/btn_check_on" />
</selector>

Common ImageButton settings:

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button2_down" />
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/button2_over" />
    <item android:state_enabled="true" android:drawable="@drawable/button2" />
</selector>

Common Button settings:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/login_input" />
    <item android:state_pressed="true" android:drawable="@drawable/login_input" />
    <item android:state_focused="true" android:drawable="@drawable/input_over" />
</selector>

Setting TextView

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="@color/gray" android:state_pressed="false" android:state_enabled="true"/>
    <item android:color="@color/white" android:state_pressed="true" android:state_enabled="true"/>
    <item android:color="@color/gray" android:state_enabled="false"/>
</selector>

Drable resources are the focus of the state selector. First of all, when drawble resources are more complex, we can generally use. 9 patch pictures to replace, perfect fit. But when it's relatively simple, we can write the drawble resource of the background ourselves. So how do you customize a drawble resource? Let's talk about the use of shape s under android.
Let's start with an example:

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="" >  

    <!-- fillet -->  
    <corners  
        android:radius="10dp"  
        android:topLeftRadius="" />  
    <!-- Stroke -->  
    <stroke  
        android:dashGap=""  
        android:dashWidth=""  
        android:width="2dp"  
        android:color="@color/darkgray" />  
    <!-- solid -->  
    <solid android:color="#c4c4c4" />  

    <!-- Size -->  
    <size  
        android:height=""  
        android:width="" />  
    <!-- mm Color Grad -->  

    <gradient  
        android:angle=""  
        android:centerColor=""  
        android:endColor=""  
        android:gradientRadius=""  
        android:startColor=""  
        android:type="" />  
</shape>  

The first is the initial shape tag. There is a shape attribute in shape, which can be set or not. When not set, the default is rectangle. There are four values to set:
1. rectangle rectangle
2. An oval ellipse is a circle when its width and height are set to the same value.
3. line Linear Shape
4. Ring ring ring can be used as a hint of rotation when data refresh
When you set it to ring ring ring, you also need to set several attributes

 android:innerRadiusRatio="3"  //Floating-point data representing the radius of an inner ring in terms of the width ratio of the ring
 android:thicknessRatio="8"    //Floating-point data, in which the width ratio of the ring is used to represent the thickness of the ring
 android:useLevel="false"  //If used as LevlListDrawable, it is true and the rest false.

Next, define the nodes in the shape tag

<!-- fillet -->  
   <corners  
       android:radius="10dp"  
       android:topLeftRadius="" />  

This represents rounded corners, which can be set to the size of four corners at one time. We also set the size of four angles separately. Here we only write the whole one and the upper left one.

<!-- Stroke -->  
<stroke  
   android:dashGap=""  
   android:dashWidth=""  
   android:width="2dp"  
   android:color="@color/darkgray" />  

This means to draw a line at the boundary. Width denotes the thickness of lines, color denotes the color, dashWidth and dashGap are used to draw dotted lines, dashWidth denotes the width of dotted lines, dashGap denotes the interval of dotted lines.

<!-- solid -->  
  <solid android:color="#c4c4c4" />  

There's nothing to say about this.

<!-- Size -->  
 <size  
     android:height=""  
     android:width="" />  

<!-- mm Color Grad -->    
 <gradient  
     android:angle=""  
     android:centerColor=""  
     android:endColor=""  
     android:gradientRadius=""  
     android:startColor=""  
     android:centerX=""  
     android:centerY=""  
     android:gradientRadius=""  
     android:type="" />  

The color gradient needs a good explanation.
angle denotes the starting position of color gradient, 0 denotes left to right and then counterclockwise, 90 denotes top to bottom, and so on. angle must be an integer multiple of 45 points.
StartColor endColor centerColor, the color value of the color gradient process.
Type, color gradient type, has three values
1. linear. linear gradient. This is the default value.
2. radial, Radioactive Gradient Radius, which is used in conjunction with the android:gradientRadius attribute. android:gradientRadius represents the radius of Radioactive Gradient Radius.
3. Swep, Scanning Stone Gradient, just like the effect of radar scanning.
Center X, center Y, denotes the relative position of the coordinates of points X and Y of the gradient center.

Posted by tendrousbeastie on Sun, 30 Jun 2019 17:21:58 -0700