使用毕加索制作带圆角的ImageView

时间:2015-06-08 08:40:06

标签: android imageview picasso

我知道有很多链接可用于ImageView圆角。 但是我使用Picasso库进行图像加载.. 我引用link来获得结果。 但问题是我在 ListView LIstView's第一项ImageView中使用它,它的工作完全正常但是对于剩余的一次转换没有用。

7 个答案:

答案 0 :(得分:42)

我正在使用这种转变: https://gist.github.com/julianshen/5829333

Picasso.with(activity).load(url).transform(new CircleTransform()).into(imageView);

答案 1 :(得分:27)

您可以使用RoundedCornersTransformationpicasso-transformations库。

示例:

- (IBAction)onbtnTapped:(id)sender {

    self.view1.frame = CGRectMake( _btn1.frame.origin.x+_btn1.frame.size.width, _btn1.frame.origin.y+_btn1.frame.size.height, 0, 0);

    self.view1.hidden=NO;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationDelegate: self];

    self.view1.transform = CGAffineTransformMakeTranslation( 1, 1);
    self.view1.frame = CGRectMake( 0, 50, self.view.frame.size.width, 200);

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fromValue = [NSNumber numberWithFloat: 100];
    animation.toValue = [NSNumber numberWithFloat: 0];
    animation.duration = 0.5;
    [self.view1.layer setCornerRadius: 0];
    [self.view1.layer addAnimation:animation forKey:@"cornerRadius"];
    [UIView commitAnimations];
}

答案 2 :(得分:24)

你可以使用这个类用Picasso制作圆角矩形图像视图,像这样使用它

 Picasso.with(activity).load(url).transform(new RoundedCornersTransform(this)).into(imageView);

这是RoundedCornersTransform类。

package com.demo.picasso;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;


public class RoundedCornersTransform 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 / 8f;
    canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}

@Override
public String key() {
    return "rounded_corners";
  }
}

答案 3 :(得分:7)

我使用RoundedCornersTransformationpicasso-transformations库。我的listview中有自定义适配器和视图持有者模式。我在build.gradle中添加了以下依赖项:

dependencies {
        compile 'jp.wasabeef:picasso-transformations:2.1.0'
} 

在我的customArrayAdapter.java中,我补充道:

Picasso.with(getContext()).load(path).transform(new RoundedCornersTransformation(10,10)).resize(175,300).into(viewHolder.ivImage);
这将调整大小并为您的图像提供圆角。

答案 4 :(得分:4)

喜欢说here。您可以使用MaskTransformationpicasso-transformations库。

示例:

final Transformation transformation = new MaskTransformation(getContext(), R.drawable.rounded_convers_transformation);
Picasso.with(activity).load(url).transform(transformation).into(imageView);

<强> RES /抽拉/ rounded_convers_transformation.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <corners android:radius="5dp"/>
    <solid android:color="@color/black"/>
</shape>

更新:但请注意,您还应该.resize(w,h)图像,因为如果图像很大,那么这一轮将无法确定

答案 5 :(得分:2)

关注@stevyhacker's answerthis related one,我提出了这个问题:

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;


public class RoundedCornersTransform implements Transformation {
    private static Bitmap createRoundedRectBitmap(Bitmap bitmap,
                                                  float topLeftCorner, float topRightCorner,
                                                  float bottomRightCorner, float bottomLeftCorner) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = Color.WHITE;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        Path path = new Path();
        float[] radii = new float[]{
                topLeftCorner, bottomLeftCorner,
                topRightCorner, topRightCorner,
                bottomRightCorner, bottomRightCorner,
                bottomLeftCorner, bottomLeftCorner
        };

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        path.addRoundRect(rectF, radii, Path.Direction.CW);
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    @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();
        }

        float r = size / 4f;

        Bitmap roundedBitmap = createRoundedRectBitmap(squaredBitmap, r, r, r, r);

        squaredBitmap.recycle();

        return roundedBitmap;
    }

    @Override
    public String key() {
        return "rounded_corners";
    }
}

像这样使用:

Picasso.with(context).load(url).transform(new RoundedCornersTransform()).into(imageView);

可能需要一些增强功能,所以要小心!

答案 6 :(得分:1)

基于@stevyhacker的回答的科特林版RoundCornerTransform。

  • 支持矩形而不是正方形图像。
  • 不需要额外的第三方库。

=========================================== >

import android.graphics.*
import com.squareup.picasso.Transformation

class RoundCornersTransform(private val radiusInPx: Float) : Transformation {

    override fun transform(source: Bitmap): Bitmap {
        val bitmap = Bitmap.createBitmap(source.width, source.height, source.config)
        val canvas = Canvas(bitmap)
        val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
        val shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        paint.shader = shader
        val rect = RectF(0.0f, 0.0f, source.width.toFloat(), source.height.toFloat())
        canvas.drawRoundRect(rect, radiusInPx, radiusInPx, paint)
        source.recycle()

        return bitmap
    }

    override fun key(): String {
        return "round_corners"
    }

}

用法:

    Picasso.get()
        .load(imageUrl)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .placeholder(R.drawable.image_placeholder)
        .transform(RoundCornersTransform(32.0f))
        .into(imageView, callback)