天天看點

ShapeDrawable做放大鏡效果

引用一下shapedrawable的類的說明:

<a target="_blank" href="http://developer.android.com/reference/java/lang/object.html">java.lang.object</a>

   ?

<a target="_blank" href="http://developer.android.com/reference/android/graphics/drawable/drawable.html">android.graphics.drawable.drawable</a>

android.graphics.drawable.shapedrawable

class overview

    a drawable object that draws primitive shapes. a shapedrawable takes a shape object and manages its presence on the screen. 

if no shape is given, then the shapedrawable will default to a rectshape.

    it can be defined in an xml file with the &lt;shape&gt; element. 

file location:

    res/drawable/filename.xml

    the filename is used as the resource id.

compiled resource datatype:

    resource pointer to a shapedrawable.

resource reference:

    in java: r.drawable.filename

    in xml: @[package:]drawable/filename 

android中,利用shapedrawable來繪制圖像,shapedrawable可以設定畫筆的形狀。通過geipaint方可以得到paint對象。

利用shapedrawable在代碼中畫圖像的步驟如下:

1.       執行個體化shapedrawable對象,并說明繪制的形狀;

a)         形狀可以是矩形,橢圓,線和環(例如橢圓:)

myshapedrawable = new shapedrawable(new ovalshape());  

//得到畫筆paint對象并設定其顔色   

myshapedrawable.getpaint().setcolor(color.green);  

myshapedrawable.setbounds(70,250,150,280);  

//繪制圖像   

myshapedrawable.draw(canvas);  

//得到畫筆paint對象并設定其顔色  

//繪制圖像  

2.       得到畫筆對象并設定其顔色;

3.       利用setbounds方法來設定圖像顯示的區域;

4.       在畫布上繪制圖像。

當然,也可以使用xml檔案來定義畫圖,就是利用xml檔案來定義一個圖像。

具體文法如下:

&lt;?xml version="1.0" encoding="utf-8"?&gt;  

&lt;shape  

    xmlns:android="http://schemas.android.com/apk/res/android"  

    android:shape=["rectangle" | "oval" | "line" | "ring"] &gt;  

    &lt;corners  

        android:radius="integer"  

        android:topleftradius="integer"  

        android:toprightradius="integer"  

        android:bottomleftradius="integer"  

        android:bottomrightradius="integer" /&gt;  

    &lt;gradient  

        android:angle="integer"  

        android:centerx="integer"  

        android:centery="integer"  

        android:centercolor="integer"  

        android:endcolor="color"  

        android:gradientradius="integer"  

        android:startcolor="color"  

        android:type=["linear" | "radial" | "sweep"]  

        android:useslevel=["true" | "false"] /&gt;  

    &lt;padding  

        android:left="integer"  

        android:top="integer"  

        android:right="integer"  

        android:bottom="integer" /&gt;  

    &lt;size  

        android:width="integer"  

        android:height="integer" /&gt;  

    &lt;solid  

        android:color="color" /&gt;  

    &lt;stroke  

        android:color="color"  

        android:dashwidth="integer"  

        android:dashgap="integer" /&gt;  

&lt;/shape&gt;  

androidsdk中定義的規則:

elements:                   

    &lt;shape&gt;

        the shape drawable. this must be the root element.

        attributes:

        xmlns:android

            string. required. defines the xml namespace, which must be "http://schemas.android.com/apk/res/android". 

        android:shape

            keyword. defines the type of shape. valid values are:

value

desciption

"rectangle"

a rectangle that fills the containing view. this is the default shape.

"oval"

an oval shape that fits the dimensions of the containing view.

"line"

a horizontal line that spans the width of the containing view. this shape requires the &lt;stroke&gt; element to define the width of the line.

"ring"

a ring shape.

矩形"rectangle",橢圓"oval",線"line"和環"ring"

當是線"line"的時候一定要定義&lt;stroke&gt; element

        the following attributes are used only when android:shape="ring":

