本文实例讲述了Android开发之组件GridView简单使用方法。分享给大家供大家参考,具体如下:
案例:简单的图片浏览器,保存图片到相册
保存图片到相册 方法代码:http://www.tuohang.net/article/246457.html
废话不多说先上效果;
具体实现:
首先是布局文件: 1.一个GridView(展示所有的图片) 2.一个ImageView(放选中的图片)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--定义一个GridView组件--> < GridView android:id = "@+id/grid01" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:horizontalSpacing = "1pt" android:verticalSpacing = "1pt" android:numColumns = "3" android:gravity = "center" /> <!--定义一个ImageView组件--> < ImageView android:id = "@+id/image_view" android:layout_width = "240dp" android:layout_height = "240dp" android:layout_gravity = "center_horizontal" /> </ LinearLayout > |
然后是点击事件的设置:
其中arry_list只有一个ImageView放图片 代码下面给出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
public class MainActivity extends Activity { private GridView gridView; //定义字符串数组作为提示的文本 private ImageView imageView; private int [] photoId = new int []{ R.drawable.huangjindiao, R.drawable.piaopiao, R.drawable.xiaochouyu, R.drawable.paodangyu, R.drawable.addphoto, R.drawable.huangjinxiahu }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个list对象,list对象的元素是Map List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for ( int i = 0 ; i < photoId.length ; i ++ ){ Map<String,Object> photolist = new HashMap<String, Object>(); photolist.put( "image" ,photoId[i]); listItems.add(photolist); } //获取图片的imageView imageView = (ImageView) findViewById(R.id.image_view); //创建一个SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter( this ,listItems, //arry_list只有一个ImageView放图片 代码下面给出 R.layout.array_list, new String[]{ "image" }, new int []{R.id.image}); gridView = (GridView) findViewById(R.id.grid01); //为gridview设置adapter gridView.setAdapter(simpleAdapter); //添加列表项中被选中的监听器 gridView.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //显示当前被选中的图片 imageView.setImageResource(photoId[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //添加列表中被单击的监听器 gridView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //显示被单击的图片 imageView.setImageResource(photoId[position]); } }); //点击图片保存 imageView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String[] PERMISSIONS = { "android.permission.READ_EXTERNAL_STORAGE" , "android.permission.WRITE_EXTERNAL_STORAGE" }; //检测是否有写的权限 int permission = ContextCompat.checkSelfPermission(MainActivity. this , "android.permission.WRITE_EXTERNAL_STORAGE" ); if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(MainActivity. this , PERMISSIONS, 1 ); } try { //创建savephoto类保存图片 SavePhoto savePhoto = new SavePhoto(MainActivity. this ); savePhoto.SaveBitmapFromView(imageView); } catch (ParseException e) { e.printStackTrace(); } } }); } } |
值得注意的一点:array_list:
1
2
3
4
5
6
7
|
<? xml version = "1.0" encoding = "utf-8" ?> < ImageView xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/image" android:layout_width = "100dp" android:layout_height = "100dp" android:scaleType = "fitXY" > </ ImageView > |
希望本文所述对大家Android程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_43377749/article/details/84325754