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

Aks*_*hay 34 android imageview picasso

我知道有很多链接可以制作ImageView圆角.但我正在使用Picasso库进行图像加载..我引用链接获取结果.但问题是我正在使用它ListView并且对于第LIstView's一个项目ImageView它的工作完全正常但是对于剩下的一次转换不起作用.

das*_*sar 48

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

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

  • 高超!!!非常感谢...我只是用`canvas.drawRoundRect(new RectF(0,0,source.getWidth(),source.getHeight())替换`canvas.drawCircle(r,r,r,paint);`, r,r,paint);`圆角矩形 (3认同)
  • 你忘了说 r 需要改变才能工作,否则它会从 RoundRect 制作圆,因为半径太大。 (2认同)

STB*_*and 31

您可以使用RoundedCornersTransformation类的picasso-transformations库.

示例:

final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius, margin);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
Run Code Online (Sandbox Code Playgroud)

  • 如果我们将`android:scaleType ="centerCrop"`设置为`ImageView`并且图像被缩放以填充视图,那么您的方法不会产生任何效果,因为圆角不在视野范围内 (13认同)

ste*_*ker 25

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

 Picasso.with(activity).load(url).transform(new RoundedCornersTransform(this)).into(imageView);
Run Code Online (Sandbox Code Playgroud)

这是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";
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我如何使用这个@stevyhacker使前两个角落四舍五入...我无法理解我想改变哪个部分? (2认同)
  • @ Lion789:在[picasso-transformations library]中(https://github.com/wasabeef/picasso-transformations/blob/master/transformations/src/main/java/jp/wasabeef/picasso/transformations/RoundedCornersTransformation.java)在下面提到,可以精确设置你想要舍入的角落,例如:Picasso .with(mContext).load(mUrl).resize(200,200).centerCrop().transnsform(new RoundedCornersTransformation(2,0, RoundedCornersTransformation.CornerType.LEFT)).into(mImageView); (2认同)

小智 8

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

dependencies {
        compile 'jp.wasabeef:picasso-transformations:2.1.0'
} 
Run Code Online (Sandbox Code Playgroud)

在我customArrayAdapter.java,我补充说:

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


Nic*_*hek 6

就像这里说的。您可以使用MaskTransformation类的毕加索转换库。

范例:

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

res / drawable / 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>
Run Code Online (Sandbox Code Playgroud)

更新:但是请注意,您也应该.resize(w,h)使用图像,因为如果图像较大,则回合将无法确定


Cha*_*ler 6

基于 @stevyhacker 的回答的 RoundCornerTransform 的 Kotlin 版本。

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

===============================================

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"
    }

}
Run Code Online (Sandbox Code Playgroud)

用法:

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