【问题标题】:Android: How to crop an image using Picasso?Android:如何使用毕加索裁剪图像?
【发布时间】:2020-08-30 04:25:48
【问题描述】:

我想使用 Android 的毕加索图像库来裁剪图像——而不是调整它的大小。

更具体地说,我想加载一张图片,然后用坐标(x,y)、宽度w、高度h 截取一张矩形:

Original Image
------------------------------
|                            |
|    (x,y)                   |
|      ------------          |
|      |          |          |
|      | Cropped  |          |
|      |  Image   | h        |
|      |          |          |
|      |          |          |
|      ------------          |
|           w                |
------------------------------

然后,我想将这个矩形的原始图像加载到ImageView 中。我怎么做?

【问题讨论】:

    标签: android bitmap picasso


    【解决方案1】:

    您可以为此使用毕加索的变换功能。它允许您通过自己的方法对Bitmap drawable 进行所需的更改:

    Picasso.with(getContext())
           .load(R.drawable.sample)  // the image you want to load
           .transform(new Transformation() {
               @Override
               public Bitmap transform(Bitmap source) {
                   Bitmap result = Bitmap.createBitmap(source,x,y,w,h);   // the actual cropping
                   source.recycle();   // recycle the source bitmap to avoid memory problems
                   return result;
               }
    
               @Override
               public String key() {
                   return x+","+y+","+w+","+h;  // some id unique for the transformation you do
               }
           })
           .into(findViewById(R.id.yourImageView);    // load the cropped image into your image view
    

    【讨论】:

    • 完美运行!
    猜你喜欢
    • 2019-10-18
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多