android button shadow,Android Button's background as shape with Shadow

问题

I've made a button background from shapes and is looking quite good for my purpose. The only thing needed is to drop a bit of shadow for it.

Here is the code:

Here's what I want to achieve

630af964ba9f34df361be6685202e560.png

How do I drop the shadow ? My guess is that I need to make another shape but with black/gray background and set some sort of topa nd left padding of margin to make it look like a shadow. But I don't know how to do it... and documentation didn't helped me too much.

Later Edit: I want to add the shadow in xml file and not by code.

Thanks.

回答1:

If you want to stack more shapes one on top of each other then you could use a layer-list. Bellow is the code for the normal item in your selector(with a strip of gray color):

The problem is that you'll not be able to achieve a true shadow look on your Button with this type of drawable. You could use the code from the other answer or a nine patch image that already has shadow on it.

回答2:

Try this...

android:bottom="5px"

android:left="5px">

回答3:

Paint mShadow = new Paint();

// radius=10, y-offset=2, color=black

mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);

// in onDraw(Canvas)

canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);

This code is from Android's Romain Guy available here : http://www.devoxx.com/download/attachments/1705921/D8_C_10_09_04.pdf

回答4:

You may try this:

android:startColor="@color/blue_dark"

android:centerColor="@color/blue_medium"

android:endColor="@color/blue_light"

android:type="linear"

android:angle="90"/>

android:centerX="0.98"

android:centerY="0"

android:startColor="@android:color/transparent"

android:centerColor="@android:color/transparent"

android:endColor="@color/blue_medium"

android:type="linear"

android:angle="90"/>

回答5:

you can try the following code also to get a smooth border shadow for view:

android:bottom="5px"

android:right="5px">

android:width="2dp"

android:color="#ffffff" />

回答6:

In this case I use lib https://github.com/dmytrodanylyk/shadow-layout

Firstly, you should turn on it in gradle

dependencies {

compile 'com.github.dmytrodanylyk.shadow-layout:library:1.0.3'

}

then put your Button into ShadowLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:sl_shadowRadius="3dp"

app:sl_shadowColor="@color/your_color">

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

It's worsk great for me)

来源:https://stackoverflow.com/questions/9974803/android-buttons-background-as-shape-with-shadow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值