如何使矩形图像变成圆形?

时间:2016-06-23 15:32:53

标签: android android-studio android-animation

我有一个带有图像的RecyclerView,我希望图像缩小到它的大小的一半,同时它变成一个圆圈然后我希望动画能够站立一段时间。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

使用毕加索设置图像

Picasso.with(context)
  .load(imgSource)
  .transform(new CircleTransform())
  .resize(128, 128)
  .centerCrop()
  .into(imageView);

转型课程

    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, BitmapShader.TileMode.CLAMP, BitmapShader.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";
    }
}

依赖

compile 'com.squareup.picasso:picasso:2.5.2'