用圆angular使用picasso制作ImageView

我知道有很多链接可用于制作ImageView圆angular。 但是,我正在使用图像加载Picasso图书馆..我参考链接获得结果。 但问题是我在ListView使用它和LIstView's第一个项目ImageView的工作完全正常,但剩下的一次转换不起作用。

Solutions Collecting From Web of "用圆angular使用picasso制作ImageView"

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

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

你可以使用这个类来做毕加索的圆angular矩形图像视图,像这样使用它

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

您可以使用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); 

我使用了RoundedCornersTransformation类的picasso-transformations库。 我有我的列表视图中的视图持有人模式的自定义适配器。 我在我的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);
这将resize,并给你的图像圆angular。

在@ stevyhacker的答案和这个相关的一个 ,我想出了这个:

 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 -Transformations库的MaskTransformation类。

例如:

 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)的形象,因为如果图像将是大的一轮将不可确定