目的: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