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

Android自定义Shape 加上阴影shadow之方法

 
阅读更多

Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:

  1. xmlversion="1.0"encoding="utf-8"?>
  2. <shape
  3. xmlns:Android="http://schemas.android.com/apk/res/android"
  4. Android:shape=["rectangle"|"oval"|"line"|"ring"]>
  5. <corners
  6. Android:radius="integer"
  7. Android:topLeftRadius="integer"
  8. Android:topRightRadius="integer"
  9. Android:bottomLeftRadius="integer"
  10. Android:bottomRightRadius="integer"/>
  11. <gradient
  12. Android:angle="integer"
  13. Android:centerX="integer"
  14. Android:centerY="integer"
  15. Android:centerColor="integer"
  16. Android:endColor="color"
  17. Android:gradientRadius="integer"
  18. Android:startColor="color"
  19. Android:type=["linear"|"radial"|"sweep"]
  20. Android:usesLevel=["true"|"false"]/>
  21. <padding
  22. Android:left="integer"
  23. Android:top="integer"
  24. Android:right="integer"
  25. Android:bottom="integer"/>
  26. <size
  27. Android:width="integer"
  28. Android:height="integer"/>
  29. <solid
  30. Android:color="color"/>
  31. <stroke
  32. Android:width="integer"
  33. Android:color="color"
  34. Android:dashWidth="integer"
  35. Android:dashGap="integer"/>
  36. shape>

其支持的属性没有shadow, 做Web前端开发的同学写CSS可以很方便地加一个shadow属性值,如何给Android Shape加一个shadow,以得到类似的效果呢?

答案是使用layer-list ! 直接上代码如下:

  1. xmlversion="1.0"encoding="utf-8"?>
  2. <layer-listxmlns:Android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <shapeAndroid:shape="rectangle">
  5. <solidAndroid:color="#792a03"/>
  6. <cornersAndroid:radius="19dp"/>
  7. shape>
  8. item>
  9. <itemAndroid:top="1px">
  10. <shapeAndroid:shape="rectangle">
  11. <gradientAndroid:startColor="#ffdb8f"android:endColor="#ffdb8f"
  12. Android:angle="270"/>
  13. <paddingAndroid:left="5dp"android:top="3dp"android:right="5dp"
  14. Android:bottom="3dp"/>
  15. <cornersAndroid:radius="20dp"/>
  16. shape>
  17. item>
  18. layer-list>

将以上xml存成btn_test, 放到res/drawable/目录下。 将该drawable xml设为一个TextView的backgroiund,

  1. <TextView
  2. Android:background="@drawable/btn_test"
  3. Android:layout_marginTop="20dip"
  4. Android:layout_marginLeft="5dip"
  5. Android:textColor="#792a03"
  6. Android:text="1天2小时14分20秒"
  7. Android:layout_width="wrap_content"
  8. Android:layout_height="wrap_content"/>

其效果如下图所示:

关于layer-list的进一步解释见SDK文档,如下:

Layer List

A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

Each drawable is represented by an element inside a single element.

file location:
res/drawable/filename .xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a LayerDrawable .
resource reference:
In Java: R.drawable.filename
In XML: @[package :]drawable/filename
syntax:

Xml代码
  1. xmlversion="1.0"encoding="utf-8"?>
  2. <layer-list
  3. xmlns:Android="http://schemas.android.com/apk/res/android">
  4. <item
  5. Android:drawable="@[package:]drawable/drawable_resource"
  6. Android:id="@[+][package:]id/resource_name"
  7. Android:top="dimension"
  8. Android:right="dimension"
  9. Android:bottom="dimension"
  10. Android:left="dimension"/>
  11. layer-list>

elements:
Required. This must be the root element. Contains one or more elements.

attributes:

xmlns:Android
String . Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android" .
Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a element. Accepts child elements.

attributes:

Android:drawable
Drawable resource . Required . Reference to a drawable resource.
android:id
Resource ID . A unique resource ID for this drawable. To create a new resource ID for this item, use the form: "@+id/name " . The plus symbol indicates that this should be created as a new ID. You can use this identifier to retrieve and modify the drawable with View.findViewById() or Activity.findViewById() .
android:top
Integer . The top offset in pixels.
android:right
Integer . The right offset in pixels.
android:bottom
Integer . The bottom offset in pixels.
android:left
Integer . The left offset in pixels.

All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a element inside the element to specify the drawable and define the gravity to something that does not scale, such as "center" . For example, the following defines an item that scales to fit its container View:

Xml代码
  1. <itemAndroid:drawable="@drawable/image" />

    To avoid scaling, the following example uses a element with centered gravity:

    Xml代码
    1. <item>
    2. <bitmapAndroid:src="@drawable/image"
    3. Android:gravity="center"/>
    4. item>
    example:
    XML file saved at res/drawable/layers.xml :
分享到:
评论

