Android LayerDrawable超詳細(xì)講解
1. 前言
Android LayerDrawble 包含一個(gè)Drawable數(shù)組,系統(tǒng)將會(huì)按照這些Drawable對(duì)象的數(shù)組順序來(lái)繪制他們,索引最大的 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可以這樣簡(jiǎn)單的來(lái)理解,使用它可以將多個(gè)圖片疊加起來(lái),可以將用shape和selector實(shí)現(xiàn)的效果疊加起來(lái)
2. 實(shí)例
該控件比較使用比較簡(jiǎn)單,我們直接通過(guò)例子來(lái)演示
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>
說(shuō)說(shuō) 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后繪制出來(lái)
第三個(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為圓形,通過(guò)第一和第二個(gè)顯示規(guī)則,此時(shí)的ImageView的寬高為80dp, 然后距離ImageView容器的top bottom left right 邊距離15dp 繪制出來(lái)
效果圖:
3. 第三種,通過(guò)層視圖顯示陰影效果
<?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)文章
Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要介紹了Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
這篇文章主要介紹了Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Android ListView與ScrollView沖突的解決方法總結(jié)
這篇文章主要介紹了Android ListView與ScrollView沖突的解決方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)為GridView添加邊框效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)為GridView添加邊框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Flutter 中的PageStorage小部件使用及最佳實(shí)踐
在Flutter中,PageStorage小部件提供了一種方法來(lái)保存和恢復(fù)頁(yè)面間的信息,這對(duì)于具有多個(gè)頁(yè)面且需要在這些頁(yè)面之間共享狀態(tài)的應(yīng)用程序非常有用,本文將詳細(xì)介紹PageStorage的用途、如何使用它以及一些最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-05-05Android 聽(tīng)筒模式的具體實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 聽(tīng)筒模式的具體實(shí)現(xiàn)實(shí)例,有需要的朋友可以參考一下2013-12-12Android開(kāi)發(fā)基礎(chǔ)之創(chuàng)建啟動(dòng)界面Splash Screen的方法
這篇文章主要介紹了Android開(kāi)發(fā)基礎(chǔ)之創(chuàng)建啟動(dòng)界面Splash Screen的方法,以實(shí)例形式較為詳細(xì)的分析了Android定制啟動(dòng)界面的布局及功能實(shí)現(xiàn)相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10