圆裁剪后调整ImageView的大小

时间:2014-06-03 22:51:33

标签: android android-layout

我有一些矩形图像,我试图在没有失真的情况下裁剪成圆形的ImageView。我已经创建了一个自定义ImageView,这里是onDraw()方法

protected void onDraw(Canvas canvas) {
    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight(), radius = 0;

    if (w < h) {
        radius = w;
    } else {
        radius = h;
    }

    Bitmap roundBitmap = getCroppedBitmap(bitmap, radius);
    if (w < h) {
        canvas.drawBitmap(roundBitmap, 0, (float)(h-w)/2, null);
    } else {
        canvas.drawBitmap(roundBitmap, (float)(w-h)/2, 0, null);
    }
}

所有这一切,都非常适合裁剪位图,最短边作为我的半径。但是,布局中的实际图像视图是保持矩形大小,并根据图像方向使用额外的填充来拧紧我的布局的其余部分。

如果我的图像是40x80,我希望将图像裁剪为半径40,宽度缩小到40x40平方。 80x40相同。

我试图调用setScaleX或setScaleY,但它需要API 11,我现在需要支持回2.3。是否有另一种强制布局视图的方法。

0 个答案:

没有答案
相关问题