android:用毕加索创建圆形图像

Kat*_*lon 101 android bitmap imageview picasso

问题已被提出,并且我正在使用的毕加索版本有一个承诺:如何使用Picasso将圆形位图发送到ImageView?我是毕加索的新手,我唯一使用的是

Picasso.with(context).load(url).resize(w, h).into(imageview);
Run Code Online (Sandbox Code Playgroud)

我已经找到https://gist.github.com/julianshen/5829333但我不知道如何以非尴尬的方式将它与上面的行结合起来.

Ani*_*rma 275

之前研究一下,因为有可用的答案.无论如何,请关注此链接并仔细阅读以了解如何使用它.

试试这个:

import com.squareup.picasso.Transformation;

public class CircleTransform implements Transformation {
    @Override
    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

    @Override
    public String key() {
        return "circle";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后简单地应用它:

Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);
Run Code Online (Sandbox Code Playgroud)


goo*_*ode 44

这是support-v4库提供的东西!查看 RoundedBitmapDrawable.无需自己动手:

Picasso.with(context).load(url)
                        .resize(w, h)
                        .into(myImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                Bitmap imageBitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();
                                RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
                                imageDrawable.setCircular(true);
                                imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
                                myImageView.setImageDrawable(imageDrawable);
                            }
                            @Override
                            public void onError() {
                                myImageView.setImageResource(R.drawable.default_image);
                            }
                        });
Run Code Online (Sandbox Code Playgroud)

注意:Picasso也有理论上可以使用的.transform(customTransformation)调用,但是,我遇到了问题.以上工作原理.祝好运!


Bre*_*eno 17

我发现的另一个选择是这个人库.它可以单独使用,也可以与毕加索一起使用.我选择了毕加索的路线,如下所示:

https://github.com/vinc3m1/RoundedImageView

Transformation transformation = new RoundedTransformationBuilder()
          .borderColor(Color.BLACK)
          .borderWidthDp(3)
          .cornerRadiusDp(30)
          .oval(false)
          .build();

Picasso.with(context)
    .load(url)
    .fit()
    .transform(transformation)
    .into(imageView);
Run Code Online (Sandbox Code Playgroud)

为我工作!


小智 10

我已经尝试了上面的所有解决方案但没有一个给我圆形变换而没有裁剪图片.这个解决方案只适用于具有相同宽度和高度的图像..这是我在上面的解决方案

首先------

Picasso.with(getActivity())
            .load(url)
            .error(R.drawable.image2)
            .placeholder(R.drawable.ic_drawer)
            .resize(200, 200)
            .transform(new ImageTrans_CircleTransform())
            .into(imageView1);
Run Code Online (Sandbox Code Playgroud)

然后这样做--------

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;

import com.squareup.picasso.Transformation;
public class ImageTrans_CircleTransform implements Transformation {
 @Override
    public Bitmap transform(Bitmap source) {
 if (source == null || source.isRecycled()) {
                return null;
            }

            final int width = source.getWidth() + borderwidth;
            final int height = source.getHeight() + borderwidth;

            Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            BitmapShader shader = new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(shader);

            Canvas canvas = new Canvas(canvasBitmap);
            float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
            canvas.drawCircle(width / 2, height / 2, radius, paint);

            //border code
            paint.setShader(null);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(bordercolor);
            paint.setStrokeWidth(borderwidth);
            canvas.drawCircle(width / 2, height / 2, radius - borderwidth / 2, paint);
            //--------------------------------------

            if (canvasBitmap != source) {
                source.recycle();
            }

            return canvasBitmap;
}
 @Override
    public String key() {
        return "circle";
    }
}
Run Code Online (Sandbox Code Playgroud)


NIP*_*HIN 8

使用此库创建圆形图像视图. 要创建循环ImageView,请将此CircularImageView库添加到项目中, 并在布局XML中添加CircularImageView

<com.pkmmte.view.CircularImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/image"
        app:border_color="#EEEEEE"
        app:border_width="4dp"
        app:shadow="true" />`
Run Code Online (Sandbox Code Playgroud)

然后使用picasso将所需图像加载到此imageView中.毕加索做了所有你不需要担心的缓存


Nik*_*kov 6

添加gradle依赖

implementation 'jp.wasabeef:picasso-transformations:2.2.1'
Run Code Online (Sandbox Code Playgroud)

最终使用它

Picasso.with(context)
       .load(url)
       .resize(w, h)
       .transform(new CropCircleTransformation())
       .into(imageview);
Run Code Online (Sandbox Code Playgroud)

Wiki:毕加索转型

  • 这确实是这个问题的完美答案,为什么票数这么少。 (2认同)