Bitmap repeat + rounded corners

前端 未结 2 1039
野性不改
野性不改 2020-12-19 06:14

I am trying to create rectangle with rounded corners and background as repeated bitmaps. I am writing like this, but getting bitmaps in the corners.

Could anyone hel

2条回答
  •  北海茫月
    2020-12-19 06:56

    I found this post by Romain Guy that's a much easier way to round corners on a tiled bitmap. Here's the short answer:

    class CurvedAndTiled extends Drawable {
    
        private final float mCornerRadius;
        private final RectF mRect = new RectF();
        private final BitmapShader mBitmapShader;
        private final Paint mTilePaint;        
    
        CurvedAndTiled(
                Bitmap bitmap, 
                float cornerRadius) {
            mCornerRadius = cornerRadius;
    
            mBitmapShader = new BitmapShader(bitmap,
                    Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    
            mTilePaint = new Paint();
            mTilePaint.setAntiAlias(true);
            mTilePaint.setShader(mBitmapShader);             
        }
    
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mRect.set(0, 0, bounds.width(), bounds.height());
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mTilePaint);           
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    
        @Override
        public void setAlpha(int alpha) {
            mTilePaint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            mTilePaint.setColorFilter(cf);
        }       
    }
    

    You just set your view's background drawable to be one of these guys and you're good to go.

提交回复
热议问题