绘制带圆角的ImageView时的效率

时间:2012-08-09 18:16:24

标签: android graphics android-custom-view

我有一个ImageView子类,用于绘制带圆角的图像。该代码基于this answer,如下所示:

public class ImageViewRoundedCorners extends ImageView {
    ...
    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap scaledBitmap = Bitmap.createBitmap(getMeasuredWidth(),
                                                  getMeasuredHeight(),
                                                  Bitmap.Config.ARGB_8888); 
        Canvas scaledCanvas = new Canvas(scaledBitmap);
        super.onDraw(scaledCanvas); 
        drawRoundedCornerBitmap(canvas, scaledBitmap, 
                                getMeasuredWidth(), getMeasuredHeight());

        scaledBitmap.recycle();
    }

    protected void drawRoundedCornerBitmap(Canvas outputCanvas, Bitmap input, int w, int h) {
        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(mClipPath, mPaint);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(input, 0, 0, mPaint);

        outputCanvas.drawBitmap(output, 0, 0, null);
    }
}

使用此代码,可以使用正确的圆角绘制图像。为了避免在drawRoundedCornerBitmap的前两行上进行分配,我想直接绘制到outputCanvas,这是最初传递给onDraw的画布。新实现如下所示:

protected void drawRoundedCornerBitmap(...) {
    mPaint.reset();
    mPaint.setAntiAlias(true);
    outputCanvas.drawARGB(0, 0, 0, 0);

    mPaint.setStyle(Paint.Style.FILL);
    outputCanvas.drawPath(mClipPath, mPaint);

    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    outputCanvas.drawBitmap(input, 0, 0, mPaint);
}

出于某种原因,此代码似乎忽略了Porter-Duff模式,而只是使用普通(非圆角)角绘制图像。为什么会这样?绘制到使原始代码有效的中间Bitmap是什么?

1 个答案:

答案 0 :(得分:0)

创建一个可绘制的Romain Guy已经为您完成了这项工作。我们不是一个链接工厂,但他的博客文章相当广泛地解释了它并提供了一种有效的方法。 Rounded Corners

真正的基本原则是创建一个BitmapShader并将其附加到Paint对象,该对象以自定义Drawable方式绘制,只需将Drawable应用于ImageView {{1}}。

使用drawable意味着Image只绘制一次画布,这意味着绘制图像只进行一次,然后ImageView所做的只是缩放drawable。

相关问题