android – 如何使用中心裁剪创建部分圆角矩形可绘制而不创建新位图?

前端之家收集整理的这篇文章主要介绍了android – 如何使用中心裁剪创建部分圆角矩形可绘制而不创建新位图?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景

我已经看过如何用位图创建一个圆形的drawable,以及如何在它周围添加一个轮廓(AKA笔划),here.

问题

我无法找到如何在不绘制新位图的情况下仅对位图的某些角进行舍入,而不是创建新的位图,并且仍然可以用于中心裁剪ImageView.

我发现了什么

这是我发现的,但它确实创建了一个新的位图,并在带有center-crop的imageView中使用它(源here):

/**
 * Create rounded corner bitmap from original bitmap.
 *
 * @param input                               Original bitmap.
 * @param cornerRadius                        Corner radius in pixel.
 * @param squareTL,squareTR,squareBL,squareBR where to use square corners instead of rounded ones.
 */
public static Bitmap getRoundedCornerBitmap(final Bitmap input,final float cornerRadius,final int w,final int h,final boolean squareTL,final boolean squareTR,final boolean squareBL,final boolean squareBR) {
    final Bitmap output = Bitmap.createBitmap(w,h,Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Rect rect = new Rect(0,w,h);
    final RectF rectF = new RectF(rect);
    // make sure that our rounded corner is scaled appropriately
    Paint paint = new Paint();
    paint.setXfermode(null);
    paint.setAntiAlias(true);
    canvas.drawARGB(0,0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF,cornerRadius,paint);
    // draw rectangles over the corners we want to be square
    if (squareTL) 
        canvas.drawRect(0,w / 2,h / 2,paint);
    if (squareTR) 
        canvas.drawRect(w / 2,paint);
    if (squareBL) 
        canvas.drawRect(0,paint);
    if (squareBR) 
        canvas.drawRect(w / 2,paint);
    paint.setXfermode(PORTER_DUFF_XFERMODE_SRC_IN);
    canvas.drawBitmap(input,paint);
    return output;
}

而且,这是我发现创建一个可以在各个角落起作用的圆角可绘制的东西:

public static class RoundedCornersDrawable extends Drawable {
    private final float mCornerRadius;
    private final RectF mRect = new RectF();
    private final BitmapShader mBitmapShader;
    private final Paint mPaint;

    public RoundedCornersDrawable(final Bitmap bitmap,final float cornerRadius) {
        mCornerRadius = cornerRadius;
        mBitmapShader = new BitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(false);
        mPaint.setShader(mBitmapShader);
        mRect.set(0,bitmap.getWidth(),bitmap.getHeight());
    }

    @Override
    protected void onBoundsChange(final Rect bounds) {
        super.onBoundsChange(bounds);
        mRect.set(0,bounds.width(),bounds.height());
    }

    @Override
    public void draw(final Canvas canvas) {
        canvas.drawRoundRect(mRect,mCornerRadius,mPaint);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(final int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(final ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }
}

但是,如果imageView显示内容,同时保持与位图相同的宽高比,并且其大小也是预先确定的,则此解决方案才有效.

这个问题

如何创建一个中心裁剪的drawable,它显示一个位图,具有特定角落的圆角,并且还能够显示它周围的轮廓/笔划?

我想这样做而不创建新的位图或扩展ImageView.仅使用具有位图作为输入的drawable.

解决方法

SMART方式是使用 PorterDuff混合模式.创建任何花哨的阴影,混合,“裁剪”效果都非常简单和光滑.你可以找到很多关于PorterDuff的好教程.这里是 good one.
原文链接:https://www.f2er.com/android/309099.html

猜你在找的Android相关文章