简体   繁体   English

Android渐变浮雕效果

[英]Android gradient emboss effect

I am trying to achieve from code the following: (can't post images unfortunatelly) 我正在尝试从代码中实现以下目的:(不能不幸地发布图像)

A rectangle with rounded corners, with an emboss effect (the light comes from top left corner). 具有圆角且具有浮雕效果的矩形(光线来自左上角)。 In the middle there is a circle engraved in the rectangle. 中间有一个刻在矩形上的圆。 Imagine a water surface, and a drop of water hits the surface. 想象一下水面,一滴水撞击水面。 It creates a dent in the surface. 它会在表面产生凹痕。 That circle is also painted with some linear gradient. 该圆也涂有一些线性渐变。

The problem is I could only use the EmbossMaskFilter from Android to raise the surface, to make it closer to the user eye, but I don't know how to implement the opposite. 问题是我只能使用Android的EmbossMaskFilter来提升表面,使其更贴近用户的眼睛,但是我不知道如何实现相反的效果。 Anyone can help me with that? 有人可以帮我吗?

Thank you very much. 非常感谢你。

Use a composite drawable, or drawables in layers. 使用复合可绘制对象或多层可绘制对象。

To create a drawable with rounded corners and a gradient within, use something like this: 要创建一个带有圆角和内部渐变的可绘制对象,请使用以下方法:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="10dp"/>
    <gradient android:startColor="@color/gradientstart" android:endColor="@color/gradientend" android:angle="315"/>
</shape>

Create two such drawables and put them on top of each other to create the required effect. 创建两个这样的可绘制对象,并将它们相互叠加以创建所需的效果。

Unless you use a pretty good number of layer-list items as a drawable you probably won't get the effect you are looking for easily with XML drawables. 除非您将相当数量的图层列表项用作可绘制对象,否则使用XML可绘制对象可能无法轻松获得所需的效果。 A better solution would be to create a 9-patch image. 更好的解决方案是创建9补丁图像。 See how it works at draw9patch.com , which is a tool to create 9 patch images from a standard images. draw9patch.com上查看其工作原理 ,该工具可从标准图像创建9个补丁图像。

NinePatch documentation: here . NinePatch文档: 在此处

In case, you still really want to use xml drawables you can still achieve the effect (although performance might take a hit) using a layer-list with multiple gradients stacked ontop of each other. 万一,您仍然真的想使用xml drawable,您仍然可以使用具有多个渐变彼此叠加的图层列表来达到效果(尽管性能可能会受到影响)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM