2012-08-09 31 views
6

我有一个ImageView子类,用于绘制带圆角的图像。该代码是基于this answer,并且如下:使用圆角绘制ImageView时的效率

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

出于某种原因,该代码似乎忽视了波特 - 达夫模式,而不是仅仅绘制图像与正常(非圆)的角落。为什么会这样?什么是绘制到中间Bitmap,使原来的代码工作?

+0

我也有同样的问题。 – 2012-08-14 07:16:58

+0

你是否找到任何解决方案......甚至我想尝试同样的事情:( – 2013-01-11 18:51:26

回答

0

创建一个可绘制的罗曼盖伊为你做了这个。我们不是链接工厂,但他的博客文章对此进行了相当广泛的解释,并提供了一种有效的方法。 Rounded Corners

真正的基本原则,是建立一个BitmapShader并将其连接到一个Paint对象,绘制一个自定义Drawable你只是应用该DrawableImageView的方式。

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

+0

当你不得不在图像视图中缩放drawable时,这种痛苦不是这样吗?drawable被创建,然后被拉伸或裁剪,这意味着你的角落看起来很奇怪,不是吗? – secureboot 2013-10-08 16:37:58

+0

记住'ImageView'会将你传递给它的任何东西变成一个'Drawable',提供它的大小,然后调用Drawable的'draw()'。你有一个BitmapDrawable,它当然会拉伸它,因为你传递了一个'RoundedBitmapDrawable',它会根据那个实现来绘制。 – 2013-10-09 08:51:22