您的位置:

利用layer-list创建炫酷按钮效果

一、什么是layer-list

在Android中,图片资源是通过Drawable类型来表示的。在Drawable API中有很多不同的Drawable类型,如BitmapDrawable、ShapeDrawable、GradientDrawable、LayerDrawable等等。其中,LayerDrawable提供了一种特殊的Drawable类型,可以通过它来组合多张图片资源成为一个图层叠加的效果。

而在LayerDrawable之上,又衍生了另一个Drawable类型:layer-list。它是LayerDrawable的一种更为灵活的扩展,它允许在每个图层中使用一个XML来定义如何绘制这个图层,从而实现更加炫酷的图形效果。

二、layer-list的使用

layer-list的用法非常简单,只需要创建一个XML文件,在其中定义多个item作为图层,然后将这个XML文件作为Drawable资源即可。

下面是一个简单的例子:

<!-- button_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#FF4081" />
        </shape>
    </item>
    <item android:bottom="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#E91E63" />
        </shape>
    </item>
</layer-list>

上面的代码实现了一个长方形的按钮,底部有一个4dp的内阴影效果。其中,第一个item是按钮的正面,使用了圆角矩形加纯色填充的方式;第二个item是按钮的底部,使用了圆角矩形加纯色填充的方式,并设置了一个较浅的颜色值,使它看起来像是按钮的底部有一层阴影。

三、layer-list的高级使用

除了上面所示的基本用法之外,layer-list还提供了众多高级用法,我们可以通过掌握它们来实现更加炫酷的视觉效果。

1. 组合不同Drawable类型

首先,我们可以使用layer-list来组合不同类型的Drawable。比如,我们可以将ShapeDrawable和GradientDrawable组合在一起,从而实现更加多彩和丰富的渐变效果。

<!-- button_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#FF4081" />
        </shape>
    </item>
    <item android:bottom="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <gradient
                android:startColor="#E91E63"
                android:endColor="#2196F3"
                android:type="linear"
                android:angle="135" />
        </shape>
    </item>
</layer-list>

上面的代码在按钮底部添加了一个渐变效果,使用了GradientDrawable来实现。

2. 使用不同的inset

layer-list还提供了inset属性,可以让我们控制每个item在图层中的位置和大小。比如,我们可以将两个不同大小和位置的item在同一个图层中显示。

<!-- button_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#FF4081" />
        </shape>
    </item>
    <item android:bottom="4dp" android:left="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="16dp" />
            <solid android:color="#E91E63" />
        </shape>
    </item>
</layer-list>

上面的代码实现了一个带有圆角的矩形底部,它在原先的按钮形状外挤了4dp的距离,看起来像是底部和正面分别作为两个图层叠加了起来。

3. 使用bitmap

除了使用Drawable资源作为item之外,layer-list还提供了BitmapDrawable作为item类型。通过使用BitmapDrawable,我们可以将一张图片作为背景并叠加到其他Drawable之上。

<!-- button_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/button_bg" />
    </item>
    <item android:bottom="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#E91E63" />
        </shape>
    </item>
</layer-list>

上面的代码使用了一张名为button_bg的图片作为按钮的背景,并在底部添加了一个圆角矩形的Drawable。

四、总结

利用layer-list,我们可以非常轻松地实现各种炫酷的图形效果。通过不同的组合和设置,我们可以实现丰富的图层叠加效果,从而让我们的应用更加吸引人。

完整代码示例:

<!-- button_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#FF4081" />
        </shape>
    </item>
    <item android:bottom="4dp" android:left="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="16dp" />
            <solid android:color="#E91E63" />
        </shape>
    </item>
</layer-list>
利用layer-list创建炫酷按钮效果

2023-05-14
打造炫酷的Android按钮

一、按钮样式的定制 Android系统自带的按钮样式十分单调,如果想要打造炫酷的按钮,我们就需要自己来进行样式的定制。在Android中,我们可以通过shape和selector两种方式来实现按钮的自

2023-12-08
Powermode——一款让你的编辑器更酷炫的插件

2023-05-19
深入了解Layer-List

2023-05-20
Android Layer-List:多层Drawable列

2023-05-20
打造炫酷渐变背景,让你的APP视觉更出众!

2023-05-14
使用CSS创建自定义鼠标光标 - 炫酷的鼠标效果展示 | W

2023-05-12
酷炫c语言代码,c++炫酷代码雨

2023-01-03
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)