下面的屬性隻有在android:shape="ring"時才在有意義

        android:innerradius

            dimension. the radius for the inner part of the ring (the hole in the middle),

            as a dimension value or dimension resource.

        android:innerradiusratio

            float. the radius for the inner part of the ring, expressed as a ratio of the ring's width.

             for instance, if android:innerradiusratio="5", then the inner radius equals the ring's width divided by 5. 

             this value is overridden by android:innerradius. default value is 9.

        android:thickness

            dimension. the thickness of the ring, as a dimension value or dimension resource.

        android:thicknessratio

            float. the thickness of the ring, expressed as a ratio of the ring's width. 

            for instance, if android:thicknessratio="2", then the thickness equals the ring's width divided by 2. 

            this value is overridden by android:innerradius. default value is 3.

        android:uselevel

            boolean. "true" if this is used as a levellistdrawable. this should normally be "false" or your shape may not appear.

    &lt;corners&gt;

        creates rounded corners for the shape. applies only when the shape is a rectangle.

        該屬性隻有android:shape="rectangle";才有意義

        android:radius

            dimension. the radius for all corners, as a dimension value or dimension resource. 

            this is overridden for each corner by the following attributes.

預設情況下四個角的半徑,如果為0則表示直角

        android:topleftradius

            dimension. the radius for the top-left corner, as a dimension value or dimension resource.

左上角的半徑

        android:toprightradius

            dimension. the radius for the top-right corner, as a dimension value or dimension resource.

右上角的半徑

        android:bottomleftradius

            dimension. the radius for the bottom-left corner, as a dimension value or dimension resource.

左下角的半徑

        android:bottomrightradius

            dimension. the radius for the bottom-right corner, as a dimension value or dimension resource.

右下角的半徑

        note: every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. 

        if you want specific corners to not be rounded, a work-around is to use android:radius to 

        set a default corner radius greater than 1, but then override each and every corner with the values you really want, 

        providing zero ("0dp") where you don't want rounded corners.

    &lt;gradient&gt;

        specifies a gradient color for the shape.

該系列屬性對線"line"沒有意義。

        android:angle

            integer. the angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top.

            it must be a multiple of 45. default is 0.

                        顔色的變化方式,0表示從左到右,90表示從下到上,45表示在從左到右和從下到上同時進行

                        發現該屬性隻有android:type="linear"才有用。

        android:centerx

            float. the relative x-position for the center of the gradient (0 - 1.0). does not apply when android:type="linear".

                中心點在x方向上的值,其值為0-1.0

                其x=x0+(x1-x0)*centerx.

                當android:type="linear"時沒有意義

        android:centery

            float. the relative y-position for the center of the gradient (0 - 1.0). does not apply when android:type="linear".

                中間點在y方向上的值,其值為0-1.0

                其y=y0+(y1-y0)*centery.

        android:centercolor

            color. optional color that comes between the start and end colors, as a hexadecimal value or color resource.

       中間點的color

        android:endcolor

            color. the ending color, as a hexadecimal value or color resource.

        終點的gradient顔色

        android:gradientradius

            float. the radius for the gradient. only applied when android:type="radial".

        中心園的半徑,隻有當android:type="radial"時有意義

        android:startcolor

            color. the starting color, as a hexadecimal value or color resource.

        起始點gradient的顔色

        android:type

            keyword. the type of gradient pattern to apply. valid values are:

            value    description

            "linear"     a linear gradient. this is the default.線性

            "radial"     a radial gradient. the start color is the center color.發射型

            "sweep"     a sweeping line gradient.

         gradient有兩個或三個顔色值

         startcolor 中心區域的顔色

         centercolor 中心區域的外環區域(中間)的顔色。它是可選的。

         endcolor 其他區域顔色

         startcolor和endcolor的使用見執行個體2

         startcolor,centercolo和endcolor的使用見執行個體4

            boolean. "true" if this is used as a levellistdrawable.

            當為levellistdrawable時其值為true否則為false

    &lt;padding&gt;

        padding to apply to the containing view element (this pads the position of the view content, not the shape).

         該屬性用于設定shapedrawable的content區域的padding。

          shapedrawable的content區域用于确定他所在控件的content(通常是文本)的區域

        android:left

            dimension. left padding, as a dimension value or dimension resource.

        android:top

            dimension. top padding, as a dimension value or dimension resource.

        android:right

            dimension. right padding, as a dimension value or dimension resource.

        android:bottom

            dimension. bottom padding, as a dimension value or dimension resource.

    &lt;size&gt;

        the size of the shape.

        android:height

            dimension. the height of the shape, as a dimension value or dimension resource.

 該屬性對線"line"沒有意義。

        android:width

            dimension. the width of the shape, as a dimension value or dimension resource.

        note: the shape scales to the size of the container view proportionate to the dimensions defined here, by default.

        when you use the shape in an imageview, you can restrict scaling by setting the android:scaletype to "center".

    shapedrawable的大小

    注意:當是線的時候&lt;size&gt;隻有android:width屬性意義了。

