grideview与PopupWindow的结合

本文介绍如何在Android应用中使用GridView展示一系列图片,并实现点击图片时弹出带有动画效果的PopupWindow。通过自定义适配器及监听器,结合PopupWindow和动画组件,实现了美观的用户交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的:grideview 展示一系列图片,当点击任意一个grideview中的view时都会弹出一个带有动画效果的PopupWindow

activety

public class grideViewfun extends Activity{
private GridView gv;
private PopupWindow mPopupWindow;
private ImageView popestyle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gridetest);


// 准备要添加的数据条目
List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 9; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("imageItem", R.drawable.icon);// 添加图像资源的ID
item.put("textItem", "cute" + i);// 按序号添加ItemText
items.add(item);
}


// 实例化一个适配器
SimpleAdapter adapter = new SimpleAdapter(this, items,
R.layout.grideviewtest,
new String[] { "imageItem", "textItem" }, new int[] {
R.id.image_item, R.id.text_item });


// 获得GridView实例
gv = (GridView) findViewById(R.id.mygridview);
// 为GridView设置适配器
gv.setAdapter(adapter);
        gv.setOnItemClickListener(GrideitemListener);
        DisplayMetrics dm = new DisplayMetrics();  
        dm = getResources().getDisplayMetrics();  
        if(dm.heightPixels== 1280 && dm.widthPixels == 800){
         gv.setNumColumns(4);
        }
}
OnItemClickListener GrideitemListener = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
getPopupWindowInstance();
mPopupWindow.showAtLocation(arg1, Gravity.CENTER, 0, 0);   //弹出动画效果
}
};
/*
* 获取PopupWindow实例
*/
private void getPopupWindowInstance() {
if (null != mPopupWindow && mPopupWindow.isShowing()) {

//如果没有 && mPopupWindow.isShowing()这句的话,只有第一次点击view时会带有动画效果,其余再点击都不会有动画效果,因为除了第一次会走updateUI(); 之后都会走这个if语句,那么为了防止走if,就必须有这条判断
mPopupWindow.dismiss();
return;
} else {
updateUI(); 
}
}


private void updateUI() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popwindow_style, null);
popestyle=(ImageView)popupWindow.findViewById(R.id.poppic);
// 创建一个PopupWindow  
        // 参数1:contentView 指定PopupWindow的内容  
        // 参数2:width 指定PopupWindow的width  
        // 参数3:height 指定PopupWindow的height  
mPopupWindow = new PopupWindow(popupWindow, 400, 350);
mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(10));// 点击窗口外消失
mPopupWindow.setFocusable(true);
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);// 点击窗口外消失,需要设置背景、焦点、touchable、update
mPopupWindow.update();
//加载动画
Animation hyperspaceJumpAnimation =AnimationUtils.loadAnimation(this, R.anim.jump);
//使用ImageView显示动画
popestyle.startAnimation(hyperspaceJumpAnimation);
}
}

参考文档:http://blog.youkuaiyun.com/leichelle/article/details/7951290

http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值