android组件GridView简单使用

2021-09-24 12:26:49 浏览数 (1)

案例:简单的图片浏览器,保存图片到相册

保存图片到相册 方法代码:https://blog.csdn.net/qq_43377749/article/details/84325487

废话不多说先上效果;

具体实现:

首先是布局文件: 1.一个GridView(展示所有的图片) 2.一个ImageView(放选中的图片)

代码语言:javascript复制
<?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放图片 代码下面给出

代码语言:javascript复制
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:

代码语言:javascript复制
<?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>

0 人点赞