activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rl_root" tools:context="com.example.popupwindowvslistview.MainActivity" > <RelativeLayout android:id="@+id/rl_common_default" android:layout_width="fill_parent" android:layout_height="48dp" android:background="@drawable/top_bg" > <ImageView android:id="@+id/iv_common_more" android:layout_width="34dp" android:layout_height="34dp" android:padding="5dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:src="@drawable/top_more_n" android:visibility="visible" /> </RelativeLayout> </RelativeLayout>
MainActivity
package com.example.popupwindowvslistview; import com.example.popupwindowvslistview.CommonPopuWindow.AnimStyle; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity { private RelativeLayout rl_common_default; private ImageView iv_common_more; private CommonPopuWindow mSmsMorePopup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); initPopup(); iv_common_more.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mSmsMorePopup.isShowing()) { mSmsMorePopup.dismiss();// 关闭 } mSmsMorePopup.show(AnimStyle.RIGHTANIM); } }); } private void initPopup() { mSmsMorePopup = new CommonPopuWindow(this, R.style.animation, new CommonPopuWindow.ItemClickCallBack() { @Override public void callBack(int position) { switch (position) { case 0: Toast.makeText(MainActivity.this, "Toast测试弹出清空记录", Toast.LENGTH_SHORT).show(); mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; case 1: Toast.makeText(MainActivity.this, "Toast测试弹出备份", Toast.LENGTH_SHORT).show(); mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; case 2: Toast.makeText(MainActivity.this, "Toast测试弹出导入", Toast.LENGTH_SHORT).show(); mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; case 3: Toast.makeText(MainActivity.this, "Toast测试弹出充值支付", Toast.LENGTH_SHORT).show(); mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; case 4: mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; case 5: mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM); break; } } }, null); mSmsMorePopup.initData(R.array.messagePopuWindowmore); } private void initView() { rl_common_default = (RelativeLayout) findViewById(R.id.rl_common_default); iv_common_more = (ImageView) findViewById(R.id.iv_common_more); } }CommonPopuWindow
package com.example.popupwindowvslistview; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.ScaleAnimation; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.PopupWindow.OnDismissListener; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams; public class CommonPopuWindow extends PopupWindow implements AnimationListener, OnDismissListener { private Activity mActivity; private View rootView; private ListView mListView; private Resources mResources; private DataAdapter mAdapter; public enum AnimStyle { LEFTANIM, RIGHTANIM } private ScaleAnimation leftShowAnim, rightShowAnim, leftExitAnim, rightExitAnim; private ItemClickCallBack mCallBack; private int animStyle; public CommonPopuWindow(Activity activity, int animStyle, ItemClickCallBack callBack, String lvWidthTag) { this.mActivity = activity; this.mResources = activity.getResources(); this.mCallBack = callBack; this.animStyle = animStyle; init(lvWidthTag); } @SuppressLint("InflateParams") @SuppressWarnings("deprecation") private void init(String lvWidthTag) { this.rootView = LayoutInflater.from(mActivity).inflate( R.layout.popupwindow_layout, null); this.mListView = (ListView) rootView.findViewById(R.id.lv_popup_list); this.setContentView(rootView); this.setWidth(LayoutParams.WRAP_CONTENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setFocusable(true); this.setOnDismissListener(this); this.setBackgroundDrawable(new BitmapDrawable()); this.setAnimationStyle(animStyle); this.setOutsideTouchable(true); this.mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mCallBack != null) { mCallBack.callBack(position); } } }); } /** * 获取数据 */ public void initData(int stringArrayId) { String[] arrays = mResources.getStringArray(stringArrayId); mAdapter = new DataAdapter(arrays); mListView.setAdapter(mAdapter); } /** * 显示 */ public void show(View anchor, int xoff, int yoff, AnimStyle style) { this.showAsDropDown(anchor, xoff, yoff); popupShowAlpha(); showAnim(style); } public void show(AnimStyle style) { Rect frame = new Rect(); mActivity.getWindow().getDecorView() .getWindowVisibleDisplayFrame(frame); int mMorePopupMarginTop = frame.top + dp2Px(mActivity, 12); int mMorePopupMarginRight = dp2Px(mActivity, 16); popupShowAlpha(); this.showAtLocation(mActivity.findViewById(R.id.rl_root), Gravity.RIGHT | Gravity.TOP, mMorePopupMarginRight, mMorePopupMarginTop); showAnim(style); } /** * 显示动画效果 */ private void showAnim(AnimStyle style) { switch (style) { case LEFTANIM: if (leftShowAnim == null) { leftShowAnim = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); leftShowAnim.setDuration(250); leftShowAnim.setFillAfter(true); } rootView.startAnimation(leftShowAnim); break; case RIGHTANIM: if (rightShowAnim == null) { rightShowAnim = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); rightShowAnim.setDuration(250); rightShowAnim.setFillAfter(true); } rootView.startAnimation(rightShowAnim); break; } } /** * 退出动画效果 */ public void thisDismiss(AnimStyle style) { switch (style) { case LEFTANIM: if (leftExitAnim == null) { leftExitAnim = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); leftExitAnim.setDuration(250); leftExitAnim.setFillAfter(true); leftExitAnim.setAnimationListener(this); } rootView.startAnimation(leftExitAnim); break; case RIGHTANIM: if (rightExitAnim == null) { rightExitAnim = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); rightExitAnim.setDuration(250); rightExitAnim.setFillAfter(true); rightExitAnim.setAnimationListener(this); } rootView.startAnimation(rightExitAnim); break; } } @Override public void onAnimationEnd(Animation animation) { this.dismiss(); } private void popupShowAlpha() { Window window = ((Activity) mActivity).getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.alpha = 0.6f; window.setAttributes(params); } private void popupExitAlpha() { Window window = ((Activity) mActivity).getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.alpha = 1.0f; window.setAttributes(params); } private class DataAdapter extends BaseAdapter { private String[] arrays; class ViewHolder { TextView dataView; } public DataAdapter(String[] arrays) { this.arrays = arrays; } @Override public int getCount() { if (arrays != null) { return arrays.length; } return 0; } @Override public Object getItem(int position) { if (arrays != null) { return arrays[position]; } return null; } @Override public long getItemId(int position) { return position; } @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(mActivity).inflate( R.layout.list_item_popupwindow, null); viewHolder.dataView = (TextView) convertView .findViewById(R.id.tv_list_item); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.dataView.setText(arrays[position]); return convertView; } } public interface ItemClickCallBack { void callBack(int position); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onDismiss() { popupExitAlpha(); } private int dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/lv_popup_list" android:layout_width="110dp" android:layout_height="wrap_content" android:background="@drawable/layer_popup" android:divider="@drawable/helper_line" android:focusableInTouchMode="true" android:footerDividersEnabled="false" android:listSelector="@drawable/popupwindow_list_item_text_selector" android:paddingBottom="5dp" android:paddingTop="5dp" android:scrollbars="none" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_list_item" android:layout_width="fill_parent" android:layout_height="45dp" android:textSize="16dp" android:textColor="#111111" android:text="aaa" android:gravity="center" android:background="@drawable/popupwindow_list_item_text_selector" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Bottom 2dp Shadow --> <item> <shape android:shape="rectangle"> <solid android:color="#d9d9d9" /> <corners android:radius="5dp" /> </shape> </item> <!-- White Top color --> <item android:bottom="5px"> <shape android:shape="rectangle"> <solid android:color="#d9d9d9" /> <corners android:radius="5dp" /> </shape> </item> </layer-list> <!-- -->
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/more_popu_normal"/> <item android:drawable="@drawable/more_popu_pressed" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 位移效果 fromXDelta="0%p" 指的x轴是相对于父控件从父控件的x=0出开始位移 fromYDelta="0%p" 指的y轴是相对于父控件从父控件的y=0出开始位移 toXDelta="100%p" 指的x轴是相对于父控件到达父控件的x=100的位置即x轴在屏幕的终点 toYDelta="100%p" 指的y轴是相对于父控件到达父控件的y=100的位置即x轴在屏幕的终点 --> <scale android:duration="250" android:pivotX="100%" android:pivotY="0%" android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="messagePopuWindowmore"> <item >@string/message_clear</item> <item >@string/back_up</item> <item >@string/lead_into</item> <item >@string/pay</item> <item >@string/set</item> <item >@string/login_out</item> </string-array> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="more_popu_pressed">#d9d9d9</drawable> <drawable name="more_popu_normal">#ffffff</drawable> </resources>
<style name="animation"> <item name="android:windowExitAnimation">@anim/out</item> </style>
原文地址:http://blog.csdn.net/u013210620/article/details/46011945