Android 使用毕加索制作圆角的ImageView
我知道有很多链接可供Android 使用毕加索制作圆角的ImageView,android,imageview,picasso,Android,Imageview,Picasso,我知道有很多链接可供ImageView使用。 但是我正在使用毕加索的库来加载图像。。 我参考了这个结果。 但问题是,我在列表视图中使用它,对于列表视图的第一项图像视图,它工作得非常好,但是对于其余的转换,一旦转换不工作,它就工作得非常好。我使用这个转换: 你可以使用类库 例如: final int radius = 5; final int margin = 5; final Transformation transformation = new RoundedCornersTransform
ImageView
使用。
但是我正在使用毕加索的库来加载图像。。
我参考了这个结果。
但问题是,我在
列表视图中使用它,对于列表视图的第一项图像视图
,它工作得非常好,但是对于其余的转换,一旦转换不工作,它就工作得非常好。我使用这个转换:
你可以使用类库
例如:
final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius, margin);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
final Transformation transformation = new MaskTransformation(getContext(), R.drawable.rounded_convers_transformation);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
您可以使用这个类来创建圆角矩形图像视图,使用方法如下
Picasso.with(activity).load(url).transform(new RoundedCornersTransform(this)).into(imageView);
这是班级的圆形角落表格
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";
}
}
我使用了RoundedCornersTransformation
毕加索变换类
库。我的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(175300).into(viewHolder.ivImage)代码>
这将调整图像的大小并为图像提供圆角。下面是我的想法:
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);
不过可能需要一些增强功能,所以请小心 就像我说的那样。您可以使用库的MaskTransformation
类
例如:
final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius, margin);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
final Transformation transformation = new MaskTransformation(getContext(), R.drawable.rounded_convers_transformation);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
res/drawable/rounded\u convers\u 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>
更新:但请注意,您还应该。调整图像的大小(w,h)
,因为如果图像较大,则圆将无法确定基于@stevyhacker答案的Kotlin版本的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)
你试过这种转变吗?用法:毕加索.with(context).load(url).transform(new RoundedTransformation()).into(imageView)@RoiDivon我刚才使用了你提供的链接,但它也提供了相同的效果..太棒了!!!非常感谢…我只是替换了canvas.drawCircle(r,r,r,paint)使用canvas.drawRoundRect(新的RectF(0,0,source.getWidth(),source.getHeight()),r,r,paint)进行编码>
若要获得圆角矩形,您忘了说需要更改r以使其工作,否则它将从RoundRect生成圆,因为半径太大。而且两人都忘了说需要将r更改为0,并将size.getHeight()以使其工作(在我的例子中)。这是最后一段代码:canvas.drawRoundRect(新的RectF(0,0,source.getWidth(),source.getHeight()),0,source.getHeight(),paint)代码>这是所有关于圆角变换的帖子中的短n完美解决方案。非常感谢:)我如何使用这个@stevyhacker只使顶部的两个角变圆。。。我不明白我应该更改什么部分?@Lion789:在下面提到的示例中,可以精确地将要圆角的角设置为(mImageView),例如:毕加索.with(mContext).load(mUrl).resize(200200).centerCrop().transform(new RoundedCornersTransformation(2,0,RoundedCornersTransformation.CornerType.LEFT))@Lion789我得到了所有圆角,代码在里面。如果我们将android:scaleType=“centerCrop”
设置为ImageView
并且图像被缩放以填充视图,那么你的方法不会起任何作用,因为圆角不在视图中view@Choletski还可以使用CropTransformation(整数宽度、整数高度)之前RoundedCornersTransformation@Choletski,可能有帮助:Picasso.with(getContext()).load(uri).centerCrop().resize(width,height).transform(transformation).into(imageView)代码>。很好的解决方案。还请注意,在应用此转换之前,ImageView
必须可见(在其他情况下,不会应用任何转换)。是的,您可以编写Picasso.with(getContext()).load(uri).centerCrop().resize(width,height).transform(transformation).into(imageView)代码>用于裁剪图像。