自定义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 android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
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.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private PopupWindow mSmsMorePopup;
	private View smsMorePopuLayout;
	List<Map<String, String>> smsMoreList;
	private ImageView iv_common_more;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		iv_common_more = (ImageView) findViewById(R.id.iv_common_more);
		iv_common_more.setOnClickListener(this);
		initSmsMorePopuData();
		initPopup();

		mSmsMorePopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				popupExitAlpha();
			}
		});
	}

	private void initSmsMorePopuData() {

		smsMoreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;

		map = new HashMap<String, String>();
		map.put("sms_more", "清空信息");
		smsMoreList.add(map);

		map = new HashMap<String, String>();
		map.put("sms_more", "备份");
		smsMoreList.add(map);

		map = new HashMap<String, String>();
		map.put("sms_more", "导入");
		smsMoreList.add(map);

		map = new HashMap<String, String>();
		map.put("sms_more", "充值支付");
		smsMoreList.add(map);

		map = new HashMap<String, String>();
		map.put("sms_more", "设置");
		smsMoreList.add(map);

		map = new HashMap<String, String>();
		map.put("sms_more", "退出登录");
		smsMoreList.add(map);

	}

	private void initPopup() {

		smsMorePopuLayout = View.inflate(this,
				R.layout.popupwindow_layout_right, null);
		ListView mMorePopupList = (ListView) smsMorePopuLayout
				.findViewById(R.id.lv_popup_list);
		mSmsMorePopup = new PopupWindow(smsMorePopuLayout);
		mSmsMorePopup.setWidth(LayoutParams.WRAP_CONTENT);
		mSmsMorePopup.setHeight(LayoutParams.WRAP_CONTENT);
		mMorePopupList.setAdapter(new SimpleAdapter(MainActivity.this,
				smsMoreList, R.layout.list_item_popupwindow,
				new String[] { "sms_more" }, new int[] { R.id.tv_list_item }));
		mMorePopupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					// mDialController.dialMenu(DialController.CLEAR);
					Toast.makeText(MainActivity.this, "Toast测试弹出清空记录", 0)
							.show();

					morePopuExit();

					break;
				case 1:
					Toast.makeText(MainActivity.this, "Toast测试弹出备份", 0).show();
					morePopuExit();
					break;
				case 2:
					Toast.makeText(MainActivity.this, "Toast测试弹出导入", 0).show();
					morePopuExit();
					break;
				case 3:
					Toast.makeText(MainActivity.this, "Toast测试弹出充值支付", 0)
							.show();
					morePopuExit();
					break;
				case 4:
					Toast.makeText(MainActivity.this, "Toast测试弹出设置", 0).show();
					morePopuExit();
					break;
				case 5:
					morePopuExit();
					break;

				default:
					break;
				}
			}

			private void morePopuExit() {
				ScaleAnimation animation = new ScaleAnimation(1f, 0f, 1f, 0f,
						Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0.0f);
				animation.setDuration(250);
				animation.setFillAfter(true);
				smsMorePopuLayout.startAnimation(animation);
				popupExitAlpha();
				animation.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {

					}

					@Override
					public void onAnimationRepeat(Animation animation) {

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						mSmsMorePopup.dismiss();// 关闭
					}
				});
			}

		});

		mSmsMorePopup.setFocusable(true);
		mSmsMorePopup.setBackgroundDrawable(new BitmapDrawable());
		mSmsMorePopup.setAnimationStyle(R.style.animation);
		mSmsMorePopup.setOutsideTouchable(true);

	}

	private void popupShowAlpha() {
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.alpha = 0.6f;
		getWindow().setAttributes(params);
	}

	private void popupExitAlpha() {
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.alpha = 1.0f;
		getWindow().setAttributes(params);
	}

	public int Dp2Px(Context context, float dp) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}

	private void onMesureLocation(View view, PopupWindow popup) {
		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

		int mMorePopupMarginTop = frame.top + Dp2Px(MainActivity.this, 12);
		int mMorePopupMarginRight = Dp2Px(MainActivity.this, 16);
		popupShowAlpha();
		popup.showAtLocation(findViewById(R.id.rl_root), Gravity.RIGHT
				| Gravity.TOP, mMorePopupMarginRight, mMorePopupMarginTop);

		ScaleAnimation animation = new ScaleAnimation(0f, 1f, 0f, 1f,
				Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
				0.0f);
		animation.setDuration(250);
		animation.setFillAfter(true);
		view.startAnimation(animation);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_common_more:

			if (mSmsMorePopup.isShowing()) {

				mSmsMorePopup.dismiss();// 关闭
			}
			onMesureLocation(smsMorePopuLayout, mSmsMorePopup);

			break;

		default:
			break;
		}
	}
}

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>  

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:gravity="left"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_list_item"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:textSize="16dp"
        android:textColor="#111111"
        android:text="aaa"
        android:gravity="center"
        android:background="@drawable/popupwindow_list_item_text_selector"

        />

</LinearLayout>

popupwindow_layout_right.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:focusableInTouchMode="true"
        android:divider="@null"
        android:listSelector="@drawable/popupwindow_list_item_text_selector"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:scrollbars="none" />

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

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>  
  <style name="animation">
        <item name="android:windowExitAnimation">@anim/out</item>
    </style>

时间: 2024-11-05 04:48:14

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

自定义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"

自定义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开发最常用例子整理----(2)自定义ListView(SimpleAdapter实现)

android开发最常用例子整理----(2)自定义ListView(SimpleAdapter实现) 一.Activity MainActivity.java源码: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layou

自定义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弹出后背景灰色状态

最近有做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;

自定义PopupWindow动画效果

Java代码   public class RollActivity extends Activity { private View view; private Button btn; private PopupWindow mPopupWindow; private View[] btns; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceS