搜索
您的当前位置:首页正文

关于安卓gridview和ImageViewer结合使用的滑动问

来源:哗拓教育

有一个页面如下图所示:
gridview分布图片,当点击双层图片图标的时候拍摄照片,当点击已拍摄的图片后显示大图且可以左右滑动。比如:点击"入口照片"是拍摄照片;点击"整体照片是"查看,且当向右滑动的时候,下一张图片是45°

image.png
因为gif太大,这里分享链接

项目:E:\ASProject\010trunk\CommonReportPage:FragmentPhoto.java/MyControlPhotoBox.java
CommonReportPage工程被打包成aar包在E:\ASProject\newapp项目中被使用
主要步骤:
①首先点击gridview条目时会判断该位置itemString fPath = adapter.getItem(i).getFilePath()是否为null,为null进入拍摄状态,不为null继续判断是否是视频/图片
②如果是视频,跳转播放视频,注意这里安卓7.0以上版本视频播放要使用FileProvider
③如果是图片,先对gridview进行遍历,对每个位置进行判断,有图片将该位置图片的path分别存到list和listPic集合中,其中list集合是存的所有gridview(不管有无图片)所有位置的path,listPic集合存的是item位置有图片的path。
④对list、listPic集合进行嵌套遍历,取出这两个集合中重复元素path所对应的两个集合的下标,保存到新集合List<IndexPojo> indexList 中,之后对有图片的gridview作点击事件监听new ImageViewer.Builder(mContext, listPic)
主要方法

    private void InitEvent() {
        /**拍照按钮点击事件*/
        ivCameraLow.setOnClickListener(this);

        /**照片grid适配器设置和初始化*/
        adapter = new AdapterPhotoGrid(getContext(), 0, phList);
        gvGrid.setAdapter(adapter);
        if (isLocalPhotoMode) InitialPhotoList(photoSavePath);//异步加载照片保存路径中的所有图片

        /**照片grid中条目点击事件*/
        gvGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String fPath = adapter.getItem(i).getFilePath();
                if (null == fPath) {  //该位置无图片
                    if (inputData.getReadOnlyMode()) return; //只读模式禁止拍照
                    Intent intent = new Intent(mContext, MyCameraActivity.class);
                    intent.putExtra(MyCameraActivity.PHOTO_TAKEN_MODE, PHOTO_TAKE_MODE_FIX);
                    intent.putExtra(MyCameraActivity.PHOTO_SAVE_PATH, photoSavePath);
                    ArrayList<String> photoPathSet = new ArrayList<>();
                    Collections.addAll(photoPathSet, fixPhotoNameArr);
                    intent.putExtra(MyCameraActivity.FIX_PHOTO_NAME_SET, photoPathSet);
                    intent.putExtra(MyCameraActivity.PHOTO_SOURCE_NAME, modelElementInfo.getEleName());
                    intent.putExtra(MyCameraActivity.FIX_PHOTO_NAME_SET_STRAT_INDEX, i);
                    mContext.startActivity(intent);
                } else {
                    if (adapter.getItem(i).isVideo()) {  //该位置有占位,是视频
                        try {
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            String vPath = adapter.getItem(i).getFilePath();
                            File file = new File(vPath);
                            Uri uri = Uri.parse("file://" + file);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //安卓7.0以上版本视频播放
                                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                Uri contentUri = FileProvider.getUriForFile(mContext, "com.jsptpd.srfa" +
                                        ".FileProvider", file); //包名指向newapp项目的包名
                                intent.setDataAndType(contentUri, "video/*");

                            } else {
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setDataAndType(uri, "video/*");
//                                video.setVideoURI(uri);
                            }

                            mContext.startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {  //该位置有占位,是图片
                        try {
                            int count = gvGrid.getAdapter().getCount();
                            List<String> list = new ArrayList<>();
                            List<String> listPic = new ArrayList<>();

                            for (int ii = 0; ii < count; ii++) {
                                ModelPhotoInfo each = ((ModelPhotoInfo) gvGrid.getAdapter().getItem(ii));
                                if (each.getFilePath() != null && new File(each.getFilePath()).exists()) {
                                    list.add("file://" + each.getFilePath());
                                    listPic.add("file://" + each.getFilePath());
                                } else {
                                    list.add("file://" + "");
                                }
                            }

                            ImageOverlayView imageOverlayView = new ImageOverlayView(mContext);


                            List<IndexPojo> indexList = new ArrayList<>(); //有图片的两个list的下标集合
                            for (int indexA = 0; indexA < list.size(); indexA++) {
                                String s = list.get(indexA);
                                for (String s1 : listPic) {
                                    if (s.equals(s1)) {
                                        int indexB = listPic.indexOf(s1);
                                        IndexPojo indexPojo = new IndexPojo();
                                        indexPojo.setIndexA(indexA);
                                        indexPojo.setIndexB(indexB);
                                        indexList.add(indexPojo);
                                    }
                                }
                            }


                            new ImageViewer.Builder(mContext, listPic)  //这里使用有图片的集合
                                    .setStartPosition(returnIndex(i, indexList))  //因此这里也使用有图片的集合的下标
                                    .setOverlayView(imageOverlayView)
                                    .setImageChangeListener(new ImageViewer.OnImageChangeListener() {
                                        @Override
                                        public void onImageChange(int position) {  //position和i是gridview的位置
                                            Log.e("position:", position + "");
                                            int indexB = returnIndex(position, indexList);

                                            String[] split = listPic.get(indexB).split("/");
                                            imageOverlayView.setTitle(split[split.length - 2] + "_" + split[split.length
                                                    - 1]);
                                        }
                                    })
                                    .show();

                        } catch (Exception ex) {
                            Toast.makeText(mContext, "没有找到可以浏览图片的应用", Toast.LENGTH_SHORT);
                        }
                    }
                }
            }
        });

