Android:如何使用毕加索裁剪图像?

问题描述 投票:0回答:1

我想使用适用于Android的Picasso图像库来裁剪图像-不调整其大小。

更具体地说,我要加载图像,然后将其取为具有(x,y),宽度w和高度h的矩形部分:

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

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

android bitmap picasso
1个回答
0
投票

您可以为此使用Picasso的变换功能。它允许您传递自己的方法来对Bitmap可绘制对象进行必要的更改:

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
© www.soinside.com 2019 - 2024. All rights reserved.