Android Drawable Resource学习(十二)、ShapeDrawable还是GradientDrawable?

一、发现奇怪的问题?

在研究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中定义:

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.     <gradient  
  5.         android:startColor="#FFFF0000"  
  6.         android:endColor="#80FF00FF"  
  7.         android:angle="45"/>  
  8.     <padding android:left="7dp"  
  9.         android:top="7dp"  
  10.         android:right="7dp"  
  11.         android:bottom="7dp" />  
  12.     <corners android:radius="8dp" />  
  13. </shape>  

在layout文件中使用:

[java]  view plain copy
  1. <TextView  
  2.         android:id="@+id/textView"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:background="@drawable/gradient_box"  
  6.         android:text="测试" />  

在java代码中控制:

[java]  view plain copy
  1. TextView textView=(TextView)findViewById(R.id.textView);  
  2.    ShapeDrawable gradientDrawable=(ShapeDrawable)textView.getBackground();  
结果发现,报错了:
[java]  view plain copy
  1. :java.lang.ClassCastException: android.graphics.drawable.GradientDrawable  
不能将GradientDrawable转换为ShapeDrawable。

可见,使用<shape>标签定义的是GradientDrawable。


并且,在官方API Drawable Resource中ShapeDrawable介绍到:

[java]  view plain copy
  1. compiled resource datatype:  
  2.     Resource pointer to a GradientDrawable.  
编译资源类型

指向GradientDrawable的指针。

可见,编译的类型是GradientDrawable。


那么,ShapeDrawable是怎么定义的,找了网上的资料,结果硬是没找到如何在XML文件中定义它,只能通过代码的方式实现。如何使用它在后面再介绍。


二、使用GradientDrawable

