Search in sources :

Example 46 with BitmapShader

use of android.graphics.BitmapShader in project glide-transformations by wasabeef.

the class CropCircleTransformation method transform.

@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();
    int size = Math.min(source.getWidth(), source.getHeight());
    int width = (source.getWidth() - size) / 2;
    int height = (source.getHeight() - size) / 2;
    Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    if (width != 0 || height != 0) {
        // source isn't square, move viewport to center
        Matrix matrix = new Matrix();
        matrix.setTranslate(-width, -height);
        shader.setLocalMatrix(matrix);
    }
    paint.setShader(shader);
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    return BitmapResource.obtain(bitmap, mBitmapPool);
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 47 with BitmapShader

use of android.graphics.BitmapShader in project glide-transformations by wasabeef.

the class RoundedCornersTransformation method transform.

@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();
    int width = source.getWidth();
    int height = source.getHeight();
    Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    drawRoundRect(canvas, paint, width, height);
    return BitmapResource.obtain(bitmap, mBitmapPool);
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 48 with BitmapShader

use of android.graphics.BitmapShader in project RoundedImageView by vinc3m1.

the class RoundedDrawable method draw.

@Override
public void draw(@NonNull Canvas canvas) {
    if (mRebuildShader) {
        BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY);
        if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) {
            bitmapShader.setLocalMatrix(mShaderMatrix);
        }
        mBitmapPaint.setShader(bitmapShader);
        mRebuildShader = false;
    }
    if (mOval) {
        if (mBorderWidth > 0) {
            canvas.drawOval(mDrawableRect, mBitmapPaint);
            canvas.drawOval(mBorderRect, mBorderPaint);
        } else {
            canvas.drawOval(mDrawableRect, mBitmapPaint);
        }
    } else {
        if (any(mCornersRounded)) {
            float radius = mCornerRadius;
            if (mBorderWidth > 0) {
                canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
                canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint);
                redrawBitmapForSquareCorners(canvas);
                redrawBorderForSquareCorners(canvas);
            } else {
                canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
                redrawBitmapForSquareCorners(canvas);
            }
        } else {
            canvas.drawRect(mDrawableRect, mBitmapPaint);
            if (mBorderWidth > 0) {
                canvas.drawRect(mBorderRect, mBorderPaint);
            }
        }
    }
}
Also used : BitmapShader(android.graphics.BitmapShader)

Example 49 with BitmapShader

use of android.graphics.BitmapShader in project BookReader by JustWayward.

the class GlideCircleTransform method circleCrop.

private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null)
        return null;
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    // TODO this could be acquired from the pool too
    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    return result;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 50 with BitmapShader

use of android.graphics.BitmapShader in project android-shape-imageview by siyamed.

the class ShaderHelper method createShader.

protected void createShader() {
    Bitmap bitmap = calculateDrawableSizes();
    if (bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
        shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        imagePaint.setShader(shader);
    }
}
Also used : Bitmap(android.graphics.Bitmap) BitmapShader(android.graphics.BitmapShader)

Aggregations

BitmapShader (android.graphics.BitmapShader)137 Bitmap (android.graphics.Bitmap)86 Paint (android.graphics.Paint)69 Canvas (android.graphics.Canvas)60 Matrix (android.graphics.Matrix)20 BitmapDrawable (android.graphics.drawable.BitmapDrawable)15 RectF (android.graphics.RectF)14 ShapeDrawable (android.graphics.drawable.ShapeDrawable)14 AnimationDrawable (android.graphics.drawable.AnimationDrawable)13 ClipDrawable (android.graphics.drawable.ClipDrawable)13 Drawable (android.graphics.drawable.Drawable)13 LayerDrawable (android.graphics.drawable.LayerDrawable)13 Rect (android.graphics.Rect)9 Shader (android.graphics.Shader)8 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)6 StateListDrawable (android.graphics.drawable.StateListDrawable)4 Point (android.graphics.Point)3 SuppressLint (android.annotation.SuppressLint)2 ColorMatrix (android.graphics.ColorMatrix)2 TypedArray (android.content.res.TypedArray)1