&lt;solid&gt;

        a solid color to fill the shape.

        android:color

            color. the color to apply to the shape, as a hexadecimal value or color resource.

        &lt;solid&gt;用(a hexadecimal value or color resource)來填充shapedrawable,這時gradient屬性無意義

    &lt;stroke&gt;

        a stroke line for the shape.

            dimension. the thickness of the line, as a dimension value or dimension resource.

            color. the color of the line, as a hexadecimal value or color resource.

        android:dashgap

            dimension. the distance between line dashes, as a dimension value or dimension resource. only valid if android:dashwidth is set.

        android:dashwidth

            dimension. the size of each dash line, as a dimension value or dimension resource. only valid if android:dashgap is set.

android:width表示邊框線的寬度。邊框是向裡外同時擴充的。

android:color表示邊框線的顔色

android:dashgap表示dash線之間的長度

android:dashwidth表示dash線的長度

注意:當android:shape="line"時必須定義&lt;stroke&gt;。這時shapedrawable的邊框就隻有一個邊(line)

this layout xml applies the shape drawable to a view:

    &lt;textview

        android:background="@drawable/gradient_box"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content" /&gt;

    this application code gets the shape drawable and applies it to a view:

    resources res = getresources();

    drawable shape = res. getdrawable(r.drawable.gradient_box);

    textview tv = (textview)findviewbyid(r.id.textview);

    tv.setbackground(shape);

舉幾個例子:

&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"  

    android:shape="rectangle"&gt;  

    &lt;gradient   

        android:startcolor="#ff0000ff"   

         android:centercolor="#ff00ff00"  

        android:endcolor="#ffff0000"  

        android:type="radial"  

        android:gradientradius="100"  

        android:angle="90"/&gt;  

    &lt;padding android:left="0dp"   

        android:top="0dp"  

        android:right="0dp"   

        android:bottom="0dp" /&gt;  

    &lt;corners android:radius="0dp" android:topleftradius="0dp"/&gt;  

執行個體2  

drawable下的gradient_box.xml  

        android:type="sweep"  

    &lt;corners android:radius="80dp" android:topleftradius="10dp"/&gt;  

執行個體3  

執行個體4  

stroke的使用  

        android:gradientradius="100"/&gt;  

    &lt;corners android:radius="10dp" android:topleftradius="0dp"/&gt;  

        &lt;size  

        android:width="150dp"  

        android:height="150dp" /&gt;  

        &lt;stroke  

            android:width="20dp"  

            android:color="#ff0000ff"  

            android:dashwidth="1dp"  

            android:dashgap="1dp" /&gt;  

執行個體5  

橢圓  

    android:shape="oval"&gt;  

        android:centercolor="#ff00ff00"  

執行個體6  

線"line"的使用  

    android:shape="line"&gt;  

        &lt;size android:width="300dp"/&gt;  

            android:width="10dp"  

繼續閱讀