`
jguangyou
  • 浏览: 379826 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Drawable资源之ShapeDrawable资源

 
阅读更多

This is a generic shape defined in XML.

file location:
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a GradientDrawable.
resource reference:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
syntax:
<?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:useLevel=["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>
 
elements:
<shape>
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 <stroke> element to define the width of the line.
"ring" A ring shape.

The following attributes are used only when 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, ifandroid: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.
<corners>
Creates rounded corners for the shape. Applies only when the shape is a rectangle.

attributes:

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.
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:radiusto 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.

<gradient>
Specifies a gradient color for the shape.

attributes:

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.
android:centerX
Float. The relative X-position for the center of the gradient (0 - 1.0).
android:centerY
Float. The relative Y-position for the center of the gradient (0 - 1.0).
android:centerColor
Color. Optional color that comes between the start and end colors, as a hexadecimal value orcolor resource.
android:endColor
Color. The ending color, as a hexadecimal value or color resource.
android:gradientRadius
Float. The radius for the gradient. Only applied when android:type="radial".
android:startColor
Color. The starting color, as a hexadecimal value or color resource.
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.
android:useLevel
Boolean. "true" if this is used as a LevelListDrawable.
<padding>
Padding to apply to the containing View element (this pads the position of the View content, not the shape).

attributes:

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.
<size>
The size of the shape.

attributes:

android:height
Dimension. The height of the shape, as a dimension value or dimension resource.
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 theandroid:scaleType to "center".

<solid>
A solid color to fill the shape.

attributes:

android:color
Color. The color to apply to the shape, as a hexadecimal value or color resource.
<stroke>
A stroke line for the shape.

attributes:

android:width
Dimension. The thickness of the line, as a dimension value or dimension resource.
android:color
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 ifandroid:dashGap is set.
example:
XML file saved at res/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="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

 

This layout XML applies the shape drawable to a View:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

 

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);

 

分享到:
评论

相关推荐

    Drawable资源管理图片处理

    在Android开发中,Drawable资源是用于管理应用中各种图形元素,如图片、颜色、形状等的关键组成部分。了解和熟练掌握Drawable资源的管理与图片处理,对于优化应用性能和提升用户体验至关重要。以下是一些关于...

    android安卓APP之13种Drawable位图片资源.zip_android客户端是什么

    本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...

    Drawable.pdf

    ### 可绘制资源(Drawable资源)在Android中的运用与解析 在Android开发中,`Drawable`资源扮演着至关重要的角色,它们是图像的一种抽象表示,主要用于界面设计中的图标、背景和其他视觉元素。`Drawable`资源的灵活...

    【Android开发API】应用程序资源-图形处理类资源-Drawable[参照].pdf

    在Android开发中,Drawable资源是极其重要的一部分,它包含了各种图形和图像的处理方式,能够被绘制到屏幕上。Drawable不仅仅是简单的图片,而是一种抽象的概念,它涵盖了多种图像表现形式。在Android API中,...

    drawable(图片).rar

    在Android项目的res/drawable目录下,开发者通常会根据需要将这些Drawable资源放入相应的子文件夹,如drawable-mdpi、drawable-hdpi等,以确保在不同密度的设备上正确显示。此外,Android Studio还支持vector ...

    Android的Drawable学习Demo

    在Android开发中,Drawable是图形和图像处理的重要组成部分,它涵盖了多种类型的图像资源,如颜色、形状、位图等。本篇文章将深入探讨...同时,合理地管理和复用Drawable资源也有助于减少应用的内存消耗和启动时间。

    安卓Drawable的使用

    1. **BitmapDrawable**:这是基于位图图像的Drawable,通常从本地文件、资源或网络加载。你可以通过`BitmapFactory.decodeResource()`方法或`BitmapDrawable(Bitmap)`构造函数创建BitmapDrawable。 2. **...

    Drawable的基础demo

    首先,`Drawable`是一个接口,有多个实现类,例如`BitmapDrawable`(用于显示位图)、`ShapeDrawable`(用于绘制几何形状)、`ColorDrawable`(用于填充单色)等。每个`Drawable`都有自己的绘制逻辑,可以通过`draw...

    Drawable Bitmap之间的转化

    `Drawable`的主要子类包括`BitmapDrawable`、`VectorDrawable`、`ShapeDrawable`等。 #### 二、Bitmap简介 `Bitmap`则是一种具体的图像格式,它表示一个像素矩阵,可以理解为一张像素图片。`Bitmap`主要用于存储和...

    玩转Android之Drawable的使用

    【玩转Android之Drawable的使用】 Drawable在Android开发中扮演着至关重要的角色,它是图形和图像的基础元素,广泛用于UI设计和自定义视图。在Android中,Drawable不仅仅局限于简单的图片,还包括各种复杂的图形和...

    Android drawable 玩转自定义图片以及bug的解决

    在Android开发中,Drawable是图形资源的核心组成部分,用于在屏幕上绘制图像。自定义drawable不仅可以提升应用的视觉效果,还能实现一些复杂的功能需求。本篇文章将深入探讨如何在Android中玩转自定义图片,并解决...

    ShapeDrawable实例

    在这个"ShapeDrawable实例"中,我们将深入探讨如何利用Shape Drawable来创建各种自定义图形。 1. **Shape类型**: - **Rectange(矩形)**: 最基础的形状,可以通过设置`android:width`和`android:height`来定义...

    自定义Drawable.zip

    系统提供了多种内置的Drawable类型,如ShapeDrawable(用于绘制各种形状)、BitmapDrawable(显示位图)等。开发者可以通过继承Drawable类或实现其接口来自定义自己的Drawable对象,以满足特定的设计需求。 接下来...

    Android高级应用源码-drawable(图片).rar

    在Android开发中,Drawable是处理图像资源的核心类,它不仅限于普通的图片,还包括状态选择器、九宫格图片、形状图等。本资源包"Android高级应用源码-drawable(图片)"提供了关于Android中Drawable使用的源码示例,这...

    Android实现简易猴子摘桃功能图片资源

    1. **Drawable资源**:在Android中,Drawable是一种可以绘制到屏幕上的对象,它可以是位图(BitmapDrawable)、形状(ShapeDrawable)、颜色(ColorDrawable)等。在本案例中,猴子和桃子的图片会被封装为...

    Android开发指南-二维图形[文].pdf

    文档提供了一段示例代码来说明如何在`onCreate`方法中创建`LinearLayout`,并添加一个`ImageView`来展示Drawable资源。在示例中,`ImageView`被添加到`LinearLayout`中,并将这个布局设置为当前Activity的内容视图。...

    Android Drawable Test

    在这个项目中,`testDrawable`可能包含了各种不同类型的Drawable资源,用于演示它们在实际应用中的使用。 Drawable在Android中扮演着多种角色,它可以是简单的颜色、图片、形状,甚至是复杂的动画。以下是一些关于...

    shapeDrawable 的属性大全,使用

    ShapeDrawableDemo项目可能包含了一些示例代码和资源,演示了如何使用这些属性创建各种形状和效果。通过这个项目,开发者可以更深入地理解和掌握Shape Drawable的使用技巧,从而在UI设计中实现更多个性化和动态的...

    android xml shape drawable

    在Android开发中,XML Shape Drawable是一种非常实用的资源类型,它允许开发者通过XML代码来创建图形,如矩形、椭圆、线以及更复杂的形状。这些形状可以用于自定义按钮、背景、图标的外观,提供了丰富的定制选项,...

    Android图像介绍-Drawable

    在Android开发中,Drawable是图像资源的一个核心概念,它不仅限于普通的图片,还包括各种图形、状态列表、层叠图像等。本篇文章将深入探讨Android中的Drawable及其在图像处理中的应用。 首先,Drawable是一个接口,...

Global site tag (gtag.js) - Google Analytics