如何在android studio中为FloatingActionButton提供动画

时间:2016-06-02 05:30:59

标签: android-layout animation android-studio floating-action-button

我正在给动画a移动右侧位置到左侧。为此,我正在实施以下代码。但那不行。我还想更改FloatingActionButton当我点击它时的图标。

Java代码:

FloatingActionButton

1 个答案:

答案 0 :(得分:0)

使用这个库我解决了我的问题。

https://github.com/shell-software/fab

第1步:您必须将此依赖项放在build.gradle文件中

dependencies {
    compile 'com.github.shell-software:fab:1.1.2'
}

第2步:将此内容写入您的xml文件中。

 <com.software.shell.fab.ActionButton 
            android:id="@+id/action_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" 
            android:layout_alignParentRight="true"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginBottom="@dimen/fab_margin"
            />

步骤:3 初始化ActionButton

ActionButton actionButton = (ActionButton) findViewById(R.id.action_button);

步骤:4 为您的不同目的应用不同的效果。

<强> 1。移动ActionButton

int distance = 100.0f // in density-independent pixels

// Move ActionButton left
actionButton.moveLeft(distance);

<强> 2。按钮类型

有三种类型的动作按钮,您可以使用它们:

  • DEFAULT(56dp)

  • MINI(40dp)

  • BIG(72dp)

第3。按钮大小

使用ActionButton大小:

// To get the ActionButton size
float size = actionButton.getSize();

// To set the ActionButton size
actionButton.setSize(60.0f); // 

<强> 4。按钮状态

有两种按钮状态,操作按钮可以驻留在:

  • 正常
  • PRESSED

<强> 5。按钮颜色

按钮颜色可以设置为NORMAL状态,因此对于PRESSED状态。默认情况下,设置以下颜色:

 #FF9B9B9B for the NORMAL state
 #FF696969 for the PRESSED state

<强> 6。影

可以通过三种方式定制阴影:颜色,半径和偏移。默认情况下启用阴影并具有以下默认值:

  • shadowColor =#42000000(〜浅灰色)
  • shadowRadius = 8.0f(与密度无关的像素)
  • shadowXOffset = 0.0f(与密度无关的像素)
  • shadowYOffset = 8.0f(与密度无关的像素)

<强> 7。图片

您可以使用以下代码更改actionButton Image。

actionButton.setImageBitmap(bitmap);
actionButton.setImageDrawable(getResource.getDrawable(R.drawable.fab_plus_icon));
actionButton.setImageResource(R.drawable.fab_plus_icon);

<强> 8。中风

可以通过两种方式定制笔划:宽度和颜色。默认情况下禁用笔划,但它具有默认值:

  • strokeColor = Color.BLACK
  • strokeWidth = 0.0f(无笔划)

InShort

在Xml中你直接定义所有东西

xml是:

<com.software.shell.fab.ActionButton 
            android:id="@+id/action_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/fab_margin"
            android:layout_marginBottom="@dimen/fab_margin"
            fab:type="DEFAULT"
            fab:button_color="@color/fab_material_lime_500"
            fab:button_colorPressed="@color/fab_material_lime_900"
            fab:image="@drawable/fab_plus_icon"
            fab:image_size="24dp"
            fab:shadow_color="#757575"
            fab:shadow_radius="1.0dp"
            fab:shadow_xOffset="0.5dp"
            fab:shadow_yOffset="1.0dp"
            fab:stroke_color="@color/fab_material_blue_grey_500"
            fab:stroke_width="1.0dp"
            fab:button_colorRipple="@color/fab_material_black"
            fab:rippleEffect_enabled="false"
            fab:shadowResponsiveEffect_enabled="true"
            fab:show_animation="@anim/fab_roll_from_down"
            fab:hide_animation="@anim/fab_roll_to_down"
            />