黑马程序员技术交流社区
标题:
Android玉石短剑之GridView之精挑细选
[打印本页]
作者:
陈君
时间:
2014-9-19 19:29
标题:
Android玉石短剑之GridView之精挑细选
玉石短剑
奔到大殿,陈家洛捡起三柄玉剑,每人手中拿了一把,低声道:“玉器可以辟邪。”这时脚步声已到殿外。。。阿里和其余勇士捉到了一个桑拉巴的手下,迫着他带路,攻进了神峰。在大殿上,他们的刀剑都被磁山收了去,桑拉巴的武士拿玉刀玉剑来杀他们。然而阿里和他的勇士学会了本事,虽然空手,仍是一个个的和他们一起战死。
今天我们学习如何利用Android平台“玉石短剑”
GridView
来实现“九宫格”的图片效果,并提供对
GridView
中各张图片的单选和复选功能。采用“九宫格”展示图像的情景很常见,像电子相册、写真集、主屏桌面、功能菜单项等。下面给出该情景的案例:
一、案例技术要点
1.创建两个
GridView
控件分别用于展示支持单选和复选功能的“九宫格”图片。
2.为这两个
GridView
控件提供图片资源适配器ImageAdapter,使用isMultiSelect变量标示是单选还是复选模式
3.为
GridView
中各项设置监听OnItemClickListener,用于实时记录各张图片的选择状态。另外,单选状态需要记录上一次选中的图片位置。
// 记录各张图片的Id
private ArrayList<Integer> imageIds = new ArrayList<Integer>();
// 记录各张图片的选择状态
private HashMap<Integer, Boolean> imageStatues = new HashMap<Integer, Boolean>();
复制代码
4.利用LayerDrawable类构建叠加图层,将标示勾选与否的图片置于显示各张图片的视图控件的左上方。
Drawable[] drawArr = new Drawable[2];
drawArr[0] = new BitmapDrawable(imageBmp);
drawArr[1] = new BitmapDrawable(selectedBmp);
LayerDrawable <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a> = new LayerDrawable(drawArr);
<a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(0, 0, 0, 0, 0);
<a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(1, 0, 0, 60, 60);
复制代码
二、案例代码陈列
工程包目录AndroidManifest.xml
<manifest xmlns:<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>="http://schemas.<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.com/apk/res/<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>"
package="cn.lynn.gridview"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:versionCode="1"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:versionName="1.0" >
<uses-sdk
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:minSdkVersion="8"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:targetSdkVersion="15" />
<application
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:icon="@drawable/ic_launcher"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:label="@string/app_name" >
<activity
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:name=".<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>MainActivity"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:label="@string/app_name" >
<intent-filter>
<action <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:name="<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.intent.action.MAIN" />
<category <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:name="<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
复制代码
strings.xml
<resources>
<string name="app_name">Android单复选九宫格<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a></string>
</resources>
复制代码
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>="http://schemas.<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.com/apk/res/<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="match_parent"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="match_parent"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:orientation="vertical"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center" >
<<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:id="@+id/radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="210dp"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="210dp"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:numColumns="3" />
<<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:id="@+id/check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="210dp"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="210dp"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center"
<a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:numColumns="3" />
</LinearLayout>
复制代码
ImageAdapter.java
package cn.lynn.gridview;
import java.util.ArrayList;
import java.util.HashMap;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.content.Context;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.Bitmap;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.BitmapFactory;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.BitmapDrawable;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.Drawable;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.LayerDrawable;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.View;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.ViewGroup;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.BaseAdapter;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context context;
// 记录各张图片的Id
private ArrayList<Integer> imageIds = new ArrayList<Integer>();
// 记录各张图片的选择状态
private HashMap<Integer, Boolean> imageStatues = new HashMap<Integer, Boolean>();
// 记录上一次选中的图片位置,-1表示未选中任何图片
private int lastPosition = -1;
// 表示当前适配器是否允许多选
private boolean isMultiSelect;
// 声明图片资源数组
private int[] imageResIds = { R.drawable.item1, R.drawable.item2,
R.drawable.item3, R.drawable.item4, R.drawable.item5,
R.drawable.item6, R.drawable.item7, R.drawable.item8,
R.drawable.item9 };
public ImageAdapter(Context context, boolean isMultiSelect) {
this.context = context;
this.isMultiSelect = isMultiSelect;
// 采集图片资源
for (int i = 0; i < imageResIds.length; i++) {
imageIds.add(imageResIds[i]);
}
for (int i = 0; i < imageIds.size(); i++) {
// 初始化各图片状态均为未选中
imageStatues.put(i, false);
}
}
@Override
public int getCount() {
return imageIds.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.LayoutParams(50, 50));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(makeBmp(imageIds.get(position), imageStatues.get(position)));
return imageView;
}
private LayerDrawable makeBmp(int id, boolean isSelect) {
Bitmap imageBmp = ((BitmapDrawable) context.getResources().getDrawable(id)).getBitmap();
// 根据isSelect来选取勾选的图片
Bitmap selectedBmp = null;
if (isSelect) {
selectedBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.checkbox_pressed);
} else {
selectedBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.checkbox_normal);
}
// 生成叠加图
Drawable[] drawArr = new Drawable[2];
drawArr[0] = new BitmapDrawable(imageBmp);
drawArr[1] = new BitmapDrawable(selectedBmp);
LayerDrawable <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a> = new LayerDrawable(drawArr);
<a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(0, 0, 0, 0, 0);
<a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(1, 0, 0, 60, 60);
return <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>;
}
// 修改选中的状态
public void changeState(int position) {
if (isMultiSelect) { // 多选
// 直接取反
imageStatues.put(position, !imageStatues.get(position));
} else { // 单选
if (lastPosition != -1 && lastPosition != position) {
// 取消上一次的选中状态
imageStatues.put(lastPosition, false);
}
// 直接取反
imageStatues.put(position, !imageStatues.get(position));
lastPosition = position; // 记录本次选中的位置
}
notifyDataSetChanged(); // 通知适配器进行更新
}
}
复制代码
GridView
MainActivity.xml
package cn.lynn.gridview;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.app.Activity;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.os.Bundle;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.View;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.AdapterView;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.AdapterView.OnItemClickListener;
import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>;
public class <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>MainActivity extends Activity {
// 单选网格视图
private <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a> radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>;
// 多选网格视图
private <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a> check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>;
// 用于单选网格视图的内容适配器
private ImageAdapter radioImageAdapter;
// 用于多选网格视图的内容适配器
private ImageAdapter checkImageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a> = (<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>) this.findViewById(R.id.radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>);
radioImageAdapter = new ImageAdapter(this, false);
radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setAdapter(radioImageAdapter);
// 设置点击图片事件监听
radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
radioImageAdapter.changeState(position);
}
});
check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a> = (<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>) findViewById(R.id.check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>);
checkImageAdapter = new ImageAdapter(this, true);
check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setAdapter(checkImageAdapter);
// 设置点击图片事件监听
check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
checkImageAdapter.changeState(position);
}
});
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2