Java Code Examples for android.support.design.widget.FloatingActionButton#setTranslationY()

The following examples show how to use android.support.design.widget.FloatingActionButton#setTranslationY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToolbarHideShowBehavior.java    From JReadHub with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    if (dependency instanceof AppBarLayout) {
        //FAB的自身高度+据底边高度的和=要实现FAB隐藏需要向下移动的距离
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = fab.getHeight() + fabBottomMargin;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
            //toolbar被移走的距离与本身高度的比值
            float ty = dependency.getY() - (float) statusBarHeight;
            float ratio = ty / (float) toolbarHeight;
            //toolbar被移走几分之几,fab就向下滑几分之几
            fab.setTranslationY(-distanceToScroll * ratio);
        }
    }
    return true;
}
 
Example 2
Source File: ScaleDownShowBehavior.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    boolean returnValue = super.onDependentViewChanged(parent, child, dependency);
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = child.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        child.setTranslationY(-distanceToScroll * ratio);
    }

    return returnValue;
}
 
Example 3
Source File: ScaleDownShowBehavior.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    boolean returnValue = super.onDependentViewChanged(parent, child, dependency);
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = child.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        child.setTranslationY(-distanceToScroll * ratio);
    }

    return returnValue;
}
 
Example 4
Source File: FloatingActionButtonBehavior.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override public boolean onDependentViewChanged(@NonNull
                                                final CoordinatorLayout parent, @NonNull
                                                final FloatingActionButton child, final View dependency) {
    log(TAG, INFO, "onDependentViewChanged: " + dependency);
    final List<View> list = parent.getDependencies(child);
    ViewGroup.MarginLayoutParams params = ((ViewGroup.MarginLayoutParams) child.getLayoutParams());
    int bottomMargin = (params.bottomMargin + params.rightMargin) - (params.topMargin + params.leftMargin);
    float t = 0;
    float t2 = 0;
    float t3 = 0;
    boolean result = false;
    for (View dep : list) {
        if (Snackbar.SnackbarLayout.class.isInstance(dep)) {
            t += dep.getTranslationY() - dep.getHeight();
            result = true;
        } else if (BottomNavigation.class.isInstance(dep)) {
            BottomNavigation navigation = (BottomNavigation) dep;
            t2 = navigation.getTranslationY() - navigation.getHeight() + bottomMargin;
            t += t2;
            result = true;

            if (navigationBarHeight > 0) {
                if (!navigation.isExpanded()) {
                    child.hide();
                } else {
                    child.show();
                }
            }
        }
    }

    if (navigationBarHeight > 0 && t2 < 0) {
        t = Math.min(t2, t + navigationBarHeight);
    }

    child.setTranslationY(t);
    return result;
}
 
Example 5
Source File: RotateBehavior.java    From CustomBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    float translationY = getFabTranslationYForSnackbar(parent, child);
    float percentComplete = -translationY / dependency.getHeight();
    child.setRotation(-90 * percentComplete);
    child.setTranslationY(translationY);
    return false;
}
 
Example 6
Source File: ScrollingFABBehavior.java    From AwesomeSplash with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = fab.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        fab.setTranslationY(-distanceToScroll * ratio);
    }
    return true;
}
 
Example 7
Source File: FABScrollBehavior.java    From WanAndroid with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    child.setTranslationY(Math.max(0.0f, Math.min(child.getHeight() * 2, child.getTranslationY() + dy)));
}
 
Example 8
Source File: BottomNavigationFABBehavior.java    From WanAndroid with GNU General Public License v3.0 4 votes vote down vote up
public void onDependentViewRemoved(@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton child, @NonNull View dependency) {
    child.setTranslationY(0.0f);
}
 
Example 9
Source File: FloatingActionMenu.java    From MaterialWpp with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    System.out.println("onLayout:" + changed);
    if (changed) {
        int right = r - getPaddingRight();
        int bottom = b - getPaddingBottom();
        int top = bottom - mMenuButton.getMeasuredHeight();
        mMenuButton.layout(right - mMenuButton.getMeasuredWidth(), top, right, bottom);
        int dw = (mMenuButton.getMeasuredWidth() - mIcon.getMeasuredWidth()) / 2;
        int dh = (mMenuButton.getMeasuredHeight() - mIcon.getMeasuredHeight()) / 2;
        mIcon.layout(right - mIcon.getMeasuredWidth() - dw,
                bottom - mIcon.getMeasuredHeight() - dh, right - dw, bottom - dh);

        if (isCircle) {
            if (mMenuItems.size() < 2) {
                Log.e("onLayout", "Floating Action Buttons must more then one!");
                return;
            }
            double angle = Math.PI/2d/(mMenuItems.size() - 1);
            for (int i = 0; i < mMenuItems.size(); i++) {
                FloatingActionButton itemFB = mMenuItems.get(i);
                int fbWidth = itemFB.getMeasuredWidth();
                int fbHeight = itemFB.getMeasuredHeight();
                if (0 != multipleOfFB) {
                    mRadius = (int) (fbWidth * multipleOfFB);
                }
                int itemDw = (mMenuButton.getMeasuredWidth() - fbWidth) / 2;
                int itemDh = (mMenuButton.getMeasuredHeight() - fbHeight) / 2;
                int itemX = (int) (mRadius*Math.cos(i*angle));
                int itemY = (int) (mRadius*Math.sin(i*angle));
                itemFB.layout(right - itemX - fbWidth - itemDw, bottom - itemY - fbHeight - itemDh,
                        right - itemX - itemDw, bottom - itemY - itemDh);

                if (!animating) {
                    if (!mOpen) {
                        itemFB.setTranslationY(mMenuButton.getTop() - itemFB.getTop());
                        itemFB.setTranslationX(mMenuButton.getLeft() - itemFB.getLeft());
                        itemFB.setVisibility(GONE);
                    } else {
                        itemFB.setTranslationY(0);
                        itemFB.setTranslationX(0);
                        itemFB.setVisibility(VISIBLE);
                    }
                }
            }
        } else {
            for (int i = 0; i < mMenuItems.size(); i++) {
                FloatingActionButton item = mMenuItems.get(i);
                TextView label = mMenuItemLabels.get(i);

                label.setBackgroundResource(R.drawable.rounded_corners);
                bottom = top -= mItemGap;

                top -= item.getMeasuredHeight();
                int width = item.getMeasuredWidth();
                int d = (mMenuButton.getMeasuredWidth() - width) / 2;
                item.layout(right - width - d, top, right - d, bottom);
                d = (item.getMeasuredHeight() - label.getMeasuredHeight()) / 2;

                label.layout(item.getLeft() - label.getMeasuredWidth() - 50,
                        item.getTop() + d, item.getLeft(),
                        item.getTop() + d + label.getMeasuredHeight());
                if (!animating) {
                    if (!mOpen) {
                        item.setTranslationY(mMenuButton.getTop() - item.getTop());
                        item.setVisibility(GONE);
                        label.setVisibility(GONE);
                    } else {
                        item.setTranslationY(0);
                        item.setVisibility(VISIBLE);
                        label.setVisibility(VISIBLE);
                    }
                }
            }
        }
        if (!animating && getBackground() != null) {
            if (!mOpen) {
                getBackground().setAlpha(0);
            } else {
                getBackground().setAlpha(0xff);
            }
        }
    }
}
 
Example 10
Source File: SnackbarBehavior.java    From CoordinatorLayoutSample with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
    child.setTranslationY(translationY);
    return true;
}
 
Example 11
Source File: FloatingActionMenu.java    From dttv-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the layout of the ViewGroup dependent on the number of menu items as well as menu direction.
 */
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    System.out.println("onLayout:" + changed);
    if (changed) {
        int right = r - getPaddingRight();
        int bottom = b - getPaddingBottom();
        int top = bottom - mMenuButton.getMeasuredHeight();
        mMenuButton.layout(right - mMenuButton.getMeasuredWidth(), top, right, bottom);
        int dw = (mMenuButton.getMeasuredWidth() - mIcon.getMeasuredWidth()) / 2;
        int dh = (mMenuButton.getMeasuredHeight() - mIcon.getMeasuredHeight()) / 2;
        mIcon.layout(right - mIcon.getMeasuredWidth() - dw,
                bottom - mIcon.getMeasuredHeight() - dh, right - dw, bottom - dh);

        if (isCircle) {
            if (mMenuItems.size() < 2) {
                Log.e("onLayout", "Floating Action Buttons must more then one!");
                return;
            }
            double angle = Math.PI/2d/(mMenuItems.size() - 1);
            for (int i = 0; i < mMenuItems.size(); i++) {
                FloatingActionButton itemFB = mMenuItems.get(i);
                int fbWidth = itemFB.getMeasuredWidth();
                int fbHeight = itemFB.getMeasuredHeight();
                if (0 != multipleOfFB) {
                    mRadius = (int) (fbWidth * multipleOfFB);
                }
                int itemDw = (mMenuButton.getMeasuredWidth() - fbWidth) / 2;
                int itemDh = (mMenuButton.getMeasuredHeight() - fbHeight) / 2;
                int itemX = (int) (mRadius*Math.cos(i*angle));
                int itemY = (int) (mRadius*Math.sin(i*angle));
                itemFB.layout(right - itemX - fbWidth - itemDw, bottom - itemY - fbHeight - itemDh,
                        right - itemX - itemDw, bottom - itemY - itemDh);

                if (!animating) {
                    if (!mOpen) {
                        itemFB.setTranslationY(mMenuButton.getTop() - itemFB.getTop());
                        itemFB.setTranslationX(mMenuButton.getLeft() - itemFB.getLeft());
                        itemFB.setVisibility(GONE);
                    } else {
                        itemFB.setTranslationY(0);
                        itemFB.setTranslationX(0);
                        itemFB.setVisibility(VISIBLE);
                    }
                }
            }
        } else {
            for (int i = 0; i < mMenuItems.size(); i++) {
                FloatingActionButton item = mMenuItems.get(i);
                TextView label = mMenuItemLabels.get(i);

                label.setBackgroundResource(R.drawable.rounded_corners);
                bottom = top -= mItemGap;

                top -= item.getMeasuredHeight();
                int width = item.getMeasuredWidth();
                int d = (mMenuButton.getMeasuredWidth() - width) / 2;
                item.layout(right - width - d, top, right - d, bottom);
                d = (item.getMeasuredHeight() - label.getMeasuredHeight()) / 2;

                label.layout(item.getLeft() - label.getMeasuredWidth() - 50,
                        item.getTop() + d, item.getLeft(),
                        item.getTop() + d + label.getMeasuredHeight());
                if (!animating) {
                    if (!mOpen) {
                        item.setTranslationY(mMenuButton.getTop() - item.getTop());
                        item.setVisibility(GONE);
                        label.setVisibility(GONE);
                    } else {
                        item.setTranslationY(0);
                        item.setVisibility(VISIBLE);
                        label.setVisibility(VISIBLE);
                    }
                }
            }
        }
        if (!animating && getBackground() != null) {
            if (!mOpen) {
                getBackground().setAlpha(0);
            } else {
                getBackground().setAlpha(0xff);
            }
        }
    }
}