Android LayerDrawable超詳細(xì)講解
1. 前言
Android LayerDrawble 包含一個(gè)Drawable數(shù)組,系統(tǒng)將會(huì)按照這些Drawable對(duì)象的數(shù)組順序來繪制他們,索引最大的 Drawable 對(duì)象將會(huì)被繪制在最上面。
LayerDrawable對(duì)象的xml文件的根元素是<layer-list>, 該元素內(nèi)部包含多個(gè)<item>。item標(biāo)簽內(nèi)部可以指定drawable、id和位置相關(guān)屬性。
layer-list可以進(jìn)一步擴(kuò)展對(duì)shape和selector的使用,對(duì)layer-list可以這樣簡單的來理解,使用它可以將多個(gè)圖片疊加起來,可以將用shape和selector實(shí)現(xiàn)的效果疊加起來
2. 實(shí)例
該控件比較使用比較簡單,我們直接通過例子來演示
activity_main.xml ,有三個(gè)ImageView 對(duì)象:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/layer_test"
/>
<ImageView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layer_icon"
/>
<ImageView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layer_icon2"
/>
</LinearLayout>1. 第一個(gè) ImageView 我們定義好 寬度和高度 150dp, 看看里面的內(nèi)容:
<?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="#0000ff"/>
</shape>
</item>
<item android:left="15dp" android:end="15dp" android:top="15dp" android:bottom="15dp">
<shape android:shape="oval">
<size android:height="10dp" android:width="10dp"/>
<solid android:color="#00ff00"/>
</shape>
</item>
<item android:left="45dp" android:end="45dp" android:top="45dp" android:bottom="45dp">
<shape android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
</item>
</layer-list>說說 item的4個(gè)屬性,作用同控件中的margin屬性
- android:top 頂部的偏移量
- android:bottom 底部的偏移量
- android:left 左邊的偏移量
- android:right 右邊的偏移量
我們定義的ImageView的寬高150dp ,
第一個(gè)item 矩形框 在最底層,鋪滿整個(gè)寬高
第二個(gè)item為圓形,距離ImageView容器的top bottom left right 邊距離為 15dp
注意:圓形定義的<size android:height="10dp" android:width="10dp"/>這里是不生效的,是以容器寬高150dp為基準(zhǔn), 上下左右偏移15dp后繪制出來
第三個(gè)item為矩形,距離ImageView容器的top bottom left right 邊距離為 45dp
效果圖:

2. 第2個(gè)ImageView,不定義寬高,讓里面圖片去填充顯示:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:width="50dp" android:height="50dp"/>
<solid android:color="#0000ff"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:width="80dp" android:height="80dp"/>
<solid android:color="#ff0000"/>
</shape>
</item>
<item android:left="15dp" android:end="15dp" android:top="15dp" android:bottom="15dp">
<shape android:shape="oval">
<solid android:color="#00ff00"/>
</shape>
</item>
</layer-list>第一個(gè)item為矩形,寬高為 50dp
第二個(gè)item也為矩形, 寬高為80dp 那么根據(jù)顯示規(guī)則,后面的item顯示在上面,所以整個(gè)ImageView的寬高變?yōu)?80dp了
第三個(gè)item為圓形,通過第一和第二個(gè)顯示規(guī)則,此時(shí)的ImageView的寬高為80dp, 然后距離ImageView容器的top bottom left right 邊距離15dp 繪制出來
效果圖:

3. 第三種,通過層視圖顯示陰影效果
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:width="100dp"
android:height="20dp"></size>
<solid android:color="#000"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
<item android:left="3dp" android:bottom="3dp">
<shape android:shape="rectangle">
<solid android:color="#f7f6f6"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
</layer-list>效果圖:

到此這篇關(guān)于Android LayerDrawable超詳細(xì)講解的文章就介紹到這了,更多相關(guān)Android LayerDrawable內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中實(shí)現(xiàn)在矩形框中輸入文字顯示剩余字?jǐn)?shù)的功能
在矩形輸入框框中輸入文字顯示剩余字?jǐn)?shù)的功能在app開發(fā)中經(jīng)常會(huì)見到,今天小編就通過實(shí)例代碼給大家分享android實(shí)現(xiàn)輸入框提示剩余字?jǐn)?shù)功能,代碼簡單易懂,需要的朋友參考下吧2017-04-04
Android TextView實(shí)現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android TextView跑馬燈效果實(shí)現(xiàn)方法,涉及Android布局文件中相關(guān)屬性的設(shè)置技巧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Android ListView與ScrollView沖突的解決方法總結(jié)
這篇文章主要介紹了Android ListView與ScrollView沖突的解決方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android三種實(shí)現(xiàn)定時(shí)器的方法
本文給大家分享了3種Android實(shí)現(xiàn)定時(shí)器的方法的示例,,需要的朋友可以參考下2015-02-02
android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放
本篇文章實(shí)現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Android ViewDragHelper完全解析 自定義ViewGroup神器
這篇文章主要為大家詳細(xì)介紹了Android ViewDragHelper完全解析,自定義ViewGroup神器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
android自定義Camera實(shí)現(xiàn)錄像和拍照
這篇文章主要為大家詳細(xì)介紹了android自定義Camera實(shí)現(xiàn)錄像和拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android?ContentObserver?監(jiān)聽短信思路詳解
ContentObserver允許在Android中監(jiān)控特定數(shù)據(jù)的變化,可用于短信等應(yīng)用的數(shù)據(jù)監(jiān)聽,開發(fā)者可通過繼承ContentObserver并實(shí)現(xiàn)onChange方法來定義當(dāng)目標(biāo)內(nèi)容變化時(shí)的響應(yīng)行為,感興趣的朋友一起看看吧2024-09-09
Android5.0以上版本錄屏實(shí)現(xiàn)代碼(完整代碼)
這篇文章主要介紹了Android5.0以上版本錄屏實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01

