Android开发之ShapeDrawable
/*
* Android开发之ShapeDrawble
* 北京Android俱乐部群:167839253
* Created on: 2011-12-6
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
引用一下ShapeDrawable的类的说明:
ClassOverview
ADrawableobjectthatdrawsprimitiveshapes.AShapeDrawabletakesaShapeobjectandmanagesitspresenceonthescreen.
IfnoShapeisgiven,thentheShapeDrawablewilldefaulttoaRectShape.
ItcanbedefinedinanXMLfilewiththe<shape>element.
filelocation:
res/drawable/filename.xml
ThefilenameisusedastheresourceID.
compiledresourcedatatype:
ResourcepointertoaShapeDrawable.
resourcereference:
InJava:R.drawable.filename
InXML:@[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);
2. 得到画笔对象并设置其颜色;
3. 利用setBounds方法来设置图像显示的区域;
4. 在画布上绘制图像。
当然,也可以使用XML文件来定义画图,就是利用XML文件来定义一个图像。
具体语法如下:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<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"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
AndroidSDK中定义的规则:
elements:
<shape>
Theshapedrawable.Thismustbetherootelement.
attributes:
xmlns:android
String.Required.DefinestheXMLnamespace,whichmustbe"http://schemas.android.com/apk/res/android".
android:shape
Keyword.Definesthetypeofshape.Validvaluesare:
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<stroke>element to define the width of the line.
|
"ring"
|
A ring shape.
|
矩形"rectangle",椭圆"oval",线"line"和环"ring"
当是线"line"的时候一定要定义<stroke>element
Thefollowingattributesareusedonlywhenandroid:shape="ring":
下面的属性只有在android:shape="ring"时才在有意义
android:innerRadius
Dimension.Theradiusfortheinnerpartofthering(theholeinthemiddle),
asadimensionvalueordimensionresource.
android:innerRadiusRatio
Float.Theradiusfortheinnerpartofthering,expressedasaratioofthering'swidth.
Forinstance,ifandroid:innerRadiusRatio="5",thentheinnerradiusequalsthering'swidthdividedby5.
Thisvalueisoverriddenbyandroid:innerRadius.Defaultvalueis9.
android:thickness
Dimension.Thethicknessofthering,asadimensionvalueordimensionresource.
android:thicknessRatio
Float.Thethicknessofthering,expressedasaratioofthering'swidth.
Forinstance,ifandroid:thicknessRatio="2",thenthethicknessequalsthering'swidthdividedby2.
Thisvalueisoverriddenbyandroid:innerRadius.Defaultvalueis3.
android:useLevel
Boolean."true"ifthisisusedasaLevelListDrawable.Thisshouldnormallybe"false"oryourshapemaynotappear.
<corners>
Createsroundedcornersfortheshape.Appliesonlywhentheshapeisarectangle.
该属性只有android:shape="rectangle";才有意义
attributes:
android:radius
Dimension.Theradiusforallcorners,asadimensionvalueordimensionresource.
Thisisoverriddenforeachcornerbythefollowingattributes.
默认情况下四个角的半径,如果为0则表示直角
android:topLeftRadius
Dimension.Theradiusforthetop-leftcorner,asadimensionvalueordimensionresource.
左上角的半径
android:topRightRadius
Dimension.Theradiusforthetop-rightcorner,asadimensionvalueordimensionresource.
右上角的半径
android:bottomLeftRadius
Dimension.Theradiusforthebottom-leftcorner,asadimensionvalueordimensionresource.
左下角的半径
android:bottomRightRadius
Dimension.Theradiusforthebottom-rightcorner,asadimensionvalueordimensionresource.
右下角的半径
Note:Everycornermust(initially)beprovidedacornerradiusgreaterthan1,orelsenocornersarerounded.
Ifyouwantspecificcornerstonotberounded,awork-aroundistouseandroid:radiusto
setadefaultcornerradiusgreaterthan1,butthenoverrideeachandeverycornerwiththevaluesyoureallywant,
providingzero("0dp")whereyoudon'twantroundedcorners.
<gradient>
Specifiesagradientcolorfortheshape.
该系列属性对线"line"没有意义。
attributes:
android:angle
Integer.Theangleforthegradient,indegrees.0islefttoright,90isbottomtotop.
Itmustbeamultipleof45.Defaultis0.
颜色的变化方式,0表示从左到右,90表示从下到上,45表示在从左到右和从下到上同时进行
发现该属性只有android:type="linear"才有用。
android:centerX
Float.TherelativeX-positionforthecenterofthegradient(0-1.0).Doesnotapplywhenandroid:type="linear".
中心点在x方向上的值,其值为0-1.0
其x=x0+(x1-x0)*centerX.
当android:type="linear"时没有意义
android:centerY
Float.TherelativeY-positionforthecenterofthegradient(0-1.0).Doesnotapplywhenandroid:type="linear".
中间点在y方向上的值,其值为0-1.0
其y=y0+(y1-y0)*centerY.
当android:type="linear"时没有意义
android:centerColor
Color.Optionalcolorthatcomesbetweenthestartandendcolors,asahexadecimalvalueorcolorresource.
中间点的Color
android:endColor
Color.Theendingcolor,asahexadecimalvalueorcolorresource.
终点的gradient颜色
android:gradientRadius
Float.Theradiusforthegradient.Onlyappliedwhenandroid:type="radial".
中心园的半径,只有当android:type="radial"时有意义
android:startColor
Color.Thestartingcolor,asahexadecimalvalueorcolorresource.
起始点gradient的颜色
android:type
Keyword.Thetypeofgradientpatterntoapply.Validvaluesare:
ValueDescription
"linear"Alineargradient.Thisisthedefault.线性
"radial"Aradialgradient.Thestartcoloristhecentercolor.发射型
"sweep"Asweepinglinegradient.
gradient有两个或三个颜色值
startColor中心区域的颜色
centerColor 中心区域的外环区域(中间)的颜色。它是可选的。
endColor 其他区域颜色
startColor和endColor的使用见实例2
startColor,centerColo和endColor的使用见实例4
android:useLevel
Boolean."true"ifthisisusedasaLevelListDrawable.
当为LevelListDrawable时其值为true否则为false
<padding>
PaddingtoapplytothecontainingViewelement(thispadsthepositionoftheViewcontent,nottheshape).
该属性用于设置ShapeDrawable的content区域的Padding。
ShapeDrawable的content区域用于确定他所在控件的content(通常是文本)的区域
attributes:
android:left
Dimension.Leftpadding,asadimensionvalueordimensionresource.
android:top
Dimension.Toppadding,asadimensionvalueordimensionresource.
android:right
Dimension.Rightpadding,asadimensionvalueordimensionresource.
android:bottom
Dimension.Bottompadding,asadimensionvalueordimensionresource.
<size>
Thesizeoftheshape.
attributes:
android:height
Dimension.Theheightoftheshape,asadimensionvalueordimensionresource.
该属性对线"line"没有意义。
android:width
Dimension.Thewidthoftheshape,asadimensionvalueordimensionresource.
Note:TheshapescalestothesizeofthecontainerViewproportionatetothedimensionsdefinedhere,bydefault.
WhenyouusetheshapeinanImageView,youcanrestrictscalingbysettingtheandroid:scaleTypeto"center".
ShapeDrawable的大小
注意:当是线的时候<size>只有android:width属性意义了。
<solid>
Asolidcolortofilltheshape.
attributes:
android:color
Color.Thecolortoapplytotheshape,asahexadecimalvalueorcolorresource.
<solid>用(ahexadecimalvalueorcolorresource)来填充ShapeDrawable,这时gradient属性无意义
<stroke>
Astrokelinefortheshape.
attributes:
android:width
Dimension.Thethicknessoftheline,asadimensionvalueordimensionresource.
android:color
Color.Thecoloroftheline,asahexadecimalvalueorcolorresource.
android:dashGap
Dimension.Thedistancebetweenlinedashes,asadimensionvalueordimensionresource.Onlyvalidifandroid:dashWidthisset.
android:dashWidth
Dimension.Thesizeofeachdashline,asadimensionvalueordimensionresource.Onlyvalidifandroid:dashGapisset.
android:width表示边框线的宽度。边框是向里外同时扩展的。
android:color表示边框线的颜色
android:dashGap表示dash线之间的长度
android:dashWidth表示dash线的长度
注意:当android:shape="line"时必须定义<stroke>。这时ShapeDrawable的边框就只有一个边(line)
ThislayoutXMLappliestheshapedrawabletoaView:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
ThisapplicationcodegetstheshapedrawableandappliesittoaView:
Resourcesres=getResources();
Drawableshape=res.getDrawable(R.drawable.gradient_box);
TextViewtv=(TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
举几个例子:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000FF"
android:centerColor="#FF00FF00"
android:endColor="#FFFF0000"
android:type="radial"
android:gradientRadius="100"
android:angle="90"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="0dp" android:topLeftRadius="0dp"/>
</shape>
实例2
drawable下的gradient_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000FF"
android:endColor="#FFFF0000"
android:type="sweep"
android:angle="90"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="80dp" android:topLeftRadius="10dp"/>
</shape>
实例3
drawable下的gradient_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000FF"
android:centerColor="#FF00FF00"
android:endColor="#FFFF0000"
android:type="sweep"
android:angle="90"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="80dp" android:topLeftRadius="10dp"/>
</shape>
实例4
stroke的使用
drawable下的gradient_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000FF"
android:centerColor="#FF00FF00"
android:endColor="#FFFF0000"
android:type="radial"
android:gradientRadius="100"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="10dp" android:topLeftRadius="0dp"/>
<size
android:width="150dp"
android:height="150dp" />
<stroke
android:width="20dp"
android:color="#FF0000FF"
android:dashWidth="1dp"
android:dashGap="1dp" />
</shape>
实例5
椭圆
drawable下的gradient_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:startColor="#FF0000FF"
android:centerColor="#FF00FF00"
android:endColor="#FFFF0000"
android:type="radial"
android:gradientRadius="100"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
实例6
线"line"的使用
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<size android:width="300dp"/>
<stroke
android:width="10dp"
android:color="#FF0000FF"
android:dashWidth="1dp"
android:dashGap="1dp" />
</shape>
分享到:
相关推荐
在Android开发中,Shape Drawable是一种常用的图形绘制工具,它允许开发者通过XML定义各种形状,如矩形、圆形、线和梯形等,并可以设置填充颜色、边框宽度和颜色等属性。然而,当我们需要更动态地创建和改变图形时,...
在Android开发中,Shape Drawable是一种非常实用的图形绘制工具,它允许开发者通过XML来定义不同形状的图形,如矩形、圆形、线或梯形等,并可以对其进行填充颜色、描边、渐变等多种样式设置。Shape Drawable是...
本书是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。本书从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层...
在Android开发中,Shape Drawable是Android图形库中的一个重要组成部分,它允许开发者创建各种形状并进行自定义绘制。这篇博客文章“android ShapeDrawable实例”可能是关于如何在实际项目中使用ShapeDrawable来实现...
在Android开发中,有时我们需要为用户界面添加一些特殊效果,比如局部放大的功能,以便用户可以更清晰地查看特定区域的细节。在这种情况下,`ShapeDrawable`是一个非常实用的工具,尤其是在结合`BitmapShader`使用时...
在Android开发中,XML Shape Drawable是一种非常实用的资源类型,它允许开发者通过XML代码来创建图形,如矩形、椭圆、线以及更复杂的形状。这些形状可以用于自定义按钮、背景、图标的外观,提供了丰富的定制选项,...
在Android开发中,自定义视图是提升用户体验和界面独特性的重要手段。本文将深入探讨如何使用`Canvas`来在`ImageView`的左上角绘制一个三角形,并结合文字进行展示,以实现标题中提到的功能。 ### 1. `ImageView`与...
### Android开发入门与实战——期刊2:图像处理篇(1) #### 一、应用风格和主题 **1.1 如何新建自定义的风格和主题** 在Android开发中,风格和主题是两个重要的概念,它们可以帮助开发者更好地控制应用程序的...
4. Drawables:Android提供的Drawable类族,包括BitmapDrawable、ShapeDrawable等,提供了更高级别的图形封装,简化了UI组件的装饰。 三、Android图形渲染管线 Android图形渲染管线由Java层的View系统和Native层的...
【Android开发艺术探索1】是关于Android应用开发的深度探讨,涵盖了多个关键知识点。以下是根据标题、描述和部分内容整理的详细知识概览: 1. **Activity的生命周期与启动模式** - **生命周期全面分析**:Activity...
6.2.2 ShapeDrawable / 247 6.2.3 LayerDrawable / 251 6.2.4 StateListDrawable / 253 6.2.5 LevelListDrawable / 255 6.2.6 TransitionDrawable / 256 6.2.7 InsetDrawable / 257 6.2.8 ...
在Android开发中,Shape是XML布局文件中定义的一种图形元素,通常用于创建自定义的背景、按钮或者其他UI组件。它可以包含各种形状(如矩形、圆形、线等),并可以通过填充色、边框色以及边框宽度来定制样式。本教程...
在Android开发中,二维图形的处理是界面设计的重要组成部分,涉及到各种图形资源和动画效果的实现。从给定文件的标题和描述来看,文档主要讲述了如何在Android平台上使用二维图形相关的API来绘制和动画化图形界面...
在Android开发中,动画效果是提升用户体验和应用视觉吸引力的重要手段。"Android-Android多种形状波纹特效可以用来实现一些背景特效"这个主题主要聚焦于Android中的波纹动画,这是一种常用于触摸反馈或者过渡效果的...
在Android开发中,二维图形是构建用户界面和创建动态效果不可或缺的部分。Android提供了一个自定义的2D图形库,使得开发者能够绘制图形图像、创建动画,丰富应用的视觉体验。以下将详细介绍Android中二维图形的基本...
在Android开发中,自定义布局是一项常见的需求,它允许开发者创造出独特且富有创意的用户界面。本主题聚焦于“Android自定义气泡布局”,这种布局主要用于实现类似消息提示或者指示箭头的效果,常见于各种应用的导航...
此外,Android还支持ShapeDrawable类,允许开发者通过XML资源文件定义基本图形,如矩形、椭圆和线等,从而实现丰富的可绘制资源。在ShapeViewerActivity中,你可以看到如何使用这些图形元素来构建复杂的视图。 总之...
在Android开发领域,微信UI的高仿设计是一个常见的学习课题,因为微信的用户界面简洁而高效,深受开发者和用户的喜爱。本项目“Android应用源码高仿微信UI”旨在通过源码解析,帮助开发者理解如何在Android平台上...
本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...
在Android应用开发中,EditText控件通常用于接收用户输入,如用户名、密码等。为了提升用户体验,有时我们需要为EditText添加边框,使其看起来更加清晰、专业。本文将详细阐述如何在Android中为EditText添加边框,并...