Search in sources :

Example 76 with Canvas

use of android.graphics.Canvas in project AndroidPicker by gzu-liyujiang.

the class ConvertUtils method toBitmap.

/**
     * 将Drawable转换为Bitmap
     * 参考:http://kylines.iteye.com/blog/1660184
     */
public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof ColorDrawable) {
        //color
        Bitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(((ColorDrawable) drawable).getColor());
        return bitmap;
    } else if (drawable instanceof NinePatchDrawable) {
        //.9.png
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) Canvas(android.graphics.Canvas) BitmapDrawable(android.graphics.drawable.BitmapDrawable) NinePatchDrawable(android.graphics.drawable.NinePatchDrawable)

Example 77 with Canvas

use of android.graphics.Canvas in project grafika by google.

the class MultiSurfaceActivity method drawBouncingCircle.

/**
     * Clears the surface, then draws a filled circle with a shadow.
     * <p>
     * Similar to drawCircleSurface(), but the position changes based on the value of "i".
     */
private void drawBouncingCircle(Surface surface, int i) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.FILL);
    Canvas canvas = surface.lockCanvas(null);
    try {
        Trace.beginSection("drawBouncingCircle");
        Trace.beginSection("drawColor");
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        // drawColor
        Trace.endSection();
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int radius, x, y;
        if (width < height) {
            // portrait
            radius = width / 4;
            x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
            y = height * 3 / 4;
        } else {
            // landscape
            radius = height / 4;
            x = width * 3 / 4;
            y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
        }
        paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
        canvas.drawCircle(x, y, radius, paint);
        // drawBouncingCircle
        Trace.endSection();
    } finally {
        surface.unlockCanvasAndPost(canvas);
    }
}
Also used : Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 78 with Canvas

use of android.graphics.Canvas in project material-components-android by material-components.

the class CollapsingTextHelper method ensureExpandedTexture.

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;
    }
    calculateOffsets(0f);
    mTextureAscent = mTextPaint.ascent();
    mTextureDescent = mTextPaint.descent();
    final int w = Math.round(mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()));
    final int h = Math.round(mTextureDescent - mTextureAscent);
    if (w <= 0 || h <= 0) {
        // If the width or height are 0, return
        return;
    }
    mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(mExpandedTitleTexture);
    c.drawText(mTextToDraw, 0, mTextToDraw.length(), 0, h - mTextPaint.descent(), mTextPaint);
    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}
Also used : Canvas(android.graphics.Canvas) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 79 with Canvas

use of android.graphics.Canvas in project material-components-android by material-components.

the class TestUtils method assertAllPixelsOfColor.

public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color, Rect checkArea, int allowedComponentVariance, boolean throwExceptionIfFails) {
    // Create a bitmap
    Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888);
    // Create a canvas that wraps the bitmap
    Canvas canvas = new Canvas(bitmap);
    if (callSetBounds) {
        // Configure the drawable to have bounds that match the passed size
        drawable.setBounds(0, 0, drawableWidth, drawableHeight);
    }
    // And ask the drawable to draw itself to the canvas / bitmap
    drawable.draw(canvas);
    try {
        int[] rowPixels = new int[drawableWidth];
        final int firstRow = checkArea != null ? checkArea.top : 0;
        final int lastRow = checkArea != null ? checkArea.bottom : drawableHeight - 1;
        final int firstCol = checkArea != null ? checkArea.left : 0;
        final int lastCol = checkArea != null ? checkArea.right : drawableWidth - 1;
        final int expectedAlpha = Color.alpha(color);
        final int expectedRed = Color.red(color);
        final int expectedGreen = Color.green(color);
        final int expectedBlue = Color.blue(color);
        for (int row = firstRow; row <= lastRow; row++) {
            bitmap.getPixels(rowPixels, 0, drawableWidth, 0, row, drawableWidth, 1);
            for (int column = firstCol; column <= lastCol; column++) {
                int sourceAlpha = Color.alpha(rowPixels[column]);
                int sourceRed = Color.red(rowPixels[column]);
                int sourceGreen = Color.green(rowPixels[column]);
                int sourceBlue = Color.blue(rowPixels[column]);
                int varianceAlpha = Math.abs(sourceAlpha - expectedAlpha);
                int varianceRed = Math.abs(sourceRed - expectedRed);
                int varianceGreen = Math.abs(sourceGreen - expectedGreen);
                int varianceBlue = Math.abs(sourceBlue - expectedBlue);
                boolean isColorMatch = (varianceAlpha <= allowedComponentVariance) && (varianceRed <= allowedComponentVariance) && (varianceGreen <= allowedComponentVariance) && (varianceBlue <= allowedComponentVariance);
                if (!isColorMatch) {
                    String mismatchDescription = failMessagePrefix + ": expected all drawable colors to be [" + expectedAlpha + "," + expectedRed + "," + expectedGreen + "," + expectedBlue + "] but at position (" + row + "," + column + ") found [" + sourceAlpha + "," + sourceRed + "," + sourceGreen + "," + sourceBlue + "]";
                    if (throwExceptionIfFails) {
                        throw new RuntimeException(mismatchDescription);
                    } else {
                        Assert.fail(mismatchDescription);
                    }
                }
            }
        }
    } finally {
        bitmap.recycle();
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas)

Example 80 with Canvas

use of android.graphics.Canvas in project scissors by lyft.

the class Utils method asBitmap.

public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) {
    final Rect tmpRect = new Rect();
    drawable.copyBounds(tmpRect);
    if (tmpRect.isEmpty()) {
        tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight()));
        drawable.setBounds(tmpRect);
    }
    Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888);
    drawable.draw(new Canvas(bitmap));
    return bitmap;
}
Also used : Rect(android.graphics.Rect) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas)

Aggregations

Canvas (android.graphics.Canvas)1237 Bitmap (android.graphics.Bitmap)890 Paint (android.graphics.Paint)609 Rect (android.graphics.Rect)257 BitmapDrawable (android.graphics.drawable.BitmapDrawable)192 RectF (android.graphics.RectF)136 PorterDuffXfermode (android.graphics.PorterDuffXfermode)103 Matrix (android.graphics.Matrix)80 Point (android.graphics.Point)76 Drawable (android.graphics.drawable.Drawable)68 BitmapShader (android.graphics.BitmapShader)60 Test (org.junit.Test)46 IOException (java.io.IOException)43 FileOutputStream (java.io.FileOutputStream)40 View (android.view.View)39 Path (android.graphics.Path)38 File (java.io.File)37 SuppressLint (android.annotation.SuppressLint)36 ColorMatrix (android.graphics.ColorMatrix)36 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)33