码迷,mamicode.com
首页 > Windows程序 > 详细

自定义PopupWindow+SimpleAdapter+Anim

时间:2015-05-29 00:57:35      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:anim   popupwindow   animation android   

效果图见  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>







自定义PopupWindow+SimpleAdapter+Anim

标签:anim   popupwindow   animation android   

原文地址:http://blog.csdn.net/u013210620/article/details/46137903

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!