Making “slide from bottom” button (similar to snackbar) (Android)

房东的猫 提交于 2019-12-11 12:10:50

问题


there's something I want to do but don't know how... that would be a button that slides from the bottom of the screen to a certain position, triggered by something. Very similar to how snackbar show, with the difference that it stays in place instead of disappearing and has the property of being "clickable". This would NOT be a panel draggable from the bottom, but a panel that slides automatically triggered by something.

How can I accomplish this?


回答1:


Sounds like a fairly straightforward animation along the y axis, with a start value that matches the height of the screen (such that it renders just off screen), to whatever the final value is. Below code is from memory, but it should work.

To grab the screen height:

int getScreenHeight() {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    return displaymetrics.heightPixels;

}

And to animate a view (in this case, to 80% of the total screen height):

void animateOnScreen(View view) {

    final int screenHeight = getScreenHeight();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", screenHeight, (screenHeight * 0.8F));
    animator.setInterpolator(new DecelerateInterpolator());
    animator.start();

}



回答2:


Sliding a View down by a distance:

view.animate().translationY(distance);

You can later slide the View back to its original position like this:

view.animate().translationY(0);


来源:https://stackoverflow.com/questions/34598554/making-slide-from-bottom-button-similar-to-snackbar-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!