自定義popwindow,從底部彈出和消失動畫

1.使用方法

popWindow = new CustomPopWindow(this, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        }
                    }
                });
                popWindow.showAtLocation(this.findViewById(R.id.etActivityEditFeedBack),Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //設置layout在PopupWindow中顯示的位置

2.自定義popwindow

public class CustomPopWindow extends PopupWindow implements View.OnClickListener {

    private TextView OK;
    private TextView GiveUp;
    private TextView Cancel;
    private View mMenuView;

    public CustomPopWindow(Activity context, View.OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.custom_popwindow, null);
        mMenuView.setOnClickListener(this);
        OK = (TextView) mMenuView.findViewById(R.id.tvPopwindowOK);
        GiveUp = (TextView) mMenuView.findViewById(R.id.tvPopwindowGiveUp);
        Cancel = (TextView) mMenuView.findViewById(R.id.tvPopwindowCancel);
        Cancel.setOnClickListener(this);
        OK.setOnClickListener(itemsOnClick);
        GiveUp.setOnClickListener(itemsOnClick);

        //設置SelectPicPopupWindowView
        setContentView(mMenuView);
        //設置SelectPicPopupWindow彈出窗體的寬
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//        //設置SelectPicPopupWindow彈出窗體的高
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        setBackgroundDrawable(dw);
        setFocusable(true);
        setAnimationStyle(R.style.popwindow_anim_style);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }
}

3.在style中設置動畫樣式

  <!--  Popwindow樣式 -->
    <style name="popwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/popwindow_show_anim</item>
        <item name="android:windowExitAnimation">@anim/popwindow_hidden_anim</item>
    </style>

4.popwindow佈局custom_popwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#90000000"
    android:gravity="bottom|center_horizontal"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tvPopwindowOK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_titlebar"
        android:gravity="center"
        android:padding="@dimen/padding_10"
        android:text="完成編輯"
        android:textSize="@dimen/text_size_18" />

    <TextView
        android:id="@+id/tvPopwindowGiveUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_titlebar"
        android:gravity="center"
        android:padding="@dimen/padding_10"
        android:text="放棄編輯"
        android:textColor="@color/color_red"
        android:textSize="@dimen/text_size_18" />


    <TextView
        android:id="@+id/tvPopwindowCancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/padding_10"
        android:layout_marginTop="@dimen/padding_10"
        android:background="@drawable/background_titlebar"
        android:gravity="center"
        android:padding="10dp"
        android:text="取消"
        android:textSize="@dimen/text_size_18" />
</LinearLayout>

5.anim 動畫樣式

popwindow_show_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
</set>

popwindow_hidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="100%p" />
</set>

這裏寫圖片描述

發佈了27 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章