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
时间: 2024-10-12 23:40:01