【问题标题】:How to Make an ImageView in Circular Shape? [duplicate]如何制作圆形的 ImageView? [复制]
【发布时间】:2013-08-25 01:59:47
【问题描述】:

我想做这样的事情。

这是一个包含用户名和图像的列表视图行。

我进行了一些搜索并完成了图像循环,但不是完美的解决方案。 任何帮助都会帮助我。

我的代码添加到 Image Loader 类中

public Bitmap processBitmap(Bitmap bitmap) {
    int pixels = 0;
    if (mRound == 0)
        pixels = 120;
    else
        pixels = mRound;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

谢谢。

【问题讨论】:

标签: android imageview


【解决方案1】:

你也可以使用这个功能......它对我有用..

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        (Math.min(((float) targetWidth), 
        ((float) targetHeight)) / 2),
        Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
        new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
        new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

【讨论】:

  • 如何增加圆形。
  • 如何添加白边??
  • 这工作正常,但我得到了圆形图像的黑色矩形背景
  • 我得到了圆形图像....但表面不光滑...谁能告诉我解决方案?
【解决方案2】:

这可能无法回答您的问题,但是,作为替代方案,您可以通过在 FrameLayout 中使用 2 个 ImageView 来模仿这种效果,例如:一个在底部 - 这将是图片,一个在顶部 -这将是一个中间有一个圆圈的灰色正方形,圆的“主体”是透明的。

这样您就不必进行任何位图处理。但是,请选择更适合您需求的那一款。

【讨论】:

  • 它有效,请参阅我的回答。
  • 使用此解决方案,您需要知道背景颜色,并且它必须是没有渐变或任何东西的唯一颜色。
  • 有关如何执行此操作的示例,请参见以下链接。 techrepublic.com/article/…
【解决方案3】:

感谢回复。

Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setStrokeWidth(5);
Canvas c = new Canvas(circleBitmap);
 //This draw a circle of Gerycolor which will be the border of image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setAntiAlias(true);
paint.setShader(shader);
// This will draw the image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2-2, paint);

使用 imageLoader 的圆形图像 github.com/nostra13/Android-Universal-Image-Loader 创建一个选项;

 DisplayImageOptions options = new DisplayImageOptions.Builder()
 // this will make circle, pass the width of image 
.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_‌​menu))) 
.cacheOnDisc(true) 
.build(); 
imageLoader.displayImage(url_for_image,ImageView,options);

或者你可以从 squre 使用毕加索图书馆。

Picasso.with(mContext).load(URL+b.image)
 .placeholder(R.drawable.profile) 
    .error(R.drawable.profile) 
    .transform(new RoundedTransformation(50, 4)) 
    .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) 
    .centerCrop() 
    .into(v.im_user);

你可以下载RoundedTrasformationfile here

重要提示: 我在使用 UIL 时发现了一个问题。如果你没有把 xml 属性 android:contentDescription="TODO" 放在 ImageView 中。然后 UIL 显示简单的图像。 希望大家理解

【讨论】:

  • 圆角图像使用imageLoader github.com/nostra13/Android-Universal-Image-Loader 创建一个选项; DisplayImageOptions options = new DisplayImageOptions.Builder() // 这将制作圆形,传递图像的宽度 .displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_menu))) .cacheOnDisc(true) .build() ; imageLoader.displayImage(url_for_image,ImageView,options);
  • 或者你可以从 squre 使用毕加索图书馆。 Picasso.with(mContext) .load(com.app.utility.Constants.BASE_URL+b.image) .placeholder(R.drawable.profile) .error(R.drawable.profile) .transform(new RoundedTransformation(50, 4 )) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerCrop() .into(v.im_user);你可以在这里下载 RoundedTrasformation 文件dropbox.com/s/lp3d43hra3gbhul/RoundedTransformation.java
  • @GalRom 这个工作就像一个魅力。谢谢你。
猜你喜欢
  • 1970-01-01
  • 2014-10-05
  • 2013-04-18
  • 2019-12-07
  • 2011-01-28
  • 2016-08-13
  • 1970-01-01
  • 2010-12-25
  • 2014-07-17
相关资源
最近更新 更多