黑马程序员技术交流社区

标题: Android玉石短剑之GridView之精挑细选 [打印本页]

作者: 陈君    时间: 2014-9-19 19:29
标题: Android玉石短剑之GridView之精挑细选
玉石短剑
奔到大殿,陈家洛捡起三柄玉剑,每人手中拿了一把,低声道:“玉器可以辟邪。”这时脚步声已到殿外。。。阿里和其余勇士捉到了一个桑拉巴的手下,迫着他带路,攻进了神峰。在大殿上,他们的刀剑都被磁山收了去,桑拉巴的武士拿玉刀玉剑来杀他们。然而阿里和他的勇士学会了本事,虽然空手,仍是一个个的和他们一起战死。
今天我们学习如何利用Android平台“玉石短剑”GridView来实现“九宫格”的图片效果,并提供对GridView中各张图片的单选和复选功能。采用“九宫格”展示图像的情景很常见,像电子相册、写真集、主屏桌面、功能菜单项等。下面给出该情景的案例:
一、案例技术要点
1.创建两个GridView控件分别用于展示支持单选和复选功能的“九宫格”图片。
2.为这两个GridView控件提供图片资源适配器ImageAdapter,使用isMultiSelect变量标示是单选还是复选模式
3.为GridView中各项设置监听OnItemClickListener,用于实时记录各张图片的选择状态。另外,单选状态需要记录上一次选中的图片位置。
  1. // 记录各张图片的Id
  2. private ArrayList<Integer> imageIds = new ArrayList<Integer>();
  3. // 记录各张图片的选择状态
  4. private HashMap<Integer, Boolean> imageStatues = new HashMap<Integer, Boolean>();
复制代码

4.利用LayerDrawable类构建叠加图层,将标示勾选与否的图片置于显示各张图片的视图控件的左上方。
  1. Drawable[] drawArr = new Drawable[2];
  2. drawArr[0] = new BitmapDrawable(imageBmp);
  3. drawArr[1] = new BitmapDrawable(selectedBmp);
  4. LayerDrawable <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a> = new LayerDrawable(drawArr);
  5. <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(0, 0, 0, 0, 0);
  6. <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(1, 0, 0, 60, 60);
复制代码

二、案例代码陈列
工程包目录AndroidManifest.xml
  1. <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>"
  2. package="cn.lynn.gridview"
  3. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:versionCode="1"
  4. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:versionName="1.0" >

  5. <uses-sdk
  6. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:minSdkVersion="8"
  7. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:targetSdkVersion="15" />

  8. <application
  9. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:icon="@drawable/ic_launcher"
  10. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:label="@string/app_name" >
  11. <activity
  12. <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"
  13. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:label="@string/app_name" >
  14. <intent-filter>
  15. <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" />

  16. <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" />
  17. </intent-filter>
  18. </activity>
  19. </application>

  20. </manifest>

复制代码

strings.xml
  1. <resources>

  2. <string name="app_name">Android单复选九宫格<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a></string>

  3. </resources>

复制代码

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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>"
  3. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="match_parent"
  4. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="match_parent"
  5. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:orientation="vertical"
  6. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center" >

  7. <<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>
  8. <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>"
  9. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="210dp"
  10. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="210dp"
  11. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center"
  12. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:numColumns="3" />

  13. <<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>
  14. <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>"
  15. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_width="210dp"
  16. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:layout_height="210dp"
  17. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:gravity="center"
  18. <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>:numColumns="3" />

  19. </LinearLayout>
复制代码

