【问题标题】:Bitmap not circle properly位图未正确圈出
【发布时间】:2015-04-30 08:21:38
【问题描述】:

我试图做位图圆圈它可以工作,但高分辨率图像不能正常工作。它在图像视图中像小圆圈一样工作。

我试过这段代码,也试过很多其他代码,但都不能正常工作

public static Bitmap circleShape(Bitmap bitmap) {

       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());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                bitmap.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
        //return _bmp;
        return output;
}

【问题讨论】:

  • 它很模糊,我测试过
  • 发布您的输出。上一条注释中的代码将图像下采样到 50*50,targetWidthtargetHeight 指定如何对原始图像进行采样。

标签: android bitmap imageview


【解决方案1】:

我尝试了很多代码,但都不能完美运行。我已经搜索并找到了此代码。它工作得很棒,太棒了..希望有人对这段代码有帮助..谢谢

public static Bitmap circleShape(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;

}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2019-08-02
    • 2023-03-06
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多