【问题标题】:Draw cirble bitmap in onDraw() ImageView without creating another bitmap在 onDraw() ImageView 中绘制圆形位图而不创建另一个位图
【发布时间】:2015-06-10 07:40:04
【问题描述】:

我在解码位图时使用options.inPurgeable将位图存储在Ashmem中。所以我不想在onDraw() 方法中绘制圆形位图时在Java heap 上创建更多位图(导致大量GC)。我的代码在下面,但它不起作用

private void init() {

        mRoundPaint = new Paint();
        mRoundPaint.setColor(Color.RED);
        mRoundPaint.setStyle(Paint.Style.FILL);
        mRoundPaint.setAntiAlias(true);
//      mRoundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        mBmPaint = new Paint();
        mBmPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    }

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = Math.min(w, h);
        rectDes = new Rect(0, 0, mWidth, mWidth);
    }

protected void onDraw(Canvas canvas) {

//      super.onDraw(canvas);

        if(getDrawable() == null)
            return;

        // Option 1
        Drawable drawable = getDrawable();
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mRoundPaint);
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        canvas.drawBitmap(bitmap, null, rectDes, mBmPaint);


        // Option 2
//      Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
//      canvas.drawBitmap(bitmap, null, rectDes, mBmPaint);
//      canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mRoundPaint);
}

结果如下:

我的期望

我也使用了 BitmapShader,它很有效。但我不知道为什么 BitmapShader 将我的位图保留在 Ashmem 中,GC 不会删除。

BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mWidth / 2, mWidth/ 2, mWidth /2, paint);

感谢您的所有帮助。

编辑抱歉,我忘了显示我想要的位图。

【问题讨论】:

  • 你期待什么?位图上的圆圈?
  • 抱歉,我编辑了我的问题。感谢您的宝贵时间

标签: android bitmap android-bitmap


【解决方案1】:

嗯,如果我没有误会,你想从普通图像中获取圆角位图吗?这是我的功能,您可以尝试更改任何您喜欢的内容:

/**
 * Create rounded corner bitmap from given bitmap. The given bitmap will be
 * recycle after createing round image.
 * 
 * @param bitmap
 *            in {@link Bitmap} to parse
 * @param corner
 *            in {@link Integer} the corner of round rect image
 * @return rounded corner bitmap in {@link Bitmap}
 */
public static Bitmap parseBitmapToRoundedBitmap(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int size = Math.min(width, height);
    Bitmap output = Bitmap.createBitmap(size, size, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    // Rect of bitmap
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    // This is how we draw round rect image or circle image
    // Round rect image
    // canvas.drawRoundRect(new RectF(rect), roundPx, roundPx, paint);
    // Circle image
    final float radius = size / 2;
    canvas.drawCircle(size / 2, size / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect srcRect = new Rect(width / 2 - size / 2, height / 2 - size / 2,
            size, size);
    canvas.drawBitmap(bitmap, srcRect, new Rect(0, 0, size, size), paint);
    // Recycle bitmap if need.
    if (hasHoneycomb()) {
        bitmap = null;
    } else {
        bitmap.recycle();
        bitmap = null;
    }
    return output;
}

【讨论】:

  • 我不想再创建一个位图。这是我的条件。每当您创建位图时,都会导致许多 GC。只想在 onDraw() 方法中画一个圆形位图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多