1. 程式人生 > >分享從下向上彈出的popwindow

分享從下向上彈出的popwindow

popwindow的樣式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@color/white0"
    android:gravity="center"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/message_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/gray5"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/wchatfriends"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/shouye_image"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/wechats1" />

            <TextView
                android:id="@+id/shouye_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="4dp"
                android:text="微信朋友圈"
                android:textColor="#46464d"
                android:textSize="@dimen/tab_text" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/contacts_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/gray5"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/weichatfriend"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/zixun_image"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/wechats2" />

            <TextView
                android:id="@+id/zixun_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="4dp"
                android:text="微信好友"
                android:textColor="#46464d"
                android:textSize="@dimen/tab_text" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/news_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/gray5"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/sina"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/taixin_image"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/sinas" />

            <TextView
                android:id="@+id/taixin_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="4dp"
                android:text="新浪微博"
                android:textColor="#46464d"
                android:textSize="@dimen/tab_text" />
        </LinearLayout>
    </RelativeLayout>


</LinearLayout>

<TextView
    android:id="@+id/tv_cancelshare"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:text="取消分享"
    android:textColor="#46464d"
    android:gravity="center"
    android:background="@color/white0"
    android:textSize="14sp" />
</LinearLayout>

視窗顯示消失的動畫

res資料夾下的anim資料夾建立:

popshow_anim.xml

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

<alpha
    android:duration="200"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />
</set>

pophidden_a.im.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
    android:duration="200"
    android:fromYDelta="0"
    android:toYDelta="50%p" />

<alpha
    android:duration="200"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />
</set>

values資料夾下的styles資料夾新增:

 <style name="Animation.Popup" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>

<style name="AppPopBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppPopTheme" parent="AppPopBaseTheme">
</style>

<!--  這個是加入的程式碼 -->
<style name="mypopwindow_anim_style">
    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
    <!-- 指定顯示的動畫xml -->
    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
    <!-- 指定消失的動畫xml -->
</style>

activity中使用

 /**
 * 顯示popupWindow
 */
private void showPopwindow() {
    // 利用layoutInflater獲得View
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.popwindowlayout, null);

    // 下面是兩種方法得到寬度和高度 getWindow().getDecorView().getWidth()
    PopupWindow window = new PopupWindow(view,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    // 設定popWindow彈出窗體可點選,這句話必須新增,並且是true
    window.setFocusable(true);

    // 例項化一個ColorDrawable顏色為半透明
    ColorDrawable dw = new ColorDrawable(0x00000000);
    window.setBackgroundDrawable(dw);
    backgroundAlpha(this, 0.5f);

    // 設定popWindow的顯示和消失動畫
    window.setAnimationStyle(R.style.mypopwindow_anim_style);
    // 在底部顯示
    window.showAtLocation(WebForShareActivity.this.findViewById(R.id.share),
            Gravity.BOTTOM, 0, 0);

    TextView tvCancelShare = (TextView) view.findViewById(R.id.tv_cancelshare);

    tvCancelShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            window.dismiss();
            backgroundAlpha(WebForShareActivity.this, 1f);
        }
    });

    LinearLayout wchatFriends = (LinearLayout) view.findViewById(R.id.wchatfriends);
    wchatFriends.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ToastUtil.show(WebForShareActivity.this, "盆友圈");
        }
    });

    LinearLayout wchatFriend = (LinearLayout) view.findViewById(R.id.weichatfriend);
    wchatFriend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ToastUtil.show(WebForShareActivity.this, "盆友");
        }
    });

    LinearLayout sina = (LinearLayout) view.findViewById(R.id.sina);
    sina.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ToastUtil.show(WebForShareActivity.this, "新浪");
        }
    });

    //popWindow消失監聽方法
    window.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            backgroundAlpha(WebForShareActivity.this, 1f);
        }
    });
}

設定新增螢幕的背景透明度

 public void backgroundAlpha(Activity context, float bgAlpha) {
    WindowManager.LayoutParams lp = context.getWindow().getAttributes();
    lp.alpha = bgAlpha;
    context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    context.getWindow().setAttributes(lp);
}

效果