Search in sources :

Example 26 with Canvas

use of android.graphics.Canvas in project enroscar by stanfy.

the class ShadowDecorator method processBitmap.

@Override
protected Bitmap processBitmap(final Bitmap source, final Canvas sourceCanvas) {
    final Bitmap buffer = this.bitmap;
    final Canvas bufferCanvas = this.bitmapCanvas;
    final Paint shadowPaint = this.shadowPaint;
    buffer.eraseColor(0);
    final float size = this.size;
    shadowPaint.setShadowLayer(blur, size, size, Color.BLACK);
    bufferCanvas.drawRect(dst, shadowPaint);
    bufferCanvas.drawBitmap(source, src, dst, null);
    source.eraseColor(0);
    sourceCanvas.drawBitmap(buffer, 0, 0, null);
    return source;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 27 with Canvas

use of android.graphics.Canvas in project enroscar by stanfy.

the class ImageView method onDraw.

@Override
protected void onDraw(final Canvas canvas) {
    final Drawable d = getDrawable();
    if (d == null) {
        return;
    }
    final ImageDecorator imageDecorator = this.imageDecorator;
    if (imageDecorator == null) {
        super.onDraw(canvas);
        return;
    }
    final int pl = getPaddingLeft(), pt = getPaddingTop();
    final int resultW = getMeasuredWidth() - pl - getPaddingRight(), resultH = getMeasuredHeight() - pt - getPaddingBottom();
    if (resultW <= 0 || resultH <= 0) {
        super.onDraw(canvas);
        return;
    }
    final Matrix drawMatrix = this.drawMatrix;
    int realW = resultW, realH = resultH;
    final Boolean respectIntrinsicDrawableSize = this.respectIntrinsicDrawableSize;
    final boolean useIntrinsic = respectIntrinsicDrawableSize == null ? d instanceof BitmapDrawable : respectIntrinsicDrawableSize;
    if (useIntrinsic) {
        realW = d.getIntrinsicWidth();
        realH = d.getIntrinsicHeight();
        // nothing to draw
        if (realW == 0 || realH == 0) {
            return;
        }
        if (drawMatrix != null) {
            final float[] src = bufferSrcVector, dst = bufferDstVector;
            src[0] = realW;
            src[1] = 0;
            drawMatrix.mapVectors(dst, src);
            realW = (int) dst[0];
            src[0] = 0;
            src[1] = realH;
            drawMatrix.mapVectors(dst, src);
            realH = (int) dst[1];
        }
    }
    final Bitmap decorated;
    Bitmap bitmap = decoratedCache != null ? decoratedCache.get() : null;
    if (bitmap == null || bitmap.getWidth() != resultW || bitmap.getHeight() != resultH) {
        final Bitmap old = bitmap;
        bitmap = Bitmap.createBitmap(resultW, resultH, Bitmap.Config.ARGB_8888);
        bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
        if (old != null) {
            old.recycle();
        }
        updateDecorationCacheReference(bitmap);
        decoratedCacheActual = false;
    }
    if (!decoratedCacheActual) {
        /*
       * it's important to set it prior to drawable drawing since
       * it can be reset by invalidateSelf call (see TransitionDrawable)
       */
        decoratedCacheActual = true;
        imageDecorator.setup(resultW, resultH, getDrawableState(), d.getLevel(), realW, realH);
        final Canvas bitmapCanvas = decorationCanvas;
        bitmapCanvas.setBitmap(bitmap);
        if (drawMatrix == null) {
            d.draw(bitmapCanvas);
        } else {
            final int saveCount = bitmapCanvas.save();
            bitmapCanvas.concat(drawMatrix);
            d.draw(bitmapCanvas);
            bitmapCanvas.restoreToCount(saveCount);
        }
        decorated = imageDecorator.decorateBitmap(bitmap, bitmapCanvas);
        if (decorated != bitmap) {
            bitmap.recycle();
            updateDecorationCacheReference(decorated);
        }
        // we used null here but some devices fail with NP
        bitmapCanvas.setBitmap(NULL_BITMAP);
    } else {
        decorated = bitmap;
    }
    if (pl == 0 && pt == 0) {
        canvas.drawBitmap(decorated, 0, 0, null);
    } else {
        final int saveCount = canvas.save();
        canvas.translate(pl, pt);
        canvas.drawBitmap(decorated, 0, 0, null);
        canvas.restoreToCount(saveCount);
    }
}
Also used : MaskImageDecorator(com.stanfy.enroscar.views.decorator.MaskImageDecorator) ImageDecorator(com.stanfy.enroscar.views.decorator.ImageDecorator) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) LayerDrawable(android.graphics.drawable.LayerDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 28 with Canvas

use of android.graphics.Canvas in project WaveLoadingView by tangqi92.

the class WaveLoadingView method updateWaveShader.

private void updateWaveShader() {
    // http://stackoverflow.com/questions/17605662/illegalargumentexception-width-and-height-must-be-0-while-loading-bitmap-from
    if (bitmapBuffer == null || haveBoundsChanged()) {
        if (bitmapBuffer != null)
            bitmapBuffer.recycle();
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (width > 0 && height > 0) {
            double defaultAngularFrequency = 2.0f * Math.PI / DEFAULT_WAVE_LENGTH_RATIO / width;
            float defaultAmplitude = height * DEFAULT_AMPLITUDE_RATIO;
            mDefaultWaterLevel = height * DEFAULT_WATER_LEVEL_RATIO;
            float defaultWaveLength = width;
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint wavePaint = new Paint();
            wavePaint.setStrokeWidth(2);
            wavePaint.setAntiAlias(true);
            // Draw default waves into the bitmap.
            // y=Asin(ωx+φ)+h
            final int endX = width + 1;
            final int endY = height + 1;
            float[] waveY = new float[endX];
            wavePaint.setColor(adjustAlpha(mWaveColor, 0.3f));
            for (int beginX = 0; beginX < endX; beginX++) {
                double wx = beginX * defaultAngularFrequency;
                float beginY = (float) (mDefaultWaterLevel + defaultAmplitude * Math.sin(wx));
                canvas.drawLine(beginX, beginY, beginX, endY, wavePaint);
                waveY[beginX] = beginY;
            }
            wavePaint.setColor(mWaveColor);
            final int wave2Shift = (int) (defaultWaveLength / 4);
            for (int beginX = 0; beginX < endX; beginX++) {
                canvas.drawLine(beginX, waveY[(beginX + wave2Shift) % endX], beginX, endY, wavePaint);
            }
            // Use the bitamp to create the shader.
            mWaveShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
            this.mWavePaint.setShader(mWaveShader);
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) BitmapShader(android.graphics.BitmapShader) Point(android.graphics.Point) Paint(android.graphics.Paint)

Example 29 with Canvas

use of android.graphics.Canvas in project Shuttle by timusus.

the class DrawableUtils method getTintedNotificationDrawable.

/**
     * Takes a drawable resource and turns it black
     *
     * @param baseDrawableResId the resource id of the drawable to theme
     * @return a themed {@link android.graphics.drawable.Drawable}
     */
public static Bitmap getTintedNotificationDrawable(Context context, int baseDrawableResId) {
    boolean inverse = SettingsManager.getInstance().invertNotificationIcons();
    int colorResId = inverse ? R.color.notification_control_tint_inverse : R.color.notification_control_tint;
    if (ShuttleUtils.hasNougat()) {
        colorResId = inverse ? R.color.notification_control_tint_v24_inverse : R.color.notification_control_tint_v24;
    }
    int tintColor = context.getResources().getColor(colorResId);
    Drawable baseDrawable = context.getResources().getDrawable(baseDrawableResId);
    baseDrawable = DrawableCompat.wrap(baseDrawable);
    DrawableCompat.setTint(baseDrawable, tintColor);
    Bitmap bitmap = Bitmap.createBitmap(baseDrawable.getIntrinsicWidth(), baseDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    baseDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    baseDrawable.draw(canvas);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) LayerDrawable(android.graphics.drawable.LayerDrawable) FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) Drawable(android.graphics.drawable.Drawable)

Example 30 with Canvas

use of android.graphics.Canvas in project glitch-hq-android by tinyspeck.

the class BitmapUtil method CreateTransparentOvalBitmap.

public static Bitmap CreateTransparentOvalBitmap(int nBitmapW, int nBitmapH, RectF r, int nInColor, int nOutColor) {
    Bitmap bmMask = Bitmap.createBitmap(nBitmapW, nBitmapH, Bitmap.Config.ARGB_8888);
    Canvas cvsMask = new Canvas(bmMask);
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);
    paint.setColor(nOutColor);
    cvsMask.drawRect(0, 0, nBitmapW, nBitmapH, paint);
    paint.setColor(nInColor);
    cvsMask.drawOval(r, paint);
    return bmMask;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

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