我正在学习新的Material Design,但是遇到了一些问题。
我想知道是否有人知道如何使用浮动操作按钮创建进度条,就像这个链接中的效果: https://dribbble.com/shots/1644982-Animated-circle-loader-FAB-with-integration-gif
在Android L(5.0)上是否有相应的API支持?
谢谢!
我正在学习新的Material Design,但是遇到了一些问题。
我想知道是否有人知道如何使用浮动操作按钮创建进度条,就像这个链接中的效果: https://dribbble.com/shots/1644982-Animated-circle-loader-FAB-with-integration-gif
在Android L(5.0)上是否有相应的API支持?
谢谢!
这对我很有用,我使用了一个RelativeLayout,并将两个内容都放在RelativeLayout的中央。
...
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
app:layout_anchor="@id/my_recycler_view"
app:layout_anchorGravity="bottom|right|end"
>
<ProgressBar
android:id="@+id/fabProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:indeterminateTint="@color/colorPrimary"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/newReportButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
app:fabSize="normal"
android:layout_centerInParent="true"
/>
</RelativeLayout>
...
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_backup_black_24dp" />
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
我已经解决了这个问题,通过将进度条添加到布局中,并使用自定义行为来控制进度条。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="es.mediaserver.newinterfacedlna.MainActivity">
<android.support.design.widget.AppBarLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/include" />
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="28dp"
android:layout_gravity="bottom|center"
app:srcCompat="@android:drawable/ic_media_play" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="bottom|center"
android:indeterminate="true"
android:layout_margin="@dimen/fab_margin"
/>
</android.support.design.widget.CoordinatorLayout>
自定义行为组件
public static class SnackBarBehavior extends CoordinatorLayout.Behavior<View> {
private static final boolean SNACKBAR_BEHAVIOR_ENABLED;
public SnackBarBehavior() {
super();
}
public SnackBarBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
child.setTranslationY(0);
}
static {
SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
}
}
最后,在onCreate方法中(或在布局中使用app:layout_behavior="xxx.xxx.SnackBarBehavior")
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
SnackBarBehavior snackBarBehavior = new SnackBarBehavior();
CoordinatorLayout.LayoutParams coordinatorParams = (CoordinatorLayout.LayoutParams) progressBar.getLayoutParams();
coordinatorParams.setBehavior(snackBarBehavior);
注意:我基于此处发布的组件来构建我的行为组件 如何像浮动按钮一样将视图移动到 Snackbar 上方
您可以使用以下库来实现材料风格的按钮 > https://github.com/makovkastar/FloatingActionButton (或者这个 > https://github.com/futuresimple/android-floating-action-button)
对于下拉菜单,您也可以使用这个库 > https://github.com/oguzbilgener/CircularFloatingActionMenu
然后,您可以通过将它们添加到RelativeLayout(一个在另一个上面)并添加一些动画,例如扩展带缩放的按钮,扩展进度条带缩放等,将这两个组件组合起来。然后,您只需要连接到进度条代码以在加载完成后将其收缩即可,即将其缩小到按钮后面。
对于向后兼容的动画,请查看这里的9个旧版Android > http://nineoldandroids.com/
希望这有所帮助。