ImageAdapter.java
  1. package cn.lynn.gridview;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;

  4. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.content.Context;
  5. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.Bitmap;
  6. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.BitmapFactory;
  7. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.BitmapDrawable;
  8. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.Drawable;
  9. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.graphics.drawable.LayerDrawable;
  10. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.View;
  11. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.ViewGroup;
  12. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.BaseAdapter;
  13. 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>;
  14. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.ImageView;

  15. public class ImageAdapter extends BaseAdapter {

  16. private Context context;
  17. // 记录各张图片的Id
  18. private ArrayList<Integer> imageIds = new ArrayList<Integer>();
  19. // 记录各张图片的选择状态
  20. private HashMap<Integer, Boolean> imageStatues = new HashMap<Integer, Boolean>();

  21. // 记录上一次选中的图片位置,-1表示未选中任何图片
  22. private int lastPosition = -1;

  23. // 表示当前适配器是否允许多选
  24. private boolean isMultiSelect;

  25. // 声明图片资源数组
  26. private int[] imageResIds = { R.drawable.item1, R.drawable.item2,
  27. R.drawable.item3, R.drawable.item4, R.drawable.item5,
  28. R.drawable.item6, R.drawable.item7, R.drawable.item8,
  29. R.drawable.item9 };

  30. public ImageAdapter(Context context, boolean isMultiSelect) {
  31. this.context = context;
  32. this.isMultiSelect = isMultiSelect;

  33. // 采集图片资源
  34. for (int i = 0; i < imageResIds.length; i++) {
  35. imageIds.add(imageResIds[i]);
  36. }
  37. for (int i = 0; i < imageIds.size(); i++) {
  38. // 初始化各图片状态均为未选中
  39. imageStatues.put(i, false);
  40. }
  41. }

  42. @Override
  43. public int getCount() {
  44. return imageIds.size();
  45. }

  46. @Override
  47. public Object getItem(int position) {
  48. return position;
  49. }

  50. @Override
  51. public long getItemId(int position) {
  52. return position;
  53. }

  54. @Override
  55. public View getView(int position, View convertView, ViewGroup parent) {
  56. ImageView imageView = null;
  57. if (convertView == null) {
  58. imageView = new ImageView(context);
  59. imageView.setLayoutParams(new <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.LayoutParams(50, 50));
  60. imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  61. } else {
  62. imageView = (ImageView) convertView;
  63. }
  64. imageView.setImageDrawable(makeBmp(imageIds.get(position), imageStatues.get(position)));
  65. return imageView;
  66. }

  67. private LayerDrawable makeBmp(int id, boolean isSelect) {
  68. Bitmap imageBmp = ((BitmapDrawable) context.getResources().getDrawable(id)).getBitmap();
  69. // 根据isSelect来选取勾选的图片
  70. Bitmap selectedBmp = null;
  71. if (isSelect) {
  72. selectedBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.checkbox_pressed);
  73. } else {
  74. selectedBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.checkbox_normal);
  75. }
  76. // 生成叠加图
  77. Drawable[] drawArr = new Drawable[2];
  78. drawArr[0] = new BitmapDrawable(imageBmp);
  79. drawArr[1] = new BitmapDrawable(selectedBmp);
  80. LayerDrawable <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a> = new LayerDrawable(drawArr);
  81. <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(0, 0, 0, 0, 0);
  82. <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>.setLayerInset(1, 0, 0, 60, 60);

  83. return <a title="layerDrawable" href="http://www.android-study.com/jiemiansheji/556.html">layerDrawable</a>;
  84. }

  85. // 修改选中的状态
  86. public void changeState(int position) {
  87. if (isMultiSelect) { // 多选
  88. // 直接取反
  89. imageStatues.put(position, !imageStatues.get(position));
  90. } else { // 单选
  91. if (lastPosition != -1 && lastPosition != position) {
  92. // 取消上一次的选中状态
  93. imageStatues.put(lastPosition, false);
  94. }
  95. // 直接取反
  96. imageStatues.put(position, !imageStatues.get(position));
  97. lastPosition = position; // 记录本次选中的位置
  98. }
  99. notifyDataSetChanged(); // 通知适配器进行更新
  100. }
  101. }
复制代码

GridViewMainActivity.xml
  1. package cn.lynn.gridview;

  2. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.app.Activity;
  3. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.os.Bundle;
  4. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.view.View;
  5. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.AdapterView;
  6. import <a title="android" href="http://www.android-study.com/jiemiansheji/556.html">android</a>.widget.AdapterView.OnItemClickListener;
  7. 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>;

  8. public class <a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>MainActivity extends Activity {
  9. // 单选网格视图
  10. 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>;
  11. // 多选网格视图
  12. 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>;
  13. // 用于单选网格视图的内容适配器
  14. private ImageAdapter radioImageAdapter;
  15. // 用于多选网格视图的内容适配器
  16. private ImageAdapter checkImageAdapter;

  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);

  21. 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>);
  22. radioImageAdapter = new ImageAdapter(this, false);
  23. radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setAdapter(radioImageAdapter);
  24. // 设置点击图片事件监听
  25. radio<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setOnItemClickListener(new OnItemClickListener() {
  26. @Override
  27. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  28. radioImageAdapter.changeState(position);
  29. }
  30. });

  31. 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>);
  32. checkImageAdapter = new ImageAdapter(this, true);
  33. check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setAdapter(checkImageAdapter);
  34. // 设置点击图片事件监听
  35. check<a title="GridView" href="http://www.android-study.com/jiemiansheji/556.html">GridView</a>.setOnItemClickListener(new OnItemClickListener() {
  36. @Override
  37. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  38. checkImageAdapter.changeState(position);
  39. }
  40. });
  41. }

  42. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2