PopupWindow 动画

本文详细解析了Android PopupWindow的使用方法及动画配置。通过源码注释和示例代码,展示了如何创建并展示PopupWindow,并配置其动画效果。包括PopupWindow的工作原理、关键方法解释、示例代码演示以及动画资源的配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.PopupWindow 简介

首先看android.widget.PopupWindow.java源码注释:

/**
 * <p>A popup window that can be used to display an arbitrary view. The popup
 * window is a floating container that appears on top of the current
 * activity.</p>
 * 
 * @see android.widget.AutoCompleteTextView
 * @see android.widget.Spinner
 */
PopupWindow是一个容器,用来存放任意view然后显示在当前activity的顶部。

再看PopupWindow是怎么显示的,调用显示的方法是

  public void showAtLocation(IBinder token, int gravity, int x, int y) {
        if (isShowing() || mContentView == null) {
            return;
        }

        unregisterForScrollChanged();

        mIsShowing = true;
        mIsDropdown = false;

        WindowManager.LayoutParams p = createPopupLayout(token);
        p.windowAnimations = computeAnimationResource();//计算当前赋值的窗口动画资源
       
        preparePopup(p);
        if (gravity == Gravity.NO_GRAVITY) {
            gravity = Gravity.TOP | Gravity.START;
        }
        p.gravity = gravity;
        p.x = x;
        p.y = y;
        if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
        if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
        invokePopup(p);
    }
在上面方法里面,WindowManager会通过方法computeAnimationResource()初始化PopupWindow所需要的动画资源:

 private int computeAnimationResource() {
        if (mAnimationStyle == -1) {
            if (mIsDropdown) {
                return mAboveAnchor
                        ? com.android.internal.R.style.Animation_DropDownUp
                        : com.android.internal.R.style.Animation_DropDownDown;//系统默认的动画资源
            }
            return 0;
        }
        return mAnimationStyle;
    }

由此可以看到PopupWindow是通过values/style.xml来配置动画效果的。这点和View是不同的,View的动画资源直接在values/anim/下配置。

PopupWindow本质上是通过WindowManager创建的一个window,然后在window里面放入了所需要显示的view,如果需要配置窗口级别的动画可以采用类似方式。

为此,PopupWindow提供了公开的设置动画资源的方法setAnimationStyle。


2.PopupWindow示例代码

附上示例:

public class PopupWindowAnimationActivity extends Activity implements OnClickListener{

	ListPopupWindow mListPopupwindow;
	Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_popupwindow_animation);
		btn = (Button) this.findViewById(R.id.showPopupwindow);
		btn.setOnClickListener(this);
		mListPopupwindow = new ListPopupWindow(this);
		mListPopupwindow.setAnimationStyle(R.style.PopupAnimation);
		mListPopupwindow.setAnchorView(btn);
		String [] str = new String[]{"test1","test2"};
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);
		adapter.addAll(str);
		mListPopupwindow.setAdapter(adapter);
		mListPopupwindow.setWidth(300);
		mListPopupwindow.setHeight(LayoutParams.WRAP_CONTENT);
		mListPopupwindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.more_menu_popup_bg));
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int id = arg0.getId();
		if(id == R.id.showPopupwindow){
			mListPopupwindow.show();
		}
	}
<style name="PopupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/ani_in</item>
        <item name="android:windowExitAnimation">@anim/ani_out</item>
    </style>


3.示例下载

点击打开链接 https://github.com/zhhp1121/AndroidAnimationTeam.git





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值