当前位置: 动力学知识库 > 问答 > 编程问答 >

java - get rounded elevation-shadow on FloatingActionButton (v21)

问题描述:

I am pretty new to android development and yes I have tried a lot to fix this before asking.

So I am trying to implement the FloatingActionButton (only on v21 for simplicity) and I just can't get the elevation-shadow to be rounded.

Here are the code lines I have already:

In the main activity .java (named overview):

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_overview);

ImageButton fabbutton = (ImageButton) findViewById(R.id.fab);

ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {

@Override

public void getOutline(View view, Outline outline) {

int size = getResources().getDimensionPixelSize(R.dimen.round_button_diameter);

outline.setOval(0, 0, size, size);

}

};

fabbutton.setOutlineProvider(viewOutlineProvider);

main activity .xml:

 <ImageButton

android:id="@+id/fab"

android:layout_width="@dimen/round_button_diameter"

android:layout_height="@dimen/round_button_diameter"

android:layout_gravity="end|bottom"

android:background="@drawable/oval_ripple"

android:src="@android:drawable/ic_input_add"

android:tint="@android:color/white"

android:elevation="@dimen/elevation_low"

android:stateListAnimator="@anim/button_elevation"

android:layout_alignBottom="@+id/expandableListView"

android:layout_alignEnd="@+id/expandableListView"

android:layout_marginRight="@dimen/add_button_margin"

android:layout_marginBottom="@dimen/add_button_margin"

android:contentDescription="@string/fab"

android:cropToPadding="false" />

dimens.xml

<dimen name="round_button_diameter">56dp</dimen>

<dimen name="add_button_margin">16dp</dimen>

<dimen name="elevation_low">8dp</dimen>

<dimen name="elevation_high">14dp</dimen>

oval_ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"

android:color="?android:colorControlHighlight">

<item>

<shape android:shape="oval">

<solid android:color="#fff1d744"/>

</shape>

</item>

button_elevation.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">

<objectAnimator

android:propertyName="translationZ"

android:duration="@android:integer/config_shortAnimTime"

android:valueFrom="8dp"

android:valueTo="14dp"

android:valueType="floatType"/>

</item>

<item>

<objectAnimator

android:propertyName="translationZ"

android:duration="@android:integer/config_shortAnimTime"

android:valueFrom="14dp"

android:valueTo="8dp"

android:valueType="floatType"/>

</item>

I hope I haven't forget any important code-part and sorry for the long post but I am pretty helpless. If it would help I also could upload the AndroidStudio project file.

Greetings from Germany and thanks for the answers. :)

网友答案:

Why do not you use library?

Add this line in you build.gradle

dependencies {
     ...
     compile 'com.getbase:floatingactionbutton:1.3.0'
     ...
}

and in your layout xml file use this:

<com.getbase.floatingactionbutton.FloatingActionButton
    android:id="@+id/common_ring_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    fab:fab_colorNormal="#FF2222"
    fab:fab_colorPressed="#FF0000"
    fab:fab_icon="android:drawable/ic_input_add"/>

But you remember you have to put FloatingButton inside of RelativeLayout

If you have any problem ask in comment :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: