Android GridView中选中图片时变大,而当焦点离开该图片后缩小

本文介绍如何使用Android中的GridView实现带有放大效果的导航界面。通过自定义Adapter并重写关键方法,实现了Item选中时放大、取消选中时缩小的效果。

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

http://www.apkbus.com/forum.php?mod=viewthread&tid=69669

用Android实现的浏览器导航界面,如下图所示:


如上图所示的众多图片是用GridView实现的。而GridView使用的时候一般都要setAdapter(...)

GrideView.setAdapter(BaseAdatper) 

一般都通过继承BaseAdapter来实现自己的Adapter。有一些必须实现的方法:

/ How many items are in the data set represented by this Adapter. 
public int getCount() { 
} 
// Get the data item associated with the specified position in the data set. 
public Object getItem(int position) { 
} 
// Get the row id associated with the specified position in the list. 
public long getItemId(int position) { 
} 
// Get a View that displays the data at the specified position in the data set. 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
}

为了使GridView里面的Item被选中的时候能够放大,而离开的时候又变回原来的大小,我也是煞费苦心,找了很多资料,最后在
http://my.oschina.net/u/126188/blog/28990。代码就是在adapter中加入

private int selected = -1; 
public void notifyDataSetChanged(int id) 
{ 
selected = id; 
super.notifyDataSetChanged(); 
}

并在 getView中进行判断:

public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
MyImageButton imgBtn; 
imgBtn = new MyImageButton(this.context, photoLink[position]); 
if(selected == position) { 
// the special one.Scale Large 
imgBtn.setScaleType(ScaleType.CENTER_CROP); 
} else { 
// the rest.Scale small 
imgBtn.setScaleType(ScaleType.CENTER_INSIDE); 
} 
return (View)imgBtn; 
}

当我们的GridView设置了Adapter的时候,我们就可以设置OnItemSelectedListener了

zonesView.setOnItemSelectedListener(new OnItemSelectedListener() { 
public void onItemSelected(AdapterView<?> arg0, View arg1, 
int arg2, long arg3) { 
Log.d("zonesView", "onItemSelected"); 
ImageAdapter zonesImageAdapter = (ImageAdapter) zonesView.getAdapter(); 
zonesImageAdapter.notifyDataSetChanged(arg2); 
} 
public void onNothingSelected(AdapterView<?> arg0) { 
// TODO Auto-generated method stub 
} 
});

这样一来,当GrideView的Item被选中的时候就会放大,而其他未被选中的就会变小

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值