Example usage for android.graphics BitmapShader setLocalMatrix

List of usage examples for android.graphics BitmapShader setLocalMatrix

Introduction

In this page you can find the example usage for android.graphics BitmapShader setLocalMatrix.

Prototype

public void setLocalMatrix(@Nullable Matrix localM) 

Source Link

Document

Set the shader's local matrix.

Usage

From source file:com.camnter.easyrecyclerviewsidebar.EasyRecyclerViewSidebar.java

private void setPaintShader(@NonNull EasyImageSection imageSection) {
    Drawable drawable;//from  ww  w  .j  av a 2s. c  o m
    if (imageSection.drawable == null && imageSection.resId >= 0) {
        drawable = ResourcesCompat.getDrawable(this.getResources(), imageSection.resId, null);
    } else {
        drawable = imageSection.drawable;
    }
    if (drawable == null)
        return;

    Bitmap bitmap = this.drawableToBitmap(drawable);
    BitmapShader sectionBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    float scale;
    scale = Math.max(this.letterSize * 1.0f / bitmap.getWidth(), this.letterSize * 1.0f / bitmap.getHeight());
    this.imageSectionMatrix.setScale(scale, scale);
    sectionBitmapShader.setLocalMatrix(this.imageSectionMatrix);
    Paint imagePaint = this.imagePaints.get(imageSection.hashCode());
    imagePaint.setShader(sectionBitmapShader);
}

From source file:github.madmarty.madsonic.util.ImageLoader.java

private Bitmap createReflection(Bitmap originalImage) {

    //   int reflectionH = 80;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // Height of reflection
    int reflectionHeight = height / 2;

    // The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectionHeight),
            Bitmap.Config.ARGB_8888);//  w w w  .  j ava2 s  .  c  om

    //// ----

    Bitmap reflection = Bitmap.createBitmap(width, reflectionHeight, Bitmap.Config.ARGB_8888);
    Bitmap blurryBitmap = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, height,
            reflectionHeight);

    // cheap and easy scaling algorithm; down-scale it, then
    // upscale it. The filtering during the scale operations
    // will blur the resulting image
    blurryBitmap = Bitmap
            .createScaledBitmap(
                    Bitmap.createScaledBitmap(blurryBitmap, blurryBitmap.getWidth() / 2,
                            blurryBitmap.getHeight() / 2, true),
                    blurryBitmap.getWidth(), blurryBitmap.getHeight(), true);

    // This shadier will hold a cropped, inverted,
    // blurry version of the original image
    BitmapShader bitmapShader = new BitmapShader(blurryBitmap, TileMode.CLAMP, TileMode.CLAMP);
    Matrix invertMatrix = new Matrix();
    invertMatrix.setScale(1f, -1f);
    invertMatrix.preTranslate(0, -reflectionHeight);
    bitmapShader.setLocalMatrix(invertMatrix);

    // This shader holds an alpha gradient
    Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionHeight, 0x80ffffff, 0x00000000,
            TileMode.CLAMP);

    // This shader combines the previous two, resulting in a
    // blurred, fading reflection
    ComposeShader compositor = new ComposeShader(bitmapShader, alphaGradient, PorterDuff.Mode.DST_IN);

    Paint reflectionPaint = new Paint();
    reflectionPaint.setShader(compositor);

    // Draw the reflection into the bitmap that we will return
    Canvas canvas = new Canvas(reflection);
    canvas.drawRect(0, 0, reflection.getWidth(), reflection.getHeight(), reflectionPaint);

    /// -----

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas finalcanvas = new Canvas(bitmapWithReflection);

    // Draw in the original image
    finalcanvas.drawBitmap(originalImage, 0, 0, null);

    // Draw in the gap
    Paint defaultPaint = new Paint();

    // transparent gap
    defaultPaint.setColor(0);

    finalcanvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

    // Draw in the reflection
    finalcanvas.drawBitmap(reflection, 0, height + reflectionGap, null);

    return bitmapWithReflection;
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 */// w  ww  .  j  a  va2 s .c o m
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                mBitmapPaint);
    } else {
        final float cornerRadius = getCalculatedCornerRadius();
        canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
    }
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 *///  ww w  .  j av a 2s.  co m
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (mBorderEnabled) {
        final float inset = mBorderPaint.getStrokeWidth() / 2;
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - inset / 2;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            dest.inset(inset, inset);
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
            dest.inset(-inset, -inset);
        }
    } else {
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
        }
    }

}