[英]How can I add shadow to ImageButton?
this is my first time posting. 这是我第一次发帖。 And I've been using this website as a great resource.
而且我一直在使用这个网站作为很好的资源。 But I have come upon an issue.
但是我遇到了一个问题。 I've tried applying a shadow effect to my ImageButton but it doest work?
我尝试对我的ImageButton应用阴影效果,但是不起作用? What I'm trying to achieve is the shadow you can see on the floating bubble (Material Design).
我想要实现的是您在浮动气泡(材料设计)上可以看到的阴影。 I have a circle ImageButton, but I wish to add a shadow around it.
我有一个圆形的ImageButton,但希望在其周围添加阴影。 How can I achieve that?
我该如何实现?
This is circle_button.xml 这是circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#C62666"/>
</shape>
This is circle_button_pressed.xml 这是circle_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#E92D6C"/>
</shape>
This is my selector which combines both drawable's into one. 这是我的选择器,将两个drawable组合为一个。 button.xml
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle_button"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="@drawable/circle_button_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="@drawable/circle_button_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/circle_button"
android:state_focused="false"
android:state_pressed="false" />
</selector>
And this is how I apply it to my ImageButton 这就是我将其应用于我的ImageButton的方式
<ImageButton
android:id="@+id/btn_apply"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/scrollView1"
android:layout_marginRight="45dp"
android:layout_marginTop="94dp"
android:adjustViewBounds="true"
android:background="@drawable/button"
android:scaleType="fitCenter"
android:src="@drawable/apply_two" />
So again? 再说一次 How can I add a shadow to my button?
如何为按钮添加阴影? Thanks for your time.
谢谢你的时间。
may help you : You can use a Button: 可能会帮助您:您可以使用按钮:
<ImageButton
android:id="@+id/fab"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/anim"
android:src="@drawable/ic_action_add"
android:elevation="4dp"
/>
where the ic_action_add
is your icon. ic_action_add
是您的图标。
drawable/ripple.xml is: drawable / ripple.xml是:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
anim/anim.xml is: anim / anim.xml是:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_elevation"
android:valueTo="@dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_press_elevation"
android:valueTo="@dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is : Dimens.xml是:
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
With the elevation attribute you should set the Outline via code: 使用height属性,您应该通过代码设置“ 轮廓” :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.