This is a generic shape defined in XML.
res/drawable/filename.xml
The filename is used as the resource ID.
GradientDrawable
.R.drawable.filename
In XML:
@[package:]drawable/filename
<?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>
<shape>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.android:shape
"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
android:innerRadiusRatio
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
android:thicknessRatio
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
LevelListDrawable
. This should normally be "false" or your shape may not appear.<corners>
attributes:
android:radius
android:topLeftRadius
android:topRightRadius
android:bottomLeftRadius
android:bottomRightRadius
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.
<gradient>
attributes:
android:angle
android:centerX
android:centerY
android:centerColor
android:endColor
android:gradientRadius
android:type="radial"
.android:startColor
android:type
"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
LevelListDrawable
.<padding>
attributes:
android:left
android:top
android:right
android:bottom
<size>
attributes:
android:height
android:width
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>
attributes:
android:color
<stroke>
attributes:
android:width
android:color
android:dashGap
android:dashWidth
is set.android:dashWidth
android:dashGap
is set.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);
相关推荐
在Android开发中,Drawable资源是用于管理应用中各种图形元素,如图片、颜色、形状等的关键组成部分。了解和熟练掌握Drawable资源的管理与图片处理,对于优化应用性能和提升用户体验至关重要。以下是一些关于...
本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...
### 可绘制资源(Drawable资源)在Android中的运用与解析 在Android开发中,`Drawable`资源扮演着至关重要的角色,它们是图像的一种抽象表示,主要用于界面设计中的图标、背景和其他视觉元素。`Drawable`资源的灵活...
在Android开发中,Drawable资源是极其重要的一部分,它包含了各种图形和图像的处理方式,能够被绘制到屏幕上。Drawable不仅仅是简单的图片,而是一种抽象的概念,它涵盖了多种图像表现形式。在Android API中,...
在Android项目的res/drawable目录下,开发者通常会根据需要将这些Drawable资源放入相应的子文件夹,如drawable-mdpi、drawable-hdpi等,以确保在不同密度的设备上正确显示。此外,Android Studio还支持vector ...
在Android开发中,Drawable是图形和图像处理的重要组成部分,它涵盖了多种类型的图像资源,如颜色、形状、位图等。本篇文章将深入探讨...同时,合理地管理和复用Drawable资源也有助于减少应用的内存消耗和启动时间。
1. **BitmapDrawable**:这是基于位图图像的Drawable,通常从本地文件、资源或网络加载。你可以通过`BitmapFactory.decodeResource()`方法或`BitmapDrawable(Bitmap)`构造函数创建BitmapDrawable。 2. **...
首先,`Drawable`是一个接口,有多个实现类,例如`BitmapDrawable`(用于显示位图)、`ShapeDrawable`(用于绘制几何形状)、`ColorDrawable`(用于填充单色)等。每个`Drawable`都有自己的绘制逻辑,可以通过`draw...
`Drawable`的主要子类包括`BitmapDrawable`、`VectorDrawable`、`ShapeDrawable`等。 #### 二、Bitmap简介 `Bitmap`则是一种具体的图像格式,它表示一个像素矩阵,可以理解为一张像素图片。`Bitmap`主要用于存储和...
【玩转Android之Drawable的使用】 Drawable在Android开发中扮演着至关重要的角色,它是图形和图像的基础元素,广泛用于UI设计和自定义视图。在Android中,Drawable不仅仅局限于简单的图片,还包括各种复杂的图形和...
在Android开发中,Drawable是图形资源的核心组成部分,用于在屏幕上绘制图像。自定义drawable不仅可以提升应用的视觉效果,还能实现一些复杂的功能需求。本篇文章将深入探讨如何在Android中玩转自定义图片,并解决...
在这个"ShapeDrawable实例"中,我们将深入探讨如何利用Shape Drawable来创建各种自定义图形。 1. **Shape类型**: - **Rectange(矩形)**: 最基础的形状,可以通过设置`android:width`和`android:height`来定义...
系统提供了多种内置的Drawable类型,如ShapeDrawable(用于绘制各种形状)、BitmapDrawable(显示位图)等。开发者可以通过继承Drawable类或实现其接口来自定义自己的Drawable对象,以满足特定的设计需求。 接下来...
在Android开发中,Drawable是处理图像资源的核心类,它不仅限于普通的图片,还包括状态选择器、九宫格图片、形状图等。本资源包"Android高级应用源码-drawable(图片)"提供了关于Android中Drawable使用的源码示例,这...
1. **Drawable资源**:在Android中,Drawable是一种可以绘制到屏幕上的对象,它可以是位图(BitmapDrawable)、形状(ShapeDrawable)、颜色(ColorDrawable)等。在本案例中,猴子和桃子的图片会被封装为...
文档提供了一段示例代码来说明如何在`onCreate`方法中创建`LinearLayout`,并添加一个`ImageView`来展示Drawable资源。在示例中,`ImageView`被添加到`LinearLayout`中,并将这个布局设置为当前Activity的内容视图。...
在这个项目中,`testDrawable`可能包含了各种不同类型的Drawable资源,用于演示它们在实际应用中的使用。 Drawable在Android中扮演着多种角色,它可以是简单的颜色、图片、形状,甚至是复杂的动画。以下是一些关于...
ShapeDrawableDemo项目可能包含了一些示例代码和资源,演示了如何使用这些属性创建各种形状和效果。通过这个项目,开发者可以更深入地理解和掌握Shape Drawable的使用技巧,从而在UI设计中实现更多个性化和动态的...
在Android开发中,XML Shape Drawable是一种非常实用的资源类型,它允许开发者通过XML代码来创建图形,如矩形、椭圆、线以及更复杂的形状。这些形状可以用于自定义按钮、背景、图标的外观,提供了丰富的定制选项,...
在Android开发中,Drawable是图像资源的一个核心概念,它不仅限于普通的图片,还包括各种图形、状态列表、层叠图像等。本篇文章将深入探讨Android中的Drawable及其在图像处理中的应用。 首先,Drawable是一个接口,...