【问题标题】:How to give animation to FloatingActionButton in android studio如何在android studio中为FloatingActionButton提供动画
【发布时间】:2016-06-02 05:30:58
【问题描述】:

我正在为FloatingActionButton 提供动画以将右侧位置移动到左侧。为此,我在代码下实现。但这不起作用。而且我还想更改FloatingActionButton的图标,当我点击它时。

Java 代码:

fab = (FloatingActionButton)findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TranslateAnimation animation = new TranslateAnimation(400.0f,200.0f,0.0f,0.0f);
                animation.setDuration(5000);
                animation.setRepeatCount(1);
                animation.setRepeatMode(1);
                fab.startAnimation(animation);
            }
        });

【问题讨论】:

  • 使用FlotingActionMenu..!!
  • @janki gadhiya 是否有任何库或示例项目,如果有,请提供链接?

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


【解决方案1】:

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

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.移动动作按钮

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

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

2。按钮类型

您可以使用三种类型的操作按钮:

  • 默认 (56dp)

  • 迷你(40dp)

  • 大(72dp)

3.按钮大小

要使用 ActionButton 大小,请使用:

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

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

4.按钮状态

有两种按钮状态,Action Button 可能驻留在其中:

  • 正常
  • 已按下

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 图像。

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(无描边)

在 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"
            />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2011-02-08
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多