【问题标题】:How to generate a uniform size circle bitmap from different size image如何从不同大小的图像生成统一大小的圆形位图
【发布时间】:2014-08-22 05:55:04
【问题描述】:

目前我正在使用此代码..

            public static Bitmap getCircularBitmap(Bitmap bitmap, int borderWidth) {
            if (bitmap == null || bitmap.isRecycled()) {
                return null;
            }

            int width = bitmap.getWidth() + borderWidth;
            int height = bitmap.getHeight() + borderWidth;

            Bitmap canvasBitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
            BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,  TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(shader);

            Canvas canvas = new Canvas(canvasBitmap);
            float radius = width > height ? ((float) height) / 2f: ((float) width) / 2f;
            canvas.drawCircle(width / 2, height / 2, radius, paint);
            paint.setShader(null);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.WHITE);
            paint.setStrokeWidth(borderWidth);
            canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2,  paint);
            return canvasBitmap;
        }

它返回一个圆形位图,但图像的大小会根据实际图像大小而有所不同。

应用程序的示例图像..

第一个个人资料图片比第二个小。

请帮帮我.. 谢谢。

【问题讨论】:

  • 保持radius的固定大小
  • 将你的代码在最后一次绘制 Circle radius - borderWidth / 2 更改为 radius + borderWidth / 2。
  • @Divyang Metalia 它不工作。
  • @Apoorv 我试图修复半径,但这也不起作用。
  • @Ankit 你能发布一张图片吗?你希望它是什么样子的

标签: android image-processing canvas bitmap


【解决方案1】:

问题是有些图像很小,有些图像很大,这就是为什么你的位图从方法中得到的结果有时大/小。

解决方案:

您需要做的是首先将您的图像重新调整为默认大小(例如 300x300),以便所有图像都具有相同的尺寸,并在重新调整大小后将圆圈绘制到画布上.

您可以使用此方法将位图重新调整为您想要的默认大小:

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
    int     width           = bm.getWidth();
    int     height          = bm.getHeight();
    float   scaleWidth      = ((float) newWidth) / width;
    float   scaleHeight     = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    return resizedBitmap;
}

并在您的 getCircularBitmap 方法中使用它:

public static Bitmap getCircularBitmap(Bitmap bitmap, int borderWidth) {
        if (bitmap == null || bitmap.isRecycled()) {
            return null;
        }
        Bitmap resizedBitmap = getResizedBitmap(bitmap, 300, 300); //pick you default size
     .
     .
     .

【讨论】:

  • 如果我将位图调整为固定大小,一些图像会拉伸和模糊,我该如何解决..
猜你喜欢
  • 2014-06-23
  • 2016-03-29
  • 1970-01-01
  • 2021-02-02
  • 2012-04-18
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多