Search in sources :

Example 1 with ImageDecorator

use of com.stanfy.enroscar.views.decorator.ImageDecorator 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 2 with ImageDecorator

use of com.stanfy.enroscar.views.decorator.ImageDecorator in project enroscar by stanfy.

the class ImageView method drawableStateChanged.

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final ImageDecorator d = imageDecorator;
    if (d != null && d.dependsOnDrawableState()) {
        clearDecorateCache();
        invalidate();
    }
}
Also used : MaskImageDecorator(com.stanfy.enroscar.views.decorator.MaskImageDecorator) ImageDecorator(com.stanfy.enroscar.views.decorator.ImageDecorator)

Aggregations

ImageDecorator (com.stanfy.enroscar.views.decorator.ImageDecorator)2 MaskImageDecorator (com.stanfy.enroscar.views.decorator.MaskImageDecorator)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Matrix (android.graphics.Matrix)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1 LayerDrawable (android.graphics.drawable.LayerDrawable)1