android 官网 : 图层列表 LayerDrawable / layer-list
LayerDrawable
是管理其他可绘制对象数组的可绘制对象。列表中的每个可绘制对象均按照列表顺序绘制。列表中的最后一个可绘制对象绘于顶部。
每个可绘制对象均由单个 <layer-list> 元素内的 <item> 元素表示。
文件位置:
res/drawable/filename.xml
文件名即为资源 ID
编译后的资源数据类型:
指向 LayerDrawable
的资源指针
资源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
语法:
<?xml version="1.0" encoding="utf-8"?>
<layer-listxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:drawable="@[package:]drawable/drawable_resource"android:id="@[+][package:]id/resource_name"android:top="dimension"android:right="dimension"android:bottom="dimension"android:left="dimension" />
</layer-list>
元素:
<layer-list>
必需。该元素必须是根元素。包含一个或多个 <item> 元素。
属性:xmlns:android字符串。必需。定义 XML 命名空间,该命名空间必须为 "http://schemas.android.com/apk/res/android"。<item>
定义放于图层可绘制对象中的可绘制对象,位置由其属性定义。
必须为 <layer-list> 元素的子项。接受子级 <bitmap> 元素。
属性:android:drawable可绘制资源。必需。引用可绘制资源。android:id资源 ID。此可绘制对象的唯一资源 ID。如要为此项创建新的资源 ID,请使用以下形式:"@+id/name"。加号表示其是作为新 ID 创建的。您可以使用此 ID 检索和修改拥有 View.findViewById() 或 Activity.findViewById() 的可绘制对象。android:top尺寸。顶部偏移量,采用尺寸值或尺寸资源的形式。android:right尺寸。右侧偏移量,采用尺寸值或尺寸资源的形式。android:bottom尺寸。底部偏移量,采用尺寸值或尺寸资源的形式。android:left尺寸。左侧偏移量,采用尺寸值或尺寸资源的形式。默认情况下,所有可绘制项都会缩放以适应所包含视图的大小。
因此,将图像放在图层列表中的不同位置可能会增大视图的大小,并且有些图像会相应地缩放。为避免缩放列表中的项,请在 <item> 元素内使用 <bitmap> 元素指定可绘制对象,
并且针对某些不缩放的项(例如 "center")定义重心。
例如,以下 <item> 定义了缩放以适应其容器视图的项:<item android:drawable="@drawable/image" />为避免缩放,以下示例使用重心居中的 <bitmap> 元素:<item><bitmap android:src="@drawable/image"android:gravity="center" />
</item>
示例:
保存在 res/drawable/layers.xml 的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item><bitmap android:src="@drawable/android_red"android:gravity="center" /></item><item android:top="10dp" android:left="10dp"><bitmap android:src="@drawable/android_green"android:gravity="center" /></item><item android:top="20dp" android:left="20dp"><bitmap android:src="@drawable/android_blue"android:gravity="center" /></item>
</layer-list>
此示例使用嵌套的 <bitmap> 元素为每个具有 "center" 重心的项定义可绘制资源。
这可确保不会有图像为适应容器的大小而缩放,因为偏移图像会造成大小调整。
此布局 XML 会将可绘制对象应用到视图:
<ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/layers" />
结果是一堆不断偏移的图像:
demo1 :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!-- 圆环 --><item><shapeandroid:dither="true"android:shape="oval"><solid android:color="@color/white" /><strokeandroid:width="1dp"android:color="@android:color/holo_blue_dark" /></shape></item><!-- 圆心 --><itemandroid:bottom="20dp"android:left="20dp"android:right="20dp"android:top="20dp"><shape android:shape="oval"><solid android:color="@android:color/holo_blue_dark" /><sizeandroid:width="30dp"android:height="30dp" /></shape></item>
</layer-list>
效果:
demo2 : 可更新seekbar thumb的颜色
定义 icon_thumb.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/layer_drawable_1"><bitmapandroid:gravity="center"android:src="@drawable/slider_color" /></item><item android:id="@+id/layer_drawable_2"><bitmapandroid:gravity="center"android:src="@drawable/slider_border" /></item>
</layer-list>
drawable-xhdpi/slider_color :
drawable-xhdpi/slider_border :
引用 :
<SeekBarandroid:id="@+id/seekbar1"android:layout_width="368dp"android:layout_height="44dp"android:layout_marginVertical="20dp"android:background="@drawable/bg_color127"android:max="126"android:progress="0"android:progressDrawable="@android:color/transparent"android:thumb="@drawable/icon_thumb"android:thumbOffset="23dp" />
改变thumb的图层颜色
注意: 如果单纯改变整个thumb,直接 seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_ATOP); 即可
下面是改变thumb的图层中某层的颜色:
SeekBar seekBar = (SeekBar) this.findViewById(R.id.seekbar1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {String colorStr = AmbientUtil.COLORS_127[progress];int color = Color.parseColor(colorStr);Drawable thumb = seekBar.getThumb();if (thumb instanceof LayerDrawable) {LayerDrawable thumb1 = (LayerDrawable) thumb;int numberOfLayers = thumb1.getNumberOfLayers();Drawable drawableByLayerId1 = thumb1.findDrawableByLayerId(R.id.layer_drawable_1);drawableByLayerId1.setColorFilter(color, PorterDuff.Mode.SRC_IN);Log.e(TAG, "onProgressChanged: =================" + numberOfLayers);}//int color = Color.parseColor(colorStr);//Drawable thumb = mColorPickerSeekbar.getThumb();//if (thumb instanceof LayerDrawable) {// LayerDrawable layerDrawable = (LayerDrawable) thumb;// Drawable drawable = layerDrawable.findDrawableByLayerId(R.id.layer_drawable_1);// drawable.setColorFilter(new SimpleColorFilter(color));//}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}
});
效果 :
其他:
public class AmbientUtil {public static final String[] COLORS_127 = new String[]{"#FFDF85FF", "#FFD869FF", "#FFD253FF", "#FFD038FF", "#FFB123DD", "#FFA24DD7", "#FF8B4FE3", "#FF8A20F9", "#FF7544D8", "#FF5C39DB","#FF4520E0", "#FF0024EC", "#FF193AFF", "#FF225CFE", "#FF0277F8", "#FF008BFE", "#FF3690FC", "#FF2A99FE", "#FF00A3FE", "#FF02A7FF","#FF19ADFF", "#FF0CB2FC", "#FF04B4F2", "#FF0AB7F3", "#FF03C2EF", "#FF01CFF1", "#FF02D7F4", "#FF02DBFE", "#FF22DFFE", "#FF44E8FE","#FF02F0FC", "#FF01FEFF", "#FF38FFFE", "#FF19FDFE", "#FF01FCFF", "#FF44FBFE", "#FF76FEFE", "#FF90FEFB", "#FF8FFFF4", "#FFA0FFF5","#FF8BFFF9", "#FF75FFFC", "#FF85FFF6", "#FF9DFFF9", "#FFAAFFF0", "#FFAAFFE6", "#FFAAFFDC", "#FFA8FAD0", "#FF96F2C0", "#FF7EF6BA","#FF6EEFB1", "#FF6EEEA1", "#FF32ED96", "#FF2BED7E", "#FF15E658", "#FF18E032", "#FF53F204", "#FF6AFB29", "#FF94FF43", "#FFAAFB68","#FFC0F987", "#FFC8FD8D", "#FFD2FA78", "#FFDFF974", "#FFE6FF74", "#FFF2FF74", "#FFF8FF8D", "#FFFFFD9B", "#FFFDFF96", "#FFFFFC87","#FFFFF596", "#FFFFE28C", "#FFFEE687", "#FFFEF086", "#FFFCF588", "#FFFEFC85", "#FFFFFF82", "#FFFCFA66", "#FFFCFC64", "#FFFCFE4F","#FFFFFC4F", "#FFFCFC55", "#FFFCFA6D", "#FFFFF654", "#FFFEF200", "#FFFAE800", "#FFF2D144", "#FFF1C91E", "#FFF1BD29", "#FFF1B934","#FFF3B34B", "#FFF9A845", "#FFFBA04B", "#FFFB964B", "#FFFB8B4B", "#FFFB864D", "#FFFB7F4E", "#FFFC7B4F", "#FFFB774F", "#FFFF724F","#FFFF6C4E", "#FFFF4B4E", "#FFFF364E", "#FFFF0048", "#FFFF0C52", "#FFFF2563", "#FFFA3871", "#FFF7387A", "#FFFA4B7A", "#FFFF587D","#FFFC6587", "#FFFD718A", "#FFFF8599", "#FFFF9FAF", "#FFFFC9D2", "#FFFFFFFF", "#FFFBFFFF", "#FFF3FEFF", "#FFEBFEFF", "#FFEAFDFF","#FFD9FAFF", "#FFC9F7FF", "#FFB3FAFF", "#FFAEF8FF", "#FF97F5FF", "#FF75F2FF", "#FF65F0FF",};
}
bg_color127 :
<vector xmlns:android="http://schemas.android.com/apk/res/android"xmlns:aapt="http://schemas.android.com/aapt"android:width="368dp"android:height="40dp"android:viewportWidth="368"android:viewportHeight="40"><path android:pathData="M0,5L368,5L368,35L0,35L0,5"><aapt:attr name="android:fillColor"><gradientandroid:endX="368"android:endY="40"android:startX="0"android:startY="40"android:type="linear"><itemandroid:color="#FFDF85FF"android:offset="0.040983606557377046" /><itemandroid:color="#FFD869FF"android:offset="0.048269581056466296" /><itemandroid:color="#FFD253FF"android:offset="0.05555555555555555" /><itemandroid:color="#FFD038FF"android:offset="0.06284153005464481" /><itemandroid:color="#FFB123DD"android:offset="0.07012750455373407" /><itemandroid:color="#FFA24DD7"android:offset="0.07741347905282331" /><itemandroid:color="#FF8B4FE3"android:offset="0.08469945355191257" /><itemandroid:color="#FF8A20F9"android:offset="0.09198542805100182" /><itemandroid:color="#FF7544D8"android:offset="0.09927140255009106" /><itemandroid:color="#FF5C39DB"android:offset="0.10655737704918034" /><itemandroid:color="#FF4520E0"android:offset="0.11384335154826958" /><itemandroid:color="#FF0024EC"android:offset="0.12112932604735882" /><itemandroid:color="#FF193AFF"android:offset="0.1284153005464481" /><itemandroid:color="#FF225CFE"android:offset="0.13570127504553733" /><itemandroid:color="#FF0277F8"android:offset="0.14298724954462658" /><itemandroid:color="#FF008BFE"android:offset="0.15027322404371585" /><itemandroid:color="#FF3690FC"android:offset="0.1575591985428051" /><itemandroid:color="#FF2A99FE"android:offset="0.16484517304189436" /><itemandroid:color="#FF00A3FE"android:offset="0.1721311475409836" /><itemandroid:color="#FF02A7FF"android:offset="0.17941712204007285" /><itemandroid:color="#FF19ADFF"android:offset="0.18670309653916212" /><itemandroid:color="#FF0CB2FC"android:offset="0.19398907103825136" /><itemandroid:color="#FF04B4F2"android:offset="0.2012750455373406" /><itemandroid:color="#FF0AB7F3"android:offset="0.20856102003642987" /><itemandroid:color="#FF03C2EF"android:offset="0.21584699453551912" /><itemandroid:color="#FF01CFF1"android:offset="0.22313296903460836" /><itemandroid:color="#FF02D7F4"android:offset="0.23041894353369763" /><itemandroid:color="#FF02DBFE"android:offset="0.23770491803278687" /><itemandroid:color="#FF22DFFE"android:offset="0.24499089253187611" /><itemandroid:color="#FF44E8FE"android:offset="0.2522768670309654" /><itemandroid:color="#FF02F0FC"android:offset="0.25956284153005466" /><itemandroid:color="#FF01FEFF"android:offset="0.2668488160291439" /><itemandroid:color="#FF38FFFE"android:offset="0.27413479052823314" /><itemandroid:color="#FF19FDFE"android:offset="0.2814207650273224" /><itemandroid:color="#FF01FCFF"android:offset="0.2887067395264117" /><itemandroid:color="#FF44FBFE"android:offset="0.2959927140255009" /><itemandroid:color="#FF76FEFE"android:offset="0.30327868852459017" /><itemandroid:color="#FF90FEFB"android:offset="0.3105646630236794" /><itemandroid:color="#FF8FFFF4"android:offset="0.31785063752276865" /><itemandroid:color="#FFA0FFF5"android:offset="0.3251366120218579" /><itemandroid:color="#FF8BFFF9"android:offset="0.3324225865209472" /><itemandroid:color="#FF75FFFC"android:offset="0.33970856102003644" /><itemandroid:color="#FF85FFF6"android:offset="0.3469945355191257" /><itemandroid:color="#FF9DFFF9"android:offset="0.3542805100182149" /><itemandroid:color="#FFAAFFF0"android:offset="0.36156648451730417" /><itemandroid:color="#FFAAFFE6"android:offset="0.3688524590163934" /><itemandroid:color="#FFAAFFDC"android:offset="0.3761384335154827" /><itemandroid:color="#FFA8FAD0"android:offset="0.38342440801457195" /><itemandroid:color="#FF96F2C0"android:offset="0.3907103825136612" /><itemandroid:color="#FF7EF6BA"android:offset="0.39799635701275043" /><itemandroid:color="#FF6EEFB1"android:offset="0.4052823315118397" /><itemandroid:color="#FF6EEEA1"android:offset="0.412568306010929" /><itemandroid:color="#FF32ED96"android:offset="0.4198542805100182" /><itemandroid:color="#FF2BED7E"android:offset="0.42714025500910746" /><itemandroid:color="#FF15E658"android:offset="0.4344262295081967" /><itemandroid:color="#FF18E032"android:offset="0.44171220400728595" /><itemandroid:color="#FF53F204"android:offset="0.4489981785063752" /><itemandroid:color="#FF6AFB29"android:offset="0.4562841530054645" /><itemandroid:color="#FF94FF43"android:offset="0.46357012750455373" /><itemandroid:color="#FFAAFB68"android:offset="0.470856102003643" /><itemandroid:color="#FFC0F987"android:offset="0.4781420765027322" /><itemandroid:color="#FFC8FD8D"android:offset="0.48542805100182146" /><itemandroid:color="#FFD2FA78"android:offset="0.49271402550091076" /><itemandroid:color="#FFDFF974"android:offset="0.5" /><itemandroid:color="#FFE6FF74"android:offset="0.5072859744990893" /><itemandroid:color="#FFF2FF74"android:offset="0.5145719489981785" /><itemandroid:color="#FFF8FF8D"android:offset="0.5218579234972678" /><itemandroid:color="#FFFFFD9B"android:offset="0.5291438979963571" /><itemandroid:color="#FFFDFF96"android:offset="0.5364298724954463" /><itemandroid:color="#FFFFFC87"android:offset="0.5437158469945356" /><itemandroid:color="#FFFFF596"android:offset="0.5510018214936249" /><itemandroid:color="#FFFFE28C"android:offset="0.558287795992714" /><itemandroid:color="#FFFEE687"android:offset="0.5655737704918034" /><itemandroid:color="#FFFEF086"android:offset="0.5728597449908925" /><itemandroid:color="#FFFCF588"android:offset="0.5801457194899818" /><itemandroid:color="#FFFEFC85"android:offset="0.5874316939890711" /><itemandroid:color="#FFFFFF82"android:offset="0.5947176684881603" /><itemandroid:color="#FFFCFA66"android:offset="0.6020036429872496" /><itemandroid:color="#FFFCFC64"android:offset="0.6092896174863388" /><itemandroid:color="#FFFCFE4F"android:offset="0.6165755919854281" /><itemandroid:color="#FFFFFC4F"android:offset="0.6238615664845174" /><itemandroid:color="#FFFCFC55"android:offset="0.6311475409836066" /><itemandroid:color="#FFFCFA6D"android:offset="0.6384335154826959" /><itemandroid:color="#FFFFF654"android:offset="0.6457194899817851" /><itemandroid:color="#FFFEF200"android:offset="0.6530054644808744" /><itemandroid:color="#FFFAE800"android:offset="0.6602914389799636" /><itemandroid:color="#FFF2D144"android:offset="0.6675774134790529" /><itemandroid:color="#FFF1C91E"android:offset="0.6748633879781422" /><itemandroid:color="#FFF1BD29"android:offset="0.6821493624772313" /><itemandroid:color="#FFF1B934"android:offset="0.6894353369763206" /><itemandroid:color="#FFF3B34B"android:offset="0.6967213114754098" /><itemandroid:color="#FFF9A845"android:offset="0.7040072859744991" /><itemandroid:color="#FFFBA04B"android:offset="0.7112932604735884" /><itemandroid:color="#FFFB964B"android:offset="0.7185792349726776" /><itemandroid:color="#FFFB8B4B"android:offset="0.7258652094717669" /><itemandroid:color="#FFFB864D"android:offset="0.7331511839708561" /><itemandroid:color="#FFFB7F4E"android:offset="0.7404371584699454" /><itemandroid:color="#FFFC7B4F"android:offset="0.7477231329690347" /><itemandroid:color="#FFFB774F"android:offset="0.7550091074681239" /><itemandroid:color="#FFFF724F"android:offset="0.7622950819672132" /><itemandroid:color="#FFFF6C4E"android:offset="0.7695810564663024" /><itemandroid:color="#FFFF4B4E"android:offset="0.7768670309653917" /><itemandroid:color="#FFFF364E"android:offset="0.784153005464481" /><itemandroid:color="#FFFF0048"android:offset="0.7914389799635702" /><itemandroid:color="#FFFF0C52"android:offset="0.7987249544626595" /><itemandroid:color="#FFFF2563"android:offset="0.8060109289617486" /><itemandroid:color="#FFFA3871"android:offset="0.8132969034608379" /><itemandroid:color="#FFF7387A"android:offset="0.8205828779599272" /><itemandroid:color="#FFFA4B7A"android:offset="0.8278688524590164" /><itemandroid:color="#FFFF587D"android:offset="0.8351548269581057" /><itemandroid:color="#FFFC6587"android:offset="0.8424408014571949" /><itemandroid:color="#FFFD718A"android:offset="0.8497267759562842" /><itemandroid:color="#FFFF8599"android:offset="0.8570127504553734" /><itemandroid:color="#FFFF9FAF"android:offset="0.8642987249544627" /><itemandroid:color="#FFFFC9D2"android:offset="0.871584699453552" /><itemandroid:color="#FFFFFFFF"android:offset="0.8788706739526412" /><itemandroid:color="#FFFBFFFF"android:offset="0.8861566484517305" /><itemandroid:color="#FFF3FEFF"android:offset="0.8934426229508197" /><itemandroid:color="#FFEBFEFF"android:offset="0.900728597449909" /><itemandroid:color="#FFEAFDFF"android:offset="0.9080145719489983" /><itemandroid:color="#FFD9FAFF"android:offset="0.9153005464480874" /><itemandroid:color="#FFC9F7FF"android:offset="0.9225865209471767" /><itemandroid:color="#FFB3FAFF"android:offset="0.9298724954462659" /><itemandroid:color="#FFAEF8FF"android:offset="0.9371584699453552" /><itemandroid:color="#FF97F5FF"android:offset="0.9444444444444445" /><itemandroid:color="#FF75F2FF"android:offset="0.9517304189435337" /><itemandroid:color="#FF65F0FF"android:offset="0.959016393442623" /></gradient></aapt:attr></path>
</vector>