GradientDrawable的作用在于定于各种样式的渐变。在XML文件中使用<shape>元素定义。
文件位置:
res/drawable/filename.xml
文件名即资源ID
编译资源类型:
指向  GradientDrawable.
资源引用
In Java:  R.drawable.filename
In XML:  @[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>
元素:
<shape>
定义这是一个GradientDrawable,必须作为根元素。

属性:

xmlns:android
String类型。必须的,定义xml文件的命名空间,必须是"http://schemas.a ndroid.com/apk/res/android".
android:shape
关键字。定义shape的值,必须是下面的之一:
描述
"rectangle" 矩阵,这也是默认的shape
"oval" 椭圆
"line" 一条水平的直线。这种shape必须使用 <stroke> 元素来定义这条线的宽度
"ring" 圆环

下面的属性只有当 android:shape="ring"才使用:

android:innerRadius
尺寸。 内环的半径。一个尺寸值(dip等等)或者一个尺寸资源。
android:innerRadiusRatio
Float类型。这个值表示内部环的比例,例如,如果android:innerRadiusRatio = " 5 ",那么内部的半径等于环的宽度除以5。这个值会被android:innerRadius重写。 默认值是9。
android:thickness
尺寸。环的厚度,是一个尺寸值或尺寸的资源。
android:thicknessRatio
Float类型。厚度的比例。例如,如果android:thicknessRatio= " 2 ",然后厚度等于环的宽度除以2。这个值是被android:innerRadius重写, 默认值是3。
android:useLevel
Boolean类型。如果用在  LevelListDrawable里,那么就是true。如果通常不出现则为false。
<corners>
为Shape创建一个圆角,只有shape是rectangle时候才使用。

属性:

android:radius
Dimension。圆角的半径。会被下面每个特定的圆角属性重写。
android:topLeftRadius
Dimension。top-left 圆角的半径。
android:topRightRadius
Dimension。top-right 圆角的半径。
android:bottomLeftRadius
Dimension。 bottom-left圆角的半径。
android:bottomRightRadius
Dimension。bottom-right圆角的半径。

注意:每个圆角半径值都必须大于1,否侧就没有圆角。

             下面的话不明白,我直接设置圆角为0就可以不圆了,其余的设置有圆角,一样的可行。不知道它为什么要这么讲。

(If you want specific cornersto not be rounded, a work-around is to use android:radius to set a default cornerradius greater than 1, but then override each and every corner with the values you reallywant, providing zero ("0dp") where you don't want rounded corners.)

<gradient>
指定这个shape的渐变颜色。

属性:

android:angle
Integer。渐变的角度。 0 代表从 left 到 right。90 代表bottom到 top。必须是45的倍数,默认为0
android:centerX
Float。渐变中心的相对X坐标,在0到1.0之间。
android:centerY
Float。渐变中心的相对Y坐标,在0到1.0之间。
android:centerColor
Color。可选的颜色值。基于startColor和endColor之间。
android:endColor
Color。 结束的颜色。
android:gradientRadius
Float 。渐变的半径。只有在  android:type="radial"才使用
android:startColor
Color。开始的颜色值。
android:type
Keyword。渐变的模式,下面值之一:
描述
"linear" 线形渐变。这也是默认的模式
"radial" 辐射渐变。startColor即辐射中心的颜色
"sweep" 扫描线渐变。
android:useLevel
Boolean。如果在LevelListDrawable中使用,则为true

<padding>

     内容与视图边界的距离

属性:

android:left
Dimension。左边填充距离.
android:top
Dimension。顶部填充距离.
android:right
Dimension。右边填充距离.
android:bottom
Dimension。底部填充距离.
<size>
这个shape的大小。

属性:

android:height
Dimension。这个shape的高度。
android:width
Dimension。这个shape的宽度。

注意:默认情况下,这个shape会缩放到与他所在容器大小成正比。当你在一个ImageView中使用这个shape,你可以使用android:scaleType="center"来限制这种缩放。

<solid>
填充这个shape的纯色

属性:

android:color
Color。颜色值,十六进制数,或者一个Color资源
<stroke>
这个shape使用的笔画,当android:shape="line"的时候,必须设置改元素。

属性:

android:width
Dimension。笔画的粗细。
android:color
Color。笔画的颜色
android:dashGap
Dimension。每画一条线就间隔多少。只有当 android:dashWidth也设置了才有效。
android:dashWidth
Dimension。每画一条线的长度。只有当  android:dashGap也设置了才有效。
示例:
XML file saved at  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:

[java]  view plain copy
  1. package com.example.shapedrawable;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Path;  
  9. import android.graphics.Picture;  
  10. import android.graphics.Rect;  
  11. import android.graphics.drawable.Drawable;  
  12. import android.graphics.drawable.GradientDrawable;  
  13. import android.graphics.drawable.ShapeDrawable;  
  14. import android.os.Bundle;  
  15. import android.util.AttributeSet;  
  16. import android.view.View;  
  17. import android.view.ViewGroup;  
  18. import android.view.ViewParent;  
  19. import android.widget.TextView;  
  20.   
  21. public class MainActivity extends GraphicsActivity {  
  22.   
  23.   @Override  
  24.   protected void onCreate(Bundle savedInstanceState) {  
  25.     super.onCreate(savedInstanceState);  
  26.    setContentView(new SampleView(this));  
  27.   }  
  28.   
  29.   private static class SampleView extends View {  
  30.     private Path mPath;  
  31.     private Paint mPaint;  
  32.     private Rect mRect;  
  33.     private GradientDrawable mDrawable;  
  34.   
  35.     public SampleView(Context context) {  
  36.       super(context);  
  37.       setFocusable(true);  
  38.   
  39.       mPath = new Path();  
  40.       mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  41.       mRect = new Rect(00120120);  
  42.   
  43.       mDrawable = new GradientDrawable(  
  44.           GradientDrawable.Orientation.TL_BR, new int[] { 0xFFFF0000,  
  45.               0xFF00FF000xFF0000FF });  
  46.       mDrawable.setShape(GradientDrawable.RECTANGLE);  
  47.       mDrawable.setGradientRadius((float) (Math.sqrt(2) * 60));  
  48.     }  
  49.   
  50.     static void setCornerRadii(GradientDrawable drawable, float r0,  
  51.         float r1, float r2, float r3) {  
  52.       drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3,  
  53.           r3 });  
  54.     }  
  55.   
  56.   
  57.     //重点的绘制过程  
  58.     @Override  
  59.     protected void onDraw(Canvas canvas) {  
  60.   
  61.       mDrawable.setBounds(mRect);  
  62.   
  63.       float r = 16;  
  64.   
  65.       canvas.save();  
  66.       canvas.translate(1010);  
  67.       mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);  
  68.       setCornerRadii(mDrawable, r, r, 00);  
  69.       mDrawable.draw(canvas);  
  70.       canvas.restore();  
  71.   
  72.       canvas.save();  
  73.       canvas.translate(10 + mRect.width() + 1010);  
  74.       mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);  
  75.       setCornerRadii(mDrawable, 00, r, r);  
  76.       mDrawable.draw(canvas);  
  77.       canvas.restore();  
  78.   
  79.       canvas.translate(0, mRect.height() + 10);  
  80.   
  81.       canvas.save();  
  82.       canvas.translate(1010);  
  83.       mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);  
  84.       setCornerRadii(mDrawable, 0, r, r, 0);  
  85.       mDrawable.draw(canvas);  
  86.       canvas.restore();  
  87.   
  88.       canvas.save();  
  89.       canvas.translate(10 + mRect.width() + 1010);  
  90.       mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);  
  91.       setCornerRadii(mDrawable, r, 00, r);  
  92.       mDrawable.draw(canvas);  
  93.       canvas.restore();  
  94.   
  95.       canvas.translate(0, mRect.height() + 10);  
  96.   
  97.       canvas.save();  
  98.       canvas.translate(1010);  
  99.       mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);  
  100.       setCornerRadii(mDrawable, r, 0, r, 0);  
  101.       mDrawable.draw(canvas);  
  102.       canvas.restore();  
  103.   
  104.       canvas.save();  
  105.       canvas.translate(10 + mRect.width() + 1010);  
  106.       mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);  
  107.       setCornerRadii(mDrawable, 0, r, 0, r);  
  108.       mDrawable.draw(canvas);  
  109.       canvas.restore();  
  110.     }  
  111.   }  
  112. }  
  113.   
  114. class GraphicsActivity extends Activity {  
  115.   // set to true to test Picture  
  116.   private static final boolean TEST_PICTURE = false;  
  117.   
  118.   @Override  
  119.   protected void onCreate(Bundle savedInstanceState) {  
  120.     super.onCreate(savedInstanceState);  
  121.   }  
  122.   
  123.   @Override  
  124.   public void setContentView(View view) {  
  125.     if (TEST_PICTURE) {  
  126.       ViewGroup vg = new PictureLayout(this);  
  127.       vg.addView(view);  
  128.       view = vg;  
  129.     }  
  130.   
  131.     super.setContentView(view);  
  132.   }  
  133. }  
  134.   
  135. class PictureLayout extends ViewGroup {  
  136.   private final Picture mPicture = new Picture();  
  137.   
  138.   public PictureLayout(Context context) {  
  139.     super(context);  
  140.   }  
  141.   
  142.   public PictureLayout(Context context, AttributeSet attrs) {  
  143.     super(context, attrs);  
  144.   }  
  145.   
  146.   @Override  
  147.   public void addView(View child) {  
  148.     if (getChildCount() > 1) {  
  149.       throw new IllegalStateException(  
  150.           "PictureLayout can host only one direct child");  
  151.     }  
  152.   
  153.     super.addView(child);  
  154.   }  
  155.   
  156.   @Override  
  157.   public void addView(View child, int index) {  
  158.     if (getChildCount() > 1) {  
  159.       throw new IllegalStateException(  
  160.           "PictureLayout can host only one direct child");  
  161.     }  
  162.   
  163.     super.addView(child, index);  
  164.   }  
  165.   
  166.   @Override  
  167.   public void addView(View child, LayoutParams params) {  
  168.     if (getChildCount() > 1) {  
  169.       throw new IllegalStateException(  
  170.           "PictureLayout can host only one direct child");  
  171.     }  
  172.   
  173.     super.addView(child, params);  
  174.   }  
  175.   
  176.   @Override  
  177.   public void addView(View child, int index, LayoutParams params) {  
  178.     if (getChildCount() > 1) {  
  179.       throw new IllegalStateException(  
  180.           "PictureLayout can host only one direct child");  
  181.     }  
  182.   
  183.     super.addView(child, index, params);  
  184.   }  
  185.   
  186.   @Override  
  187.   protected LayoutParams generateDefaultLayoutParams() {  
  188.     return new LayoutParams(LayoutParams.MATCH_PARENT,  
  189.         LayoutParams.MATCH_PARENT);  
  190.   }  
  191.   
  192.   @Override  
  193.   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  194.     final int count = getChildCount();  
  195.   
  196.     int maxHeight = 0;  
  197.     int maxWidth = 0;  
  198.   
  199.     for (int i = 0; i < count; i++) {  
  200.       final View child = getChildAt(i);  
  201.       if (child.getVisibility() != GONE) {  
  202.         measureChild(child, widthMeasureSpec, heightMeasureSpec);  
  203.       }  
  204.     }  
  205.   
  206.     maxWidth += getPaddingLeft() + getPaddingRight();  
  207.     maxHeight += getPaddingTop() + getPaddingBottom();  
  208.   
  209.     Drawable drawable = getBackground();  
  210.     if (drawable != null) {  
  211.       maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());  
  212.       maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());  
  213.     }  
  214.   
  215.     setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),  
  216.         resolveSize(maxHeight, heightMeasureSpec));  
  217.   }  
  218.   
  219.   private void drawPict(Canvas canvas, int x, int y, int w, int h, float sx,  
  220.       float sy) {  
  221.     canvas.save();  
  222.     canvas.translate(x, y);  
  223.     canvas.clipRect(00, w, h);  
  224.     canvas.scale(0.5f, 0.5f);  
  225.     canvas.scale(sx, sy, w, h);  
  226.     canvas.drawPicture(mPicture);  
  227.     canvas.restore();  
  228.   }  
  229.   
  230.   @Override  
  231.   protected void dispatchDraw(Canvas canvas) {  
  232.     super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));  
  233.     mPicture.endRecording();  
  234.   
  235.     int x = getWidth() / 2;  
  236.     int y = getHeight() / 2;  
  237.   
  238.     if (false) {  
  239.       canvas.drawPicture(mPicture);  
  240.     } else {  
  241.       drawPict(canvas, 00, x, y, 11);  
  242.       drawPict(canvas, x, 0, x, y, -11);  
  243.       drawPict(canvas, 0, y, x, y, 1, -1);  
  244.       drawPict(canvas, x, y, x, y, -1, -1);  
  245.     }  
  246.   }  
  247.   
  248.   @Override  
  249.   public ViewParent invalidateChildInParent(int[] location, Rect dirty) {  
  250.     location[0] = getLeft();  
  251.     location[1] = getTop();  
  252.     dirty.set(00, getWidth(), getHeight());  
  253.     return getParent();  
  254.   }  
  255.   
  256.   @Override  
  257.   protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  258.     final int count = super.getChildCount();  
  259.   
  260.     for (int i = 0; i < count; i++) {  
  261.       final View child = getChildAt(i);  
  262.       if (child.getVisibility() != GONE) {  
  263.         final int childLeft = getPaddingLeft();  
  264.         final int childTop = getPaddingTop();  
  265.         child.layout(childLeft, childTop,  
  266.             childLeft + child.getMeasuredWidth(),  
  267.             childTop + child.getMeasuredHeight());  
  268.   
  269.       }  
  270.     }  
  271.   }  
  272. }  

三、ShapeDrawable的使用

ShapeDrawable没有发现如何在XML文件中定义使用,所以应该不属于Drawable Resource。这个留到后面学习Graphics在看。

你可能感兴趣的:(Android Drawable Resource学习(十二)、ShapeDrawable还是GradientDrawable?)