returnIndex()方法

    /**
     * 返回gridView中图片的下标在新集合中的位置indexB
     *
     * @param position
     * @param indexList
     * @return
     */
    public int returnIndex(int position, List<IndexPojo> indexList) {
        int indexB = 0;
        for (IndexPojo indexPojo : indexList) {
            int indexA = indexPojo.getIndexA();
            if (position == indexA) {
                indexB = indexPojo.getIndexB();
            }
        }
        return indexB;
    }

IndexPojo

package com.jsptpd.workformcommon.Wiget;

/**
 * Created by HASEE on 2018/11/23 10:34
 */

class IndexPojo {
    private int indexA;  //gridView下标
    private int indexB;  //有图片集合下标

    public int getIndexA() {
        return indexA;
    }

    public void setIndexA(int indexA) {
        this.indexA = indexA;
    }

    public int getIndexB() {
        return indexB;
    }

    public void setIndexB(int indexB) {
        this.indexB = indexB;
    }
}

FragmentPhoto

/**
 *
 */
package com.jsptpd.workformcommon.Fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jsptpd.workformcommon.Model.ModelElementInfo;
import com.jsptpd.workformcommon.Wiget.MyControlPhotoBox;
import com.jsptpd.workformcommon.R;

/**
 * @author lw
 */
public class FragmentPhoto extends BasicWorkFormFragment {

    List<ModelElementInfo> wftList;
    List<MyControlPhotoBox> photoBoxList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wftList = inputData.getPageFrameData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View thisFragView = inflater.inflate(R.layout.work_form_photo_frag, container, false);
        LinearLayout ll = (LinearLayout) thisFragView.findViewById(R.id.work_form_photo);
        photoBoxList = new ArrayList<>();
        for (ModelElementInfo modelElementInfo : wftList) {
            if (!modelElementInfo.getEleType().equals(ModelElementInfo.ZU) && modelElementInfo.getControlType().equals(ModelElementInfo.CT_PHOTO)) {
                String photoPath = inputData.getPhotoPath() + File.separator + modelElementInfo.getEleName() + File.separator; //默认照片路径等照片路径+组名
                //如果初始化数据中有照片路径值,则取代默认的照片路径,这里不检查路径的有效性,会在拍照或照片下载的时候检查
                if (inputData.getInitData() != null && inputData.getInitData().containsKey(modelElementInfo.getEleId())) {
                    photoPath = inputData.getInitData().get(modelElementInfo.getEleId()).getValue();
                }
                MyControlPhotoBox myPhotoBoxControl = new MyControlPhotoBox(this.getActivity(), modelElementInfo, photoPath,inputData);
                ll.addView(myPhotoBoxControl);
                photoBoxList.add(myPhotoBoxControl);
            }
        }
        return thisFragView;
    }


}

Top