自定义PopupWindow+ListView+Anim

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);
	}
}

popupwindow_layout.xml

<?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>

list_item_popupwindow.xml

<?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>

layer_popup.xml

<?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>
<!-- 

 -->

popupwindow_list_item_text_selector.xml

<?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>

out.xml

<?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>

另外还有arrays.xml

<?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>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <drawable name="more_popu_pressed">#d9d9d9</drawable>
    <drawable name="more_popu_normal">#ffffff</drawable>
</resources>

styles.xml

    <style name="animation">
        <item name="android:windowExitAnimation">@anim/out</item>
</style>
时间: 2024-11-17 11:27:10

自定义PopupWindow+ListView+Anim的相关文章

自定义PopupWindow+SimpleAdapter+Anim

效果图见  http://blog.csdn.net/u013210620/article/details/46011945 MainActivity package com.example.mypopupwindow; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import andr

自定义PopupWindow实现3急地区联动

做项目时有时我们会需要3级联动,比如注册,买东西下单等,这里我在android上使用popupwindow实现3级联动功能,我实现的思路是,当程序启动时就将后台的地区JSON数据格式全部加载上来,通过SharedPreferences将获取到的数据保存,点击按钮获取SharedPreferences中的地区数据,再通过JSONObject转为List集合,具体实现如下: 布局文件: activity_main.xml: <LinearLayout xmlns:android="http:/

Android 自定义PopupWindow以及参数传递与返回

在这篇博客之前,还写了一篇关于PopupWindow,那篇主要是关于PopupWindow弹出位置的设置.以及选择PopupWindow布局后的监听.详情看Android popupwindow 示例程序一.接下来这篇主要是讲自定义PopupWindow以及参数传递与返回,我在里面写了一个listview来示例.接下来看代码,都有所注释. 本文项目资源下载: 一.MainActivity <span style="background-color: rgb(240, 240, 240);&

安卓PopupWindow+ListView实现登录账号选择下拉框

这段时间在做android开发,发现自定义下拉框有很多种方法实现,我介绍一种PopupWindow+ListView的方式,实现起来比较灵活.效果: 直接看核心代码: Java代码   //获取文本框 etLoginName = (EditText)findViewById(R.id.login_edit_account); //自定义ListView的Adapter adapter=new myAdapter(); listView=new ListView(TestPopupWindowAc

自定义PopupWindow弹出框(带有动画)

使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow 1 public class LostPopupWindow extends PopupWindow { 2 public Lost lost; 3 public void onLost(Lost lost){ 4 this.lost = lost; 5 } 6 private View conentView; 7 8 public View getConentView() { 9 return cone

分别用自定义PopupWindow和自定义Dialog实现下拉菜单

首先看下分别使用PopupWindow和Dialog实现的下拉菜单的不同之处: PopupWindow: Dialog: 由于之前用PopupWindow实现的效果不是太理想,并且弹出下拉菜单的时候背景透明度变化的也不是太好,后来改为Diaolog,项目中其他弹窗也都用的diaolog,便于更改背景透明度,整体看起来也比较统一. 下面把两种实现的方法都记录下来: **第一种:自定义PopupWindow** 首先自定义一个PopWindow: PopWindow.class: public cl

Android开发:自定义GridView/ListView数据源

http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮助我们实现我们想要的布局效果,并且在绑定大数据量时也不会感觉有卡壳现象.记得以前用一个ListView直接去绑定手机内的联系人Cursor(一百多号人),滑动的时候就会有卡的感觉.今天决定写个Demo是因为在项目中可能会要实现这样的一个效果:一个GridView中绑定4个ImageButton,有些

自定义PopupWindow弹出后背景灰色状态

最近有做fragment里弹出自定义popupWindow, fragment里面调用: // 点击加号按钮 @Click protected void ll_add_pharmacy() { mPopTempList.showAsDropDown(ll_add_pharmacy, 0, getActivity());  // 传给popupWindow getActivity(); mPopTempList.setOnClickListener(new OnOKClickListener()

自定义PopupWindow+xml布局+Anim

MainActivity.java package com.example.mybottompopupwindowdemo; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.view.ViewPager.LayoutParams; import android.view.Gravity;