Android development art Exploration Study Notes (6), android art Exploration

Source: Internet
Author: User

Android development art Exploration Study Notes (6), android art Exploration

Chapter 6 Drawable of Android

Advantages of Drawable: it is easy to use, which is lower than the cost of custom view. Non-image Drawable occupies less space, which helps reduce the size of the APK installation package.

6.1Drawable Introduction

    There are many Drawable types. They all represent an image concept. Drawable is often used as the background of a view. Drawable is an abstract class. The internal width and height of Drawable can be obtained through the getIntrinsicWidth and getIntrinsicHeight methods. But not all Drawable have internal width and height.

6.2Drawable Classification

      6.2.1 BitmapDrawable

      BitmapDrawableb indicates an image. Below is a common attribute (Description in xml format)     

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@mipmap/ic_launcher"    android:antialias="true|false"    android:dither="true|false"    android:filter="true|false"    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"    android:mipMap="true|false"    android:tileMode="disabled|clamp|repeat|mirror"/>

Android: A required property of the src image resource ID.

Android: antialias anti-tooth feature

Android: dither jitter effect (optimizes the image display effect)

Android: filter effect (the function of optimizing the image display effect)

Android: gravity

Android: mipMap texture ing (not commonly used)

Android: tileMode tile mode (gravity attribute is ignored when this mode is enabled) there are four modes: disabled -- disable tile mode; repeat -- tiled effect in the horizontal and vertical directions; clamp-pixels around the image are extended to the surrounding area. Mirror-a mirror projection effect in both the horizontal and vertical directions.

NinePatchDrawable indicates an image in. 9 format. The xml format is described as follows:

<?xml version="1.0" encoding="utf-8"?><nine-patch xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@mipmap/ic_launcher"    android:dither="true|false"/>

      6.2.2 ShapeDrawable

ShapeDrawable indicates that the image constructed by color can be either solid or gradient.

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

Android: shape: rectangle (rectangle), oval (elliptic), line (horizontal line), ring (ring), default rectangle, line and ring must have <stroke> labels

<Corners> Angle of the four corners. It is only applicable to rectangles.

<Gradient> gradient effect, mutually exclusive with the <solid> label

<Solid> solid color Filling

<Stroke> stroke

<Padding> blank View containing it

<Size> the size of a shape, indicating the inherent size of the shape, does not represent the final display size.

 

      6.2.3 LayerDrawable

LayerDrawable represents a hierarchical set of Drawable, which is used to achieve the superposition effect.

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@mipmap/ic_launcher"        android:id="@+id/drawable"        android:top="dimension"        android:right="dimension"        android:bottom="dimension"        android:left="dimension">    </item></layer-list>

 

A <layer-list> can contain multiple items. Each item represents a drawable, and the items below will overwrite the items above. Through reasonable layering, some special overlay effects can be achieved. The following example implements the text input box effect.

<?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="#0ac39e" />        </shape>    </item>    <item android:bottom="6dip">        <shape android:shape="rectangle">            <solid android:color="#ffffff" />        </shape>    </item>    <item        android:bottom="1dp"        android:left="1dp"        android:right="1dp">        <shape android:shape="rectangle">            <solid android:color="#ffffff" />        </shape>    </item></layer-list>

 

      6.2.4 StateListDrawable            

It is mainly used to set the background of a clickable View. The most common is the Button, which corresponds to the <selector> label. The XML format is as follows:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"    android:constantSize="true|false"    android:dither="true|false"    android:variablePadding="true|false">    <item        android:drawable="@mipmap/ic_launcher"        android:state_pressed="true|false"        android:state_focused="true|false"        android:state_selected="true|false"        android:state_checked="true|false"        android:state_enabled="true|false"        /></selector>

 

Items have many States. The preceding columns only take several common states.

Android: constantSize whether the inherent size of StateListDrawable does not change with its status.

Android: dither: whether to enable Jitter

Android: variablePadding whether the padding of StateListDrawable changes with its status.

Android: state_pressed press status

Android: state_focused get focus

Android: state_selected

Android: state_checked selected

 

 

Android: state_enabled available

 

The default item is generally placed at the last entry of the selector without any status.

 

      6.2.5 LevelListDrawable

A Drawable set. Each Drawable has a level. Based on different levels, LevelListDrawable switches the corresponding Drawable. The syntax of xml format is as follows:

<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@mipmap/ic_launcher"        android:maxLevel="integer"        android:minLevel="integer" /></level-list>

 

Level 1: 0 ~ 10000 as the background of the View, you can set different levels through the setLevel method of Drawable to switch the specific Drawable; as the foreground Drawable of the ImageView, you can use the setImageLevel method of ImageView to switch Drawable.

 

      6.2.6 TransitionDrawable

Implements the fade-in and fade-out effect between two Drawable, corresponding to the <transition> label. The XML format syntax is as follows:

<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@mipmap/ic_launcher"        android:id="@+id/resource_name"        android:top="dimension"        android:bottom="dimension"        android:left="dimension"        android:right="dimension"        /></transition>

 

Instance: Switch the background of TextView

<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/drawable1" />    <item android:drawable="@mipmap/drawable2" /></transition>
  <TextView        android:background="@drawable/test9"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />
        TextView textView=(TextView)findViewById(R.id.test);        TransitionDrawable drawable=(TransitionDrawable)textView.getBackground();        drawable.startTransition(1000);

      6.2.7 InsetDrawable

You can embed other Drawable items into your own, corresponding to the <inset> label. When a View wants its own background to be smaller than its actual region, you can use InsetDrawable to implement it. The XML format syntax is as follows:

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:insetTop="dimension"    android:insetBottom="dimension"    android:insetLeft="dimension"    android:insetRight="dimension"></inset>

      6.2.8 ScaleDrawable

Scales the specified Drawable to a certain scale based on its level, corresponding to the <scale> label. The XML syntax is as follows:

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:scaleGravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"    android:scaleHeight="percentage"    android:scaleWidth="percentage"/>

When using ScaleDrawable, you also need to set the ScaleDrawable level to a value greater than 0 and less than 10000; otherwise, the result will not be displayed.

        ScaleDrawable scaleDrawable=(ScaleDrawable)textView.getBackground();        scaleDrawable.setLevel(1);

       6.2.9 ClipDrawable

You can crop another Drawable based on your current level. The cropping direction is controlled by the android: clipOrientation and android: gravity attributes. The XML property syntax is as follows:

<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android"    android:clipOrientation="horizontal|vertical"    android:drawable="@mipmap/ic_launcher"    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" />

 

 

The level range is also 0 ~ Crop, 0 indicates full cropping, and 10000 indicates no cropping.

   ClipDrawable clipDrawable=(ClipDrawable)textView.getBackground();   clipDrawable.setLevel(5000);

 

 

    6.3 custom Drawable     

It is used only in some special cases. Note the getIntrinsicWidth and getIntrinsicHeight methods (p262 ).

 

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.