zoukankan      html  css  js  c++  java
  • Android 打开相册拍照选择多张图片显示

    添加依赖:

       compile 'me.iwf.photopicker:PhotoPicker:0.1.8'
        compile 'com.jaeger.ninegridimageview:library:1.0.1'
        compile 'com.youth.banner:banner:1.4.10'
    public class MainActivity extends AppCompatActivity {
        int REQUEST_CODE = 0;
        private NineGridImageView ngiv;
        private ArrayList<String> photos;
        private Dialog dialog;
        private cn.com.jwtimes.www.jwtimes.mAdapter mAdapter1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv = findViewById(R.id.tv);
            ngiv = findViewById(R.id.ngiv);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(MainActivity.this);
                    intent.setPhotoCount(9);
                    intent.setShowCamera(true);
                    startActivityForResult(intent, REQUEST_CODE);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
                if (data != null) {
                    photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                    ngiv.setAdapter(mAdapter);
                    ngiv.setImagesData(photos);
                }
            }
        }
    
        private NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, final String url) {
                Glide.with(context)
                        .load(url)
                        .into(imageView);
            }
    
            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }
    
            @Override
            protected void onItemImageClick(Context context, int index, List<String> photoList) {
                showBigPicture(context, photos, index);
            }
        };
    
        private void showBigPicture(Context context, final ArrayList<String> photos, final int index) {
            dialog = new Dialog(context, R.style.MyDialog);
            View contentView = View.inflate(context, R.layout.dialog_tip, null);
            final ViewPager vp = (ViewPager) contentView.findViewById(R.id.vp);
            ImageView ivDelete = (ImageView) contentView.findViewById(R.id.ivDelete);
            mAdapter1 = new mAdapter(this, photos, dialog);
            vp.setAdapter(mAdapter1);
            vp.setCurrentItem(index);
            ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int currentItem = vp.getCurrentItem();
                    Iterator<String> sListIterator = photos.iterator();
                    while (sListIterator.hasNext()) {
                        String e = sListIterator.next();
                        if (e.equals(photos.get(currentItem))) {
                            sListIterator.remove();
                            mAdapter1.notifyDataSetChanged();
                            break;
                        }
                    }
                }
            });
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    ngiv.setAdapter(MainActivity.this.mAdapter);
                    ngiv.setImagesData(photos);
                }
            });
            dialog.setContentView(contentView);
            dialog.setCancelable(true);
            dialog.getWindow().setLayout(
                    getScreenWidth(),
                    getScreenHeight());
            dialog.show();
        }
        public int getScreenWidth() {
            Resources resources = getResources();
            return resources.getDisplayMetrics().widthPixels;
        }
        public int getScreenHeight() {
            Resources resources = getResources();
            return resources.getDisplayMetrics().heightPixels;
        }
    
    }
    public class mAdapter extends PagerAdapter {
        private ArrayList<String> photos;
        private Context context;
        private  Dialog dialog;
    
        public mAdapter(Context context, ArrayList<String> photos, Dialog dialog) {
            this.photos = photos;
            this.context = context;
            this.dialog=dialog;
        }
    
        @Override
        public int getCount() {
            return photos.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public View instantiateItem(ViewGroup container, int position) {
    
            ImageView iv = new ImageView(context);
            Glide.with(context).load(photos.get(position)).into(iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            container.addView(iv);
            return iv;
    
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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="cn.com.jwtimes.www.jwtimes.MainActivity">
       <TextView
           android:id="@+id/tv"
           android:layout_width="match_parent"
           android:text="选择照片"
           android:layout_height="wrap_content" />
        <com.jaeger.ninegridimageview.NineGridImageView
            android:id="@+id/ngiv"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:imgGap="4dp"
            app:showStyle="fill"
            app:singleImgSize="120dp" />
    
    </android.support.constraint.ConstraintLayout>

    dialog_top.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black_40">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <ImageView
            android:id="@+id/ivDelete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/delete" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_margin="10dp"
            android:gravity="right"
            android:text="1/9"
            android:textColor="@color/colorAccent" />
    </FrameLayout>
    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
        <style name="MyDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:windowBackground">@color/black_40</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
        </style>
    </resources>

    效果:

  • 相关阅读:
    Linux+Apache环境下安装SSL证书
    Linux+Tomcat环境下安装SSL证书
    nginx配置ssl证书实现https访问
    Centos7安装jenkins
    kali linux网络配置
    Docker 国内镜像的配置及使用
    centos7安装tomcat8.5
    centos7 安装nginx
    Kali linux 2018 安装 Fluxion
    mysql5.7安装
  • 原文地址:https://www.cnblogs.com/loaderman/p/10986151.html
Copyright © 2011-2022 走看看