相关推荐

    Android编程使用自定义shape实现shadow阴影效果的方法

    本文将详细讲解如何通过自定义shape来实现阴影效果。首先,我们需要理解Android中的`&lt;shape&gt;`和`&lt;layer-list&gt;`标签。 `&lt;shape&gt;`标签用于创建各种几何形状,如矩形、椭圆等,并能为其填充颜色、设置边框等。在这个...

    android 图像 阴影 投影 背影 圆角等

    接着,投影(Shadow Projection)在Android中通常指的是类似阴影的效果,但通常用于控件的边缘,使它们看起来像是浮在其他元素之上。这可以通过设置`View`的`outlineProvider`和`clipToOutline`属性实现。例如,可以...

    Android 自定义阴影效果详解及实例

    总结来说,Android自定义阴影效果主要依赖于`&lt;layer-list&gt;`和LayerDrawable,通过创建多层Drawable并调整它们的相对位置和颜色,来模拟阴影。这种技术不仅适用于`TabLayout`,还可以应用于任何支持设置背景的View或 ...

    Android应用源码之阴影和影子.zip

    为了简化阴影处理,开发者社区还提供了一些第三方库,如`android-shape-shadow`和`android-view-shadow`,它们提供了更灵活的阴影配置和更好的性能优化。 综上所述,Android应用中的阴影和影子实现涉及到多个层面...

    Android源码——图片阴影效果和影子效果源码.zip

    此外,第三方库如`Android-Shape-Image-View`和`ShadowView`提供了更便捷的方式来实现阴影效果,它们通常提供更多的定制选项和更好的性能优化。 总结来说,Android提供了多种方式来实现图片和UI元素的阴影效果,...

    安卓Android源码——图片阴影效果和影子效果源码.zip

    开发者还可以利用第三方库,如`android-shadow`或`Android-Shape-Image-View`,它们提供了更加便捷的方式来实现阴影效果,简化代码。 综上所述,实现图片阴影和影子效果在Android开发中涉及到多种技术,包括基本的...

    Android 图片阴影效果和影子效果源码.zip

    Android社区还提供了许多第三方库,如`android-shape-imageview`和`android-gradient-shadow`,它们简化了阴影效果的实现。 总之,Android为开发者提供了多种方式来实现图片阴影和影子效果,可以根据项目需求和...

    Android 图片阴影效果和影子效果源码 .rar

    在Android中,有两种主要的方式来实现阴影效果:`Paint`对象的`setShadowLayer()`方法和`CardView`组件。 1. `Paint`对象的`setShadowLayer()` 这个方法允许你在绘制图形时添加一个阴影层。参数包括模糊半径、阴影...

    Android 图片阴影效果和影子效果源码.rar

    - **`Android-Shape-Image-View`**: 这是一个开源库,提供了带有自定义阴影效果的ImageView,简化了阴影效果的实现。 - **`Carbon`库**: Carbon库是一个跨平台的UI框架,支持Android和iOS,提供了Material Design...

    Android 给控件添加边框阴影效果

    除了使用`layer-list`,Android还提供了其他方法来实现控件的阴影效果,如`CardView`组件自带的阴影效果,或者利用`Paint`对象的`setShadowLayer()`方法在自定义视图中绘制阴影。然而,使用XML drawable的方式简单且...

    Android程序研发源码图片阴影效果和影子效果.zip

    - **Android社区提供了许多库**,如`android-shape-imageview`和`android-floating-action-button`,它们封装了阴影效果,使得开发者能轻松添加具有阴影的ImageView或FloatingActionButton。 7. **性能考虑** - *...

    Android给布局、控件加阴影效果的示例代码

    本文将详细讲解两种实现Android布局和控件阴影效果的方法。 方法一:使用`android:elevation`属性 从Android Lollipop (API 21) 开始,系统引入了`android:elevation`属性,它可以直接给布局或控件添加阴影效果。`...

    通过一行代码即可实现阴影效果.zip

    Android系统默认的Shadow类并不直接支持二维阴影,但我们可以借助其他技术来实现。这个开源项目可能利用了`layer-list`、`shape`以及`NinePatch`等Drawable资源来构造阴影。`layer-list`可以将多个Drawable堆叠起来...

    头部阴影渐变效果基于eclipse

    如果需要自定义更多的头部阴影效果,比如动态改变颜色或阴影的深浅,可以扩展`View`类并重写`onDraw()`方法,或者使用第三方库,如`CardView`,它提供了方便的阴影效果。 总之,"头部阴影渐变效果基于Eclipse"是一...

    android圆形360旋转按钮

    在Android开发中,创建独特且吸引用户的界面是至关重要的,其中按钮控件是用户交互的核心元素之一。"Android圆形360旋转按钮"是一个专为提升用户体验设计的组件,它结合了美观与动态效果,带给用户更加直观的操作...

    android studio 抽屉和其他效果

    3. **圆角矩形(Rounded Corners)**:Material Design提倡使用圆角矩形,开发者可以通过`android:background`属性或自定义Shape资源来实现。 4. **颜色过滤(Color Filter)**:可以改变视图的颜色,如使用饱和度...

Global site tag (gtag.js) - Google Analytics