1

在我的 ImageView 中,onDraw我正在努力将 Drawable 对象转换为位图(考虑缩放)。我正在加载一个 SVG 文件作为 PictureDrawable。然后我尝试使用 BitmapShader 将圆角应用于图像。为此,我必须将 Drawable 转换为位图。它基本上可以工作,但我并没有完全了解缩放过程。

Bitmap bitmap = Bitmap.createBitmap(
    picture.getIntrinsicWidth(),
    picture.getIntrinsicHeight(),
    Bitmap.Config.ARGB_8888
)

Canvas canvas = new Canvas( bitmap )
// Scaling the Canvas appears to work ...
canvas.concat( getImageMatrix() )
canvas.drawPicture(
    picture.getPicture,
    // ... however this will not fill the viewport, as the getWidth and getHeight
    // values do not reflect the scaling
    new RectF( 0, 0, canvas.getWidth(), canvas.getHeight() )
)

paint.setShader( new BitmapShader( bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP ) )
canvas.drawRoundRect(
    new RectF( 0, 0, bitmap.getWidth(), bitmap.getHeight() ),
    radius,
    radius,
    paint
)

centerCrop 缩放的错误渲染示例: 截屏

除了上面代码注释中描述的问题之外,我想知道是否可以使用诸如 clipPath 之类的绘图操作来屏蔽图片/SVG 文件,而不是这种繁重的位图转换。但当然,它必须是抗锯齿的。

该代码最初是用 Scala 编写的,并为 SO 松散地翻译成 Java,所以请忽略任何语法错误

4

1 回答 1

1

随着 pskink 对类似问题的回答(Make Image view rounded (not the image)),我基于 saveLayer 方法(稍作修改)得出了以下解决方案。

@Override
public void onDraw( Canvas canvas )
{
    Paint paint1 = new Paint( Paint.ANTI_ALIAS_FLAG )
    Paint paint2 = new Paint()
    paint2.setXfermode( new PorterDuffXfermode( Mode.SRC_IN ) )

    Drawable drawable = getDrawable()
    RectF rectangle = new RectF()
    rectangle.set( drawable.getBounds() )

    getImageMatrix.mapRect( rectangle )
    rectangle.offset( getPaddingLeft(), getPaddingTop() )

    // Prevent radius being drawn out of canvas bounds
    rectangle.intersect( new RectF( 0, 0, canvas.getWidth(), canvas.getHeight() ) )

    int restore = canvas.saveLayer( rectangle, null, Canvas.ALL_SAVE_FLAG )
    canvas.drawRoundRect( rectangle, radius.getValue(), radius.getValue(), paint1 )
    canvas.saveLayer( rectangle, paint2, Canvas.ALL_SAVE_FLAG )
    super.onDraw( canvas )
    canvas.restoreToCount( restore )
}

上面的代码忽略了类级别的对象缓存,并忽略了来自 getDrawable() 的 NPE。

于 2014-10-26T21:22:43.463 回答