Camera2缩略图预览功能的实现

第一代缩略图打开预览功能,通过自定义实现缩略图的打开展示。通过继承ViewSwitcher.ViewFactory接口实现其makeView方法获取view。

具体方法如下:

@Override
public View makeView() {
    cursorImage = getContentResolver().query(EXT_CONTENT_URI, PROJECTION_IMAGE,
                                             null, null, MediaStore.Images.Media.DATE_ADDED + " DESC");
    cursorImage.moveToFirst();

    while (!cursorImage.isLast() && cursorImage.getCount() > 0){
        String pathImage = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "compareImageAndVideoPath: 所有图片path: " + pathImage);
        Log.d(TAG, "makeView: i = " + ++i);
        boolean isMatchImage = Pattern.matches(patternDCIMDirectory, pathImage);
        if (isMatchImage){
            long id = cursorImage.getLong(cursorImage.getColumnIndex(MediaStore.MediaColumns._ID));
            uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
            break;
        }else {
            cursorImage.moveToNext();
        }
    }
    ImageView imageView = new ImageView(ImagePreview.this);
    imageView.setImageURI(uri);
    return imageView;
}

通过继承View.OnTouchListener的onTouch方法实现左右滑动切换图片的功能:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN){
        //如果获得当前事件的活动等于鼠标点下,则给touchDownX赋值
        touchDownMouse = event.getX();
        return true;
    }else if (event.getAction() == MotionEvent.ACTION_UP){  如果鼠标抬起
        //记录此时坐标
        touchUpMouse = event.getX();
        //判断移动方向
        if (touchUpMouse - touchDownMouse > 100){
            //渐入渐出效果
            previewImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImagePreview.this,android.R.anim.fade_in));
            previewImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImagePreview.this,android.R.anim.fade_out));
            if (cursorImage.isLast()){
                Log.d(TAG, "onTouch: 这就是最后一张照片");
            }else {
                while (cursorImage.moveToNext()){
                    String pathImage = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));
                    Log.d(TAG, "compareImageAndVideoPath: 所有图片path: " + pathImage);
                    Log.d(TAG, "makeView: i = " + ++i);
                    boolean isMatchImage = Pattern.matches(patternDCIMDirectory, pathImage);
                    if (isMatchImage){
                        long id = cursorImage.getLong(cursorImage.getColumnIndex(MediaStore.MediaColumns._ID));
                        uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                        break;
                    }
                }
                previewImageSwitcher.setImageURI(uri);
            }
        }else if (touchDownMouse - touchUpMouse > 100){
            //渐入渐出效果
            previewImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImagePreview.this,android.R.anim.slide_in_left));
            previewImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImagePreview.this,android.R.anim.slide_out_right));
            if (cursorImage.isFirst()){
                Log.d(TAG, "onTouch: 这就是第一张照片");
            }else {
                while (cursorImage.moveToPrevious()) {
                    String pathImage = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));

                    boolean isMatchImage = Pattern.matches(patternDCIMDirectory, pathImage);
                    if (isMatchImage) {
                        long id = cursorImage.getLong(cursorImage.getColumnIndex(MediaStore.MediaColumns._ID));
                        uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                        break;
                    }
                }
                previewImageSwitcher.setImageURI(uri);
            }
        }
        return true;
    }
    return false;
}

效果不好,deprecated

第二代缩略图点击预览功能,通过调用厂商相册自定义action跳转gallery预览,在AnyscTask中执行防止耗时过多:

@SuppressLint("Recycle")
Cursor cursorImage = getContentResolver().query(EXT_CONTENT_URI, PROJECTION_IMAGE,
                                                null, null, MediaStore.Images.Media.DATE_ADDED + " DESC");
