android PopupWindow底部弹出gridview菜单
2016-09-21 11:20
561 查看
本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体如下:
1、新建anim文件夹,添加动画效果
pophidden_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
popshow_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
2、在styles.xml中以下样式
<!-- 这个是popupwindow的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style>
第一步:设计弹出窗口
popwindow_choose_icon.xml
第二步:创建SelectPicPopupWindow类继承PopupWindow:
第三步:编写MainActivity类实现测试:
第四:运行效果如下:
1、新建anim文件夹,添加动画效果
pophidden_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
popshow_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
2、在styles.xml中以下样式
<!-- 这个是popupwindow的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style>
第一步:设计弹出窗口
popwindow_choose_icon.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <!-- 标题 --> <LinearLayout android:id="@+id/pop_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/blue" /> <TextView android:layout_width="match_parent" android:layout_height="35dp" android:gravity="center" android:text="@string/popwindow_icon_title" android:textColor="@color/blue" /> </LinearLayout> <!-- 图标列表 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <GridView android:id="@+id/image_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp"> </GridView> </LinearLayout> </LinearLayout>
第二步:创建SelectPicPopupWindow类继承PopupWindow:
package com.fro.iotuniversal.base; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.GridView; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import com.fro.iotuniversal.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* * @Title: SelectPicPopupWindow * @Copyright: GuangZhou F.R.O Electronic Technology Co.,Ltd. Copyright 2006-2016, All rights reserved * @Description: ${TODO}<图片选择悬浮框> * @author: Jorble * @data: 2016/9/20 10:51 * @version: V1.0 */ public class SelectPicPopupWindow extends PopupWindow { private GridView image_gv; private View mMenuView; private List<Map<String, Object>> data_list; private SimpleAdapter sim_adapter; // 图片封装为一个数组 private int[] icon ={R.mipmap.system_home_icon,R.mipmap.system_traffic_icon}; private String[] iconName = { "001", "002"}; public SelectPicPopupWindow(Activity context, AdapterView.OnItemClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //利用布局生成view mMenuView = inflater.inflate(R.layout.popwindow_choose_icon, null); image_gv = (GridView) mMenuView.findViewById(R.id.image_gv); //新建List data_list = new ArrayList<Map<String, Object>>(); //获取数据 getData(); //新建适配器 String [] from ={"image","text"}; int [] to = {R.id.image,R.id.text}; sim_adapter = new SimpleAdapter(context, data_list, R.layout.item_pic_gv, from, to); //配置适配器 image_gv.setAdapter(sim_adapter); //设置按钮监听 image_gv.setOnItemClickListener(itemsOnClick); //设置SelectPicPopupWindow的View this.setContentView(mMenuView); //设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(WindowManager.LayoutParams.MATCH_PARENT); //设置SelectPicPopupWindow弹出窗体的高 this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); //设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); //设置SelectPicPopupWindow弹出窗体动画效果 this.setAnimationStyle(R.style.mypopwindow_anim_style); //实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); //设置SelectPicPopupWindow弹出窗体的背景 this.setBackgroundDrawable(dw); //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框 mMenuView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); } /** * 获取组装数据map * @return */ public List<Map<String, Object>> getData(){ //cion和iconName的长度是相同的,这里任选其一都可以 for(int i=0;i<icon.length;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("image", icon[i]); map.put("text", iconName[i]); data_list.add(map); } return data_list; } /** * 返回图标数组 * @return */ public int[] getIcon() { return icon; } }
第三步:编写MainActivity类实现测试:
/** * 显示图标选择悬浮框 */ private void showPicWindow() { //实例化SelectPicPopupWindow picWindow = new SelectPicPopupWindow(this, picItemsOnClick); //显示窗口,设置layout在PopupWindow中显示的位置 picWindow.showAtLocation(this.findViewById(R.id.title_tv), Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL, 0, 0); } //为弹出窗口实现监听类 private AdapterView.OnItemClickListener picItemsOnClick =new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //隐藏悬浮框 picWindow.dismiss(); //设置相应图片 systemIconIv.setImageResource(picWindow.getIcon()[position]); //保存图片名称 iconName=ReflectUtil.getResourceName(getContext(),picWindow.getIcon()[position]); } };
第四:运行效果如下:
相关文章推荐
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- Android PopupWindow实现右侧、左侧和底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- PopupWindow实现右侧、左侧和底部弹出菜单
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- ios实现底部PopupWindow的示例代码(底部弹出菜单)
- Android之PopupWindow类似微信右上角的弹出菜单
- Android PopupWindow底部菜单
- Android PopUpWindow的底部弹出和消失
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- Android之PopupWindow类似微信右上角的弹出菜单
- android 仿微信底部弹出菜单
- 【Android UI设计与开发】第09期:底部菜单栏(四)Fragment+PopupWindow仿QQ空间最新版底部菜单栏
- Android PopupWindow介绍及实现菜单效果