擴展Snackbar 使其支持居中顯示

https://github.com/nispok/snackbarjava

默認Snackbar支持底部或者頂部顯示,不支持居中顯示android

查看Snackbar.java的源碼能夠看到createMarginLayoutParams方法中有位置的處理SnackbarPosition ,git

針對不一樣的顯示位置設置對應的params規則,這裏咱們將表示位置的枚舉SnackbarPosition中添加一種類型PARENT_CENTERgithub

public static enum SnackbarPosition {
        TOP(Gravity.TOP), BOTTOM(Gravity.BOTTOM), BOTTOM_CENTER(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL)
        , PARENT_CENTER(Gravity.CENTER_VERTICAL);
}

createMarginLayoutParams方法中部分邏輯修改以下:ide

else if (viewGroup instanceof RelativeLayout) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);

            if (position == SnackbarPosition.TOP)
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            else if(position == SnackbarPosition.BOTTOM)
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            else if(position == SnackbarPosition.PARENT_CENTER)
                params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

            return params;
        } 

 

若是viewgGroup的類型不是RelativeLayout,也能夠不修改createMarginLayoutParams方法中的邏輯。函數

 

由於,提示條在中間顯示默認位移的動畫效果有些突兀,這裏咱們修改下居中顯示時的動畫效果,修改成透明度動畫。動畫

修改兩個獲取動畫資源函數:this

/**
     * @param snackbarPosition
     * @return the animation resource used by this {@link com.nispok.snackbar.Snackbar} instance
     * to enter the view
     */
    @AnimRes
    public static int getInAnimationResource(SnackbarPosition snackbarPosition) {
        return snackbarPosition == SnackbarPosition.TOP ? R.anim.sb__top_in  :
                (snackbarPosition == SnackbarPosition.PARENT_CENTER ? R.anim.sb__center_in : R.anim.sb__bottom_in);
    }

    /**
     * @param snackbarPosition
     * @return the animation resource used by this {@link com.nispok.snackbar.Snackbar} instance
     * to exit the view
     */
    @AnimRes
    public static int getOutAnimationResource(SnackbarPosition snackbarPosition) {
        return snackbarPosition == SnackbarPosition.TOP ? R.anim.sb__top_out :
                (snackbarPosition == SnackbarPosition.PARENT_CENTER ? R.anim.sb__center_out : R.anim.sb__bottom_out);
    }

anim文件夾下增長sb__center_in sb__center_out兩個配置文件以下:spa

sb__center_in.xmlcode

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromAlpha="0"
        android:interpolator="@interpolator/sb__decelerate_cubic"
        android:toAlpha="1" />
</set>

sb__center_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromAlpha="1"
        android:interpolator="@interpolator/sb__decelerate_cubic"
        android:toAlpha="0" />
</set>

 

在sample的示例代碼SnackSampleActivity中單行顯示文本的Snackbar的代碼修改以下:

singleLineButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SnackbarManager.show(
                        Snackbar.with(SnackbarSampleActivity.this)
                                .position(Snackbar.SnackbarPosition.PARENT_CENTER)
                                .margin(25)
                                .text("Center Single-line snackbar"));
            }
        });

 

最終顯示效果以下:

    

相關文章
相關標籤/搜索