我使用的是https://github.com/hdodenhof/CircleImageView图书馆,但也尝试了其他几个图书馆并没有帮助。我需要使用picasso从服务器加载图像,例如mPicasso.load(url).into(mCircularImageView)
。但是我总是得到这个:
但我需要实现这个目标:
试图致电resize(100,100)
,fit()
,transform(new CircleTransorm())
,
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";
}
}
上面没有任何帮助。
我的布局:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/account_profile_picture"
style="@style/ProfilePicture"
android:layout_gravity="center_horizontal"/>
风格:
<style name="ProfilePicture">
<item name="android:layout_width">@dimen/profile_picture_size</item>
<item name="android:layout_height">@dimen/profile_picture_size</item>
<item name="android:src">@drawable/profile_picture_placeholder</item>
<item name="civ_border_width">1dp</item>
<item name="civ_border_color">#FF000000</item>
</style>
答案 0 :(得分:0)
白色fit()
推迟调用,直到加载并测量视图,它无法达到您想要的结果。您需要添加centerCrop()
才能获得预期的输出:
mPicasso.load(url).fit().centerCrop().into(mCircularImageView)
答案 1 :(得分:0)
只需将此属性设置为circleimageview
即可android:scaleType="centerCrop"
这应该这样做。
谢谢
答案 2 :(得分:0)
请使用此功能,您可以创建动态imageview,希望这对您有所帮助。
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
答案 3 :(得分:0)
您无需为圆形图像视图添加任何库。 Android api已经提供
用于此目的。我使用Glide库来做你想做的事 -
Glide.with(context)
.load(imageUrl)
.asBitmap()
.placeholder(R.color.gray)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.into(new BitmapImageViewTarget(imageView){
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
imageView.setImageResource(R.drawable.profile);
}
});
答案 4 :(得分:0)
我在您的代码中看到的是您正在使用圆形变换和圆形图像视图。对于制作圆圈你只需要使用一种方法,我建议使用圆形变换也可以将圆形图像视图更改为简单的图像视图。
WinForms
希望它会有所帮助。