@SuppressLint("Recycle")
Cursor cursorVideo = getContentResolver().query(EXT_CONTENT_URI_VIDEO, PROJECTION_VIDEO,
                                                null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");

Object[] uriAndPath = compareImageAndVideoPathOpen(cursorImage, cursorVideo);
assert uriAndPath != null;
Uri mUri = (Uri) uriAndPath[0];
String pathImageOrVideo = (String) uriAndPath[1];

Uri lastPictureUri = Uri.parse(pathImageOrVideo);

Intent intentScanFile = new Intent();
intentScanFile.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentScanFile.setData(lastPictureUri);
sendBroadcast(intentScanFile);

Intent intent = new Intent("com.miui.camera.action.REVIEW", mUri);
intent.setPackage("com.miui.gallery");
startActivity(intent);
Log.d(TAG, "onClick: intent action" + intent.getAction());
break;

util类中获取image和video的方法如下:

public static Object[] compareImageAndVideoPathOpen(Cursor cursorImage, Cursor cursorVideo){
    cursorImage.moveToFirst();
    cursorVideo.moveToFirst();
    String subStringPathImage = "";
    String subStringPathVideo = "";

    while (!cursorImage.isLast() && cursorImage.getCount() > 0){
        String pathImage = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));
        File file = new File(pathImage);
        if (file.length() == 0){
            cursorImage.moveToNext();
        }else {
            boolean isMatchImage = Pattern.matches(PATTERN_DCIM_DIRECTORY, pathImage);
            if (isMatchImage) {
                subStringPathImage = pathImage.substring(pathImage.length() - 18, pathImage.length() - 4);
                break;
            } else {
                cursorImage.moveToNext();
            }
        }
    }

    while (!cursorVideo.isLast() && cursorVideo.getCount() > 0){
        String pathVideo = cursorVideo.getString(cursorVideo.getColumnIndex(MediaStore.Video.Media.DATA));
        File file = new File(pathVideo);
        if (file.length() == 0){
            //                boolean delete = file.delete();
            //                if (delete){
            //                    Log.d(TAG, "compareImageAndVideoPathOpen: empty Video deleted");
            //                }else {
            //                    Log.d(TAG, "compareImageAndVideoPathOpen: empty Video deleted failed");
            //                }
            cursorVideo.moveToNext();
        }else {
            boolean isMatchVideo = Pattern.matches(PATTERN_DCIM_DIRECTORY, pathVideo);
            if (isMatchVideo) {
                subStringPathVideo = pathVideo.substring(pathVideo.length() - 18, pathVideo.length() - 4);
                break;
            } else {
                cursorVideo.moveToNext();
            }
        }
    }

    Log.d(TAG, "compareImageAndVideoPathOpen: image的子串:------------" + subStringPathImage);
    Log.d(TAG, "compareImageAndVideoPathOpen: video的子串:------------" + subStringPathVideo);
    String pathImageOrVideo = "";
    long id;
    Uri mUri;

    if (subStringPathImage.equals("") && subStringPathVideo.equals("")){
        Log.d(TAG, "image and video all null ");
        return null;
    }else if (subStringPathImage.equals("")){
        Log.d(TAG, "image is null ");
        pathImageOrVideo = cursorVideo.getString(cursorVideo.getColumnIndex(MediaStore.Video.Media.DATA));
        pathImageOrVideo = "file:///" + pathImageOrVideo;
        Log.d(TAG, "compareImageAndVideoPathOpen: pathImageOrVideo : " + pathImageOrVideo);
        id = cursorVideo.getLong(cursorVideo.getColumnIndex(MediaStore.MediaColumns._ID));
        mUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
    }else if (subStringPathVideo.equals("")){
        Log.d(TAG, "video is null ");
        pathImageOrVideo = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));
        pathImageOrVideo = "file:///" + pathImageOrVideo;
        Log.d(TAG, "compareImageAndVideoPathOpen: pathImageOrVideo : " + pathImageOrVideo);
        id = cursorImage.getLong(cursorImage.getColumnIndex(MediaStore.MediaColumns._ID));
        mUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
    }else {
        int result = subStringPathImage.compareTo(subStringPathVideo);
        Log.d(TAG, "compareImageAndVideoPathOpen -- image and video all not null ");

        if (result > 0) {
            pathImageOrVideo = cursorImage.getString(cursorImage.getColumnIndex(MediaStore.Images.Media.DATA));
            pathImageOrVideo = "file:///" + pathImageOrVideo;
            Log.d(TAG, "compareImageAndVideoPathOpen: pathImageOrVideo : " + pathImageOrVideo);
            id = cursorImage.getLong(cursorImage.getColumnIndex(MediaStore.MediaColumns._ID));
            mUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        } else {
            pathImageOrVideo = cursorVideo.getString(cursorVideo.getColumnIndex(MediaStore.Video.Media.DATA));
            pathImageOrVideo = "file:///" + pathImageOrVideo;
            Log.d(TAG, "compareImageAndVideoPathOpen: pathImageOrVideo : " + pathImageOrVideo);
            id = cursorVideo.getLong(cursorVideo.getColumnIndex(MediaStore.MediaColumns._ID));
            mUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
        }

    }
    cursorImage.close();
    cursorVideo.close();
    return objReturnFun(mUri,pathImageOrVideo);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值