Search in sources :

Example 66 with BitmapShader

use of android.graphics.BitmapShader in project Klyph by jonathangerbaud.

the class BitmapUtil method getCirleBitmap.

public static Bitmap getCirleBitmap(Bitmap bitmap, int borderColor, int border) {
    if (bitmap == null)
        return null;
    int side = (int) Math.min(bitmap.getWidth(), bitmap.getHeight());
    int x = (int) (bitmap.getWidth() - side) / 2;
    int y = (int) (bitmap.getHeight() - side) / 2;
    Bitmap b = Bitmap.createBitmap(bitmap, x, y, side, side, new Matrix(), true);
    BitmapShader bitmapShader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(bitmapShader);
    Paint bPaint = new Paint();
    bPaint.setAntiAlias(true);
    bPaint.setColor(borderColor);
    bPaint.setStrokeWidth(border / 2);
    bPaint.setStyle(Style.STROKE);
    Bitmap returnBitmap;
    try {
        returnBitmap = Bitmap.createBitmap(side, side, Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        return null;
    }
    Canvas canvas = new Canvas(returnBitmap);
    float center = (float) side / 2;
    float radius = (float) (side - border / 2) / 2;
    canvas.drawCircle(center, center, center, paint);
    canvas.drawCircle(center, center, radius, bPaint);
    return returnBitmap;
}
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 67 with BitmapShader

use of android.graphics.BitmapShader in project ImagePicker by jeasonlzy.

the class CropImageView method makeCropBitmap.

/**
     * @param bitmap          需要裁剪的图片
     * @param focusRect       中间需要裁剪的矩形区域
     * @param imageMatrixRect 当前图片在屏幕上的显示矩形区域
     * @param expectWidth     希望获得的图片宽度,如果图片宽度不足时,拉伸图片
     * @param exceptHeight    希望获得的图片高度,如果图片高度不足时,拉伸图片
     * @param isSaveRectangle 是否希望按矩形区域保存图片
     * @return 裁剪后的图片的Bitmap
     */
private Bitmap makeCropBitmap(Bitmap bitmap, RectF focusRect, RectF imageMatrixRect, int expectWidth, int exceptHeight, boolean isSaveRectangle) {
    float scale = imageMatrixRect.width() / bitmap.getWidth();
    int left = (int) ((focusRect.left - imageMatrixRect.left) / scale);
    int top = (int) ((focusRect.top - imageMatrixRect.top) / scale);
    int width = (int) (focusRect.width() / scale);
    int height = (int) (focusRect.height() / scale);
    if (left < 0)
        left = 0;
    if (top < 0)
        top = 0;
    if (left + width > bitmap.getWidth())
        width = bitmap.getWidth() - left;
    if (top + height > bitmap.getHeight())
        height = bitmap.getHeight() - top;
    try {
        bitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
        if (expectWidth != width || exceptHeight != height) {
            bitmap = Bitmap.createScaledBitmap(bitmap, expectWidth, exceptHeight, true);
            if (mStyle == CropImageView.Style.CIRCLE && !isSaveRectangle) {
                //如果是圆形,就将图片裁剪成圆的
                int length = Math.min(expectWidth, exceptHeight);
                int radius = length / 2;
                Bitmap circleBitmap = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(circleBitmap);
                BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                Paint paint = new Paint();
                paint.setShader(bitmapShader);
                canvas.drawCircle(expectWidth / 2f, exceptHeight / 2f, radius, paint);
                bitmap = circleBitmap;
            }
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 68 with BitmapShader

use of android.graphics.BitmapShader in project fresco by facebook.

the class RoundedBitmapDrawable method updatePaint.

private void updatePaint() {
    Bitmap bitmap = getBitmap();
    if (mLastBitmap == null || mLastBitmap.get() != bitmap) {
        mLastBitmap = new WeakReference<Bitmap>(bitmap);
        mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        mIsShaderTransformDirty = true;
    }
    if (mIsShaderTransformDirty) {
        mPaint.getShader().setLocalMatrix(mTransform);
        mIsShaderTransformDirty = false;
    }
}
Also used : Bitmap(android.graphics.Bitmap) BitmapShader(android.graphics.BitmapShader)

Example 69 with BitmapShader

use of android.graphics.BitmapShader in project InstaMaterial by frogermcs.

the class CircleTransformation method transform.

@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }
    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
    Canvas canvas = new Canvas(bitmap);
    Paint avatarPaint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    avatarPaint.setShader(shader);
    Paint outlinePaint = new Paint();
    outlinePaint.setColor(Color.WHITE);
    outlinePaint.setStyle(Paint.Style.STROKE);
    outlinePaint.setStrokeWidth(STROKE_WIDTH);
    outlinePaint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, avatarPaint);
    canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint);
    squaredBitmap.recycle();
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 70 with BitmapShader

use of android.graphics.BitmapShader in project nmid-headline by miao1007.

the class CircleTransformation method transform.

@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }
    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);
    float r = size / 2f;
    // Prepare the background
    Paint paintBg = new Paint();
    paintBg.setColor(BORDER_COLOR);
    paintBg.setAntiAlias(true);
    // Draw the background circle
    canvas.drawCircle(r, r, r, paintBg);
    // Draw the image smaller than the background so a little border will be seen
    canvas.drawCircle(r, r, r - BORDER_RADIUS, paint);
    squaredBitmap.recycle();
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

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