在研究Android Drawable资源的时候,发现了一个奇怪的问题。在官方API介绍中:
ShapeDrawable 介绍:This object can be defined in an XML file with the <shape>
element(这个对象可以用<shape>元素在xml文件中定义)
GradientDrawable 介绍:This object can be defined in an XML file with the <shape>
element(这个对象可以用<shape>元素在xml文件中定义)
两者的介绍一模一样,都说可以使用<shape>标签在xml文件中定义。
那么,到底用<shape>标签定义的是什么的呢?经过下面的实验:
在shape.xml中定义:
可见,使用<shape>标签定义的是GradientDrawable。
并且,在官方API Drawable Resource中ShapeDrawable介绍到:
指向GradientDrawable的指针。
可见,编译的类型是GradientDrawable。
那么,ShapeDrawable是怎么定义的,找了网上的资料,结果硬是没找到如何在XML文件中定义它,只能通过代码的方式实现。如何使用它在后面再介绍。
res/drawable/filename.xml
GradientDrawable
.
R.drawable.filename
@[package:]drawable/filename
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">shape</a><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:shape</span><span class="pun">=</span><span class="atv">["rectangle"</span><span class="pln"> | </span><span class="atv">"oval"</span><span class="pln"> | </span><span class="atv">"line"</span><span class="pln"> | </span><span class="atv">"ring"</span><span class="pln">] </span><span class="tag">></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">corners</a><span class="pln"> </span><span class="atn">android:radius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:topLeftRadius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:topRightRadius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:bottomLeftRadius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:bottomRightRadius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">gradient</a><span class="pln"> </span><span class="atn">android:angle</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:centerX</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:centerY</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:centerColor</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:endColor</span><span class="pun">=</span><span class="atv">"</span><em>color</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:gradientRadius</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:startColor</span><span class="pun">=</span><span class="atv">"</span><em>color</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:type</span><span class="pun">=</span><span class="atv">["linear"</span><span class="pln"> | </span><span class="atv">"radial"</span><span class="pln"> | </span><span class="atv">"sweep"</span><span class="pln">] </span><span class="atn">android:useLevel</span><span class="pun">=</span><span class="atv">["true"</span><span class="pln"> | </span><span class="atv">"false"</span><span class="pln">] </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">padding</a><span class="pln"> </span><span class="atn">android:left</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:top</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:right</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:bottom</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">size</a><span class="pln"> </span><span class="atn">android:width</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:height</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">solid</a><span class="pln"> </span><span class="atn">android:color</span><span class="pun">=</span><span class="atv">"</span><em>color</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">stroke</a><span class="pln"> </span><span class="atn">android:width</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:color</span><span class="pun">=</span><span class="atv">"</span><em>color</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:dashWidth</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:dashGap</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"></shape></span>
<padding>
内容与视图边界的距离
res/drawable/gradient_box.xml
:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><shape</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:shape</span><span class="pun">=</span><span class="atv">"rectangle"</span><span class="tag">></span><span class="pln"> </span><span class="tag"><gradient</span><span class="pln"> </span><span class="atn">android:startColor</span><span class="pun">=</span><span class="atv">"#FFFF0000"</span><span class="pln"> </span><span class="atn">android:endColor</span><span class="pun">=</span><span class="atv">"#80FF00FF"</span><span class="pln"> </span><span class="atn">android:angle</span><span class="pun">=</span><span class="atv">"45"</span><span class="tag">/></span><span class="pln"> </span><span class="tag"><padding</span><span class="pln"> </span><span class="atn">android:left</span><span class="pun">=</span><span class="atv">"7dp"</span><span class="pln"> </span><span class="atn">android:top</span><span class="pun">=</span><span class="atv">"7dp"</span><span class="pln"> </span><span class="atn">android:right</span><span class="pun">=</span><span class="atv">"7dp"</span><span class="pln"> </span><span class="atn">android:bottom</span><span class="pun">=</span><span class="atv">"7dp"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><corners</span><span class="pln"> </span><span class="atn">android:radius</span><span class="pun">=</span><span class="atv">"8dp"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"></shape></span>
This layout XML applies the shape drawable to a View:
<span class="tag"><TextView</span><span class="pln"> </span><span class="atn">android:background</span><span class="pun">=</span><span class="atv">"@drawable/gradient_box"</span><span class="pln"> </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="tag">/></span>
This application code gets the shape drawable and applies it to a View:
<span class="typ">Resources</span><span class="pln"> res </span><span class="pun">=</span><span class="pln"> </span><code><a target=_blank style="color: rgb(202, 0, 0);"><span class="pln">getResources</span><span class="pun">()</span></a></code><span class="pun">;</span><span class="pln"> </span><span class="typ">Drawable</span><span class="pln"> shape </span><span class="pun">=</span><span class="pln"> res</span><span class="pun">.</span><span class="pln"> </span><code><a target=_blank style="color: rgb(202, 0, 0);">getDrawable</a></code><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">drawable</span><span class="pun">.</span><span class="pln">gradient_box</span><span class="pun">);</span><span class="pln"> </span><span class="typ">TextView</span><span class="pln"> tv </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">TextView</span><span class="pun">)</span><span class="pln">findViewByID</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">textview</span><span class="pun">);</span><span class="pln"> tv</span><span class="pun">.</span><span class="pln">setBackground</span><span class="pun">(</span><span class="pln">shape</span><span class="pun">);</span>
ShapeDrawable
下面是API的Demo:
ShapeDrawable没有发现如何在XML文件中定义使用,所以应该不属于Drawable Resource。这个留到后面学习Graphics在看。