Android popupwindow的基本使用

Android popupwindow在指定控件正下方滑动弹出效果

1.PopupWindow的布局(record_popupwindow.xml)


	<?xml version="1.0" encoding="utf-8"?>
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="match_parent"
    	android:layout_height="match_parent"
    	android:background="@color/transparent_black">
        <ImageView
            android:id="@+id/iv_cancel"
            android:layout_width="@dimen/dp_33"
            android:layout_height="@dimen/dp_33"
            android:background="@mipmap/img_cancel"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/dp_95"
            android:layout_marginRight="@dimen/dp_47"
            />
        <RelativeLayout
            android:id="@+id/rl_popupwindow"
            android:layout_width="@dimen/dp_282"
            android:layout_height="@dimen/dp_349"
            android:layout_centerInParent="true"
            android:background="@color/colorPrimary">
            <ImageView
                android:id="@+id/iv_recommend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/recommend_text"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/dp_100"
                />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_record_recommend"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/iv_recommend"
                android:layout_marginTop="@dimen/dp_15"
                >

            </android.support.v7.widget.RecyclerView>
        </RelativeLayout>
        <ImageView
            android:id="@+id/iv_record_recommend_offline"
            android:layout_width="@dimen/dp_110"
            android:layout_height="@dimen/dp_125"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_95"
            android:background="@mipmap/offline_logo"/>
        <Button
            android:layout_width="@dimen/dp_168"
            android:layout_height="@dimen/dp_32"
            android:layout_below="@+id/rl_record_popupwindow"
            android:background="@drawable/bg_pic_saved"
            android:layout_centerHorizontal="true"
            android:text="查看产品"
            android:textColor="@color/white"
            android:textSize="@dimen/dp_15"
            android:layout_marginTop="@dimen/dp_20"
            />
</RelativeLayout>

2.BaseApplication.class


	public class BaseApplication extends Application {
		//static代码可以防止内存泄漏
		static Context mContext;
    	static Handler sHandler;
		@Override
    	public void onCreate() {
        	super.onCreate();
        	mContext = getApplicationContext();
        	sHandler = new Handler();
		}
		public static Context getContext() {
        	return mContext;
    	}

    	public static Handler getHandler() {
        	return sHandler;
    	}
	}

3.通过子线程更新PopupWindow


	 BaseApplication.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showPopupWindow();
            }
        },2000);

4.PopupWindow在Activity中显示


	public void showPopupWindow(){
        PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.record_popupwindow, null));
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.showAtLocation(LoanManagerActivity.this.mLayout, Gravity.CENTER,0,0);
    }
popupwindow 的动画效果

PopupWindow弹出动画设置

1.anim_popupwindow_in.xml

	
	<?xml version="1.0" encoding="utf-8"?>
	<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/overshoot_interpolator"
    android:duration="1000"
    >
    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0"
        />
</set>

2.anim_popwindow_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

duration:即动画执行时长
scale:缩放动画
fromXScale:横向开始比例
fromYScale:纵向开始比例
pivotX:缩放的参照物横轴 一般为50%,即是横向的中间点
pivotY:缩放的参照物纵轴 一半为50%,即是纵向的中间点
toXScale:动画结束时横向的比例 float值 1即是保持正常大小
toYScale:动画结束时纵向的比例

2.定义动画style

	<style name="pop_animation" parent="android:Animation">
    	<item name="android:windowEnterAnimation">@anim/anim_popwindow_in</item>
    	<item name="android:windowExitAnimation">@anim/anim_popupwindow_out</item>
	</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值