【问题标题】:Java Android glide resize image [duplicate]Java Android滑行调整图像大小[重复]
【发布时间】:2018-09-26 12:34:44
【问题描述】:

我想调整 glide 使用的图像大小。现在我用毕加索,但我想用滑翔机

private void resizeImg(Context context, final File file, int targetSize, Handler handler) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), options);

        float ratio = Math.max(options.outWidth, options.outHeight) / (float) targetSize;

        //don't scale image that is smaller than targetSize
        if (ratio < 1) return;

        int dstWidth = (int) (options.outWidth / ratio);
        int dstHeight = (int) (options.outHeight / ratio);

        PicassoTargetFactory.getInstance().putTarget(file, handler);

        Picasso.with(context)
                .load(file)
                .error(R.drawable.ic_delete)
                .resize(dstWidth, dstHeight)
                .into(PicassoTargetFactory.getInstance().getTarget(file));
        ArrayList<String> paths = new ArrayList<>();
        files = new ArrayList<>();
        files.add(mCurrentPhotoPath);
        for (File f : files) {
            paths.add(f.getAbsolutePath());
        }
        mdb.insertPhotos(paths);

    }

我该怎么做?在 piccaso 上有时我有一个剪切图像

【问题讨论】:

标签: java android


【解决方案1】:

首先,在 Gradle 中添加 Glide 库。然后,你可以这样使用它

Glide  
.with(context)
.load("your image path")
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);

【讨论】:

  • 好的,但我想调整图像并将其保存到文件中而不是 imageView
【解决方案2】:

必须在最新版本的 Glide 中通过 RequestOptions 访问覆盖。

Glide
.with(context)
.load(path)
.apply(new RequestOptions().override(600, 200))
.into(imageViewResizeCenterCrop);

【讨论】:

    【解决方案3】:

    您可以创建 RequestOptions 类对象并将其传递给 Glide 中的 apply() 方法。

    类似使用,

    Glide.with(context)
        .load(url)
        .apply(new RequestOptions().override(Your image size here))
        .into(imageView);
    

    更多来自here

    【讨论】:

    【解决方案4】:

    我认为你应该给你的方法一个单一的责任。您想要一种调整图像大小的方法,但您的方法实际上做得更多。请把东西分开

    private Bitmap resizeBitmap(Bitmap b, int w, int h) {
        // Your code.
        return myResizedBitmap;
    }
    

    那么帮助你会更容易

    【讨论】:

    • 这个方法调整我的图片大小
    【解决方案5】:
    private String getImgCachePath(String url) {
        FutureTarget<File> futureTarget = Glide.with(getBaseContext()).load(url).downloadOnly(100, 100);
        try {
            File file = futureTarget.get();
            String path = file.getAbsolutePath();
            return path;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    100,100 是您要保存的图像的宽度和高度,根据需要进行更改。 path是图片url给定分辨率的缓存图片路径。

    【讨论】:

    • 此方法调整我的图像大小并保存到文件中? FutureTarget futureTarget = Glide.with(getBaseContext()).load(url).downloadOnly(100, 100);
    • 是的@jpok2,试试看。
    • 好的,但是当我的图像是水平的时,它会更改为垂直,但它不会在关于图像的 exddif 信息中改变
    • 什么是 exddif?
    • ExifInterface ,图像方向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多