美文网首页
Picture viewer

Picture viewer

作者: 始終上路过 | 来源:发表于2019-08-16 21:33 被阅读0次

    超简单图片浏览器

    首先展示一下简单的效果
    就是这样

    1、manifest清单文件中的授权,获取读写权限,展示手机(模拟器)中所有的图片

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    build.gradle中Glide版本

    implementation 'com.github.bumptech.glide:glide:4.5.0'
    

    2、xml布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:padding="10dp">
    
        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="查看图片"
            android:background="@android:color/holo_blue_bright"/>
    
        <GridView
            android:id="@+id/gv_id"
            android:numColumns="4"
            android:gravity="center"
    
            android:horizontalSpacing="5dp"
            android:verticalSpacing="20dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </GridView>
    
    </LinearLayout>
    
    布局样式

    3、网格布局显示图片的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/im_id"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    
    </LinearLayout>
    

    4、弹框布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    5、MainActivity中的方法

    public class MainActivity extends AppCompatActivity {
    
        private Button button;
        private List<String> names = new ArrayList<String>();
        private List<String> descs = new ArrayList<String>();
        private List<String> fileNames = new ArrayList<String>();
        private GridView gvId;
        private Handler handler = new Handler();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button = findViewById(R.id.button_1);
            gvId = findViewById(R.id.gv_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
                }
            });
    
            gvId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    // 加载view.xml文件布局代表的视图
                    final View view = getLayoutInflater().inflate(R.layout.img_view_itme, null);
                    // 获取图片组件
                    ImageView image = (ImageView) view.findViewById(R.id.imageView1);
                    image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(arg2)));
    //                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    //                builder.setView(view);
    //
    //                AlertDialog alertDialog = builder.create();
    //                alertDialog.show();
                    final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setMax(100);
                    dialog.setMessage("加载中...");
                    dialog.show();//显示
                    //闲着没事模拟一个进度条
                    final Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        int progress = 0;
                        @Override
                        public void run() {
                            if (progress >= 100){
                                dialog.dismiss();
                                timer.cancel();
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                        builder.setView(view).setCancelable(true);
                                        //使用建造者创建对话框
                                        AlertDialog dialog = builder.create();
                                        dialog.show();
                                    }
                                });
                            }
                            dialog.setProgress(progress += 2);
                        }
                    },0,10);
    
                }
            });
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                allPic();
            }
        }
    
        private void allPic() {
    
            names.clear();
            descs.clear();
            fileNames.clear();
            // 通过ContentProvider查询所有的图片信息
            Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    null, null, null, null);
            while (cursor.moveToNext()) {
                // 获取图片的显示名
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
                byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                // 将图片描述添加到集合中
                descs.add(desc);
                names.add(name);
                fileNames.add(new String(data, 0, data.length - 1));
            }
    
            gvId.setAdapter(new MyAdapter());
    
        }
        class MyAdapter extends BaseAdapter {
            @Override
            public int getCount() {
                return fileNames.size();
            }
    
            @Override
            public Object getItem(int position) {
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if(convertView == null){
                    convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.image_layout,parent,false);
                    viewHolder = new ViewHolder();
                    viewHolder.imageView = convertView.findViewById(R.id.im_id);
                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                String s = fileNames.get(position);
                Glide.with(MainActivity.this).load(s).into(viewHolder.imageView);
                return convertView;
            }
            class ViewHolder{
                ImageView imageView;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Picture viewer

          本文链接:https://www.haomeiwen.com/subject/ntqcsctx.html