图片选择裁剪库


这里我选择的是B站开源的库:
https://github.com/bilibili/boxing

最简单的使用方法

//初始化
IBoxingMediaLoader loader;
loader = new BoxingGlideLoader();
BoxingMediaLoader.getInstance().init(loader);
BoxingConfig singleImgConfig = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG);
Boxing.of(singleImgConfig).withIntent(this, BoxingActivity.class).start(this, REQUEST_CODE);

BoxingGlideLoader类
注意要引入:implementation 'com.github.bumptech.glide:glide:3.7.0'
版本不能错。

public class BoxingGlideLoader implements IBoxingMediaLoader {

    @Override
    public void displayThumbnail(@NonNull ImageView img, @NonNull String absPath, int width, int height) {
        String path = "file://" + absPath;
        try {
            // https://github.com/bumptech/glide/issues/1531
            Glide.with(img.getContext()).load(path).placeholder(R.mipmap.ic_boxing_default_image).crossFade().centerCrop().override(width, height).into(img);
        } catch(IllegalArgumentException ignore) {
        }

    }

    @Override
    public void displayRaw(@NonNull final ImageView img, @NonNull String absPath, int width, int height, final IBoxingCallback callback) {
        String path = "file://" + absPath;
        BitmapTypeRequest<String> request = Glide.with(img.getContext())
                .load(path)
                .asBitmap();
        if (width > 0 && height > 0) {
            request.override(width, height);
        }
        request.listener(new RequestListener<String, Bitmap>() {
            @Override
            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                if (callback != null) {
                    callback.onFail(e);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                if (resource != null && callback != null) {
                    img.setImageBitmap(resource);
                    callback.onSuccess();
                    return true;
                }
                return false;
            }
        }).into(img);

    }
}

在activity中获取图片要重写下面这个方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        final ArrayList<BaseMedia> medias = Boxing.getResult(data);
        if (requestCode ==REQUEST_CODE) {
            assert medias != null;
            background.setBackground(new BitmapDrawable(getResources(),Tools.getBitmapFromPath(medias.get(0).getPath())));
        }
    }
}

这个常量是随便设置的:
private static final int REQUEST_CODE = 1024;


文章作者: 小游
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小游 !
  目录