Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

In this page you can find the example usage for android.graphics Canvas drawBitmap.

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:com.example.zhaozhu.practisecustomview.customviewgroup.HSlidingPaneLayout.java

@Override
protected boolean drawChild(final Canvas canvas, final View child, final long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (this.mCanSlide && !lp.slideable && (this.mSlideableView != null)) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered.
        canvas.getClipBounds(this.mTmpRect);
        this.mTmpRect.right = Math.min(this.mTmpRect.right, this.mSlideableView.getLeft());
        canvas.clipRect(this.mTmpRect);
    }//from w w  w.j a  v a2  s .co  m

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && (this.mSlideOffset > 0)) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            Bitmap cache = child.getDrawingCache();
            if (cache != null && !cache.isRecycled()) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(HSlidingPaneLayout.TAG,
                        "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.example.zhaozhu.practisecustomview.customviewgroup.HSlidingPaneLayout2.java

@Override
protected boolean drawChild(final Canvas canvas, final View child, final long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (this.mCanSlide && !lp.slideable && (this.mSlideableView != null)) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered.
        canvas.getClipBounds(this.mTmpRect);
        this.mTmpRect.right = Math.min(this.mTmpRect.right, this.mSlideableView.getLeft());
        canvas.clipRect(this.mTmpRect);
    }/*  ww  w. ja  va  2 s .  c  o  m*/

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && (this.mSlideOffset > 0)) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            Bitmap cache = child.getDrawingCache();
            if (cache != null && !cache.isRecycled()) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(HSlidingPaneLayout2.TAG,
                        "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:cn.zmdx.kaka.locker.widget.SlidingPaneLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered.
        canvas.getClipBounds(mTmpRect);/*from ww w  .j av  a2 s. c  o  m*/
        if (isLayoutRtlSupport()) {
            mTmpRect.left = Math.max(mTmpRect.left, mSlideableView.getRight());
        } else {
            mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        }
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

@Override
public void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    mDrawOperation.setCanvas(canvas);/*from w w  w.  j  av a 2  s . c o m*/

    if (mDrawPending || mBuffer == null || mKeyboardChanged) {
        GCUtils.getInstance().peformOperationWithMemRetry(TAG, mDrawOperation, true);
    }
    // maybe there is no buffer, since drawing was not done.
    if (mBuffer != null)
        canvas.drawBitmap(mBuffer, 0.0f, 0.0f, null);
}

From source file:cn.wyx.android.swipeback.swipe.SwipeBackLayout.java

/**
 * ?// ww  w  . java 2 s. c  o m
 *
 * @param canvas canvas
 */
private void drawLastScreenshot(Canvas canvas) {
    if (mLastScreenshot != null && target != null) {
        int right = target.getLeft();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Rect src = new Rect(mLastScreenshot.getWidth() - target.getLeft(), 0, mLastScreenshot.getWidth(),
                target.getBottom());
        //target??
        Rect dst = new Rect(0, target.getBottom() - mLastScreenshot.getHeight(), right, target.getBottom());

        int navigationBarHeight = getNavigationBarHeight();

        isFullscreen = target.getHeight() == getResources().getDisplayMetrics().heightPixels
                + navigationBarHeight;

        int statusBarHeight = getStatusBarHeight();

        switch (dragEdge) {
        case LEFT:
            src = new Rect(mLastScreenshot.getWidth() - target.getLeft(), 0, mLastScreenshot.getWidth(),
                    mLastScreenshot.getHeight());
            dst = new Rect(0, isFullscreen ? statusBarHeight : target.getBottom() - mLastScreenshot.getHeight(),
                    right, isFullscreen ? target.getHeight() - navigationBarHeight : target.getBottom());

            break;
        case RIGHT:
            src = new Rect(0, 0, mLastScreenshot.getWidth() - target.getRight(), mLastScreenshot.getHeight());
            dst = new Rect(target.getRight(),
                    isFullscreen ? statusBarHeight : target.getBottom() - mLastScreenshot.getHeight(),
                    mLastScreenshot.getWidth(),
                    isFullscreen ? target.getHeight() - navigationBarHeight : target.getBottom());
            break;

        case TOP:
            src = new Rect(0, mLastScreenshot.getHeight() - target.getTop(), mLastScreenshot.getWidth(),
                    target.getBottom());
            dst = new Rect(0, 0, target.getRight(), target.getTop());
            break;
        case BOTTOM:
            src = new Rect(0, 0, mLastScreenshot.getWidth(), mLastScreenshot.getHeight() - target.getBottom());
            dst = new Rect(0, target.getBottom(), mLastScreenshot.getWidth(), mLastScreenshot.getHeight());
            break;
        }

        canvas.drawBitmap(mLastScreenshot, src, dst, paint);
    }
}

From source file:com.cnh.library.materialdrawer.view.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;/*from   www . j  a va 2s  . co m*/
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight || isSelected != isPressed) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            if (isSelected) {
                if (mSelectorFilter != null) {
                    mMaskedPaint.setColorFilter(mSelectorFilter);
                } else {
                    mMaskedPaint.setColorFilter(mDesaturateColorFilter);

                }
            } else {
                mMaskedPaint.setColorFilter(null);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (isSelected) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            if (mSelectorFilter != null) {
                mMaskedPaint.setColorFilter(mSelectorFilter);
            } else {
                mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);

    //remember the previous press state
    isPressed = isPressed();
}

From source file:cn.bingoogolapple.swipebacklayout.BGASwipeBackLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);/*  w  ww  . ja v a 2 s  .  c o m*/
        if (isLayoutRtlSupport()) {
            mTmpRect.left = Math.max(mTmpRect.left, mSlideableView.getRight());
        } else {
            mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        }
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.android.mms.ui.MessageUtils.java

public static Bitmap getCircularBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = bitmap.getWidth() / 2;

    paint.setAntiAlias(true);/*from w w  w.j av  a2s .c o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//* w w w . ja  v  a 2s . c om*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    final Drawable phoneDrawable = r.getDrawableForDensity(actionResId, mIconDensity);
    // These icons have the same height and width so either is fine for the size.
    final Bitmap phoneIcon = BitmapUtil.drawableToBitmap(phoneDrawable, phoneDrawable.getIntrinsicHeight());

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type if we're pre-O. O created shortcuts have the
    // app badge which overlaps the type overlay.
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);
    if (!BuildCompat.isAtLeastO() && overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    int iconWidth = icon.getWidth();
    if (BuildCompat.isAtLeastO()) {
        // On O we need to calculate where the phone icon goes slightly differently. The whole
        // canvas area is 108dp, a centered circle with a diameter of 66dp is the "safe zone".
        // So we start the drawing the phone icon at
        // 108dp - 21 dp (distance from right edge of safe zone to the edge of the canvas)
        // - 24 dp (size of the phone icon) on the x axis (left)
        // The y axis is simply 21dp for the distance to the safe zone (top).
        // See go/o-icons-eng for more details and a handy picture.
        final int left = (int) (mIconSize - (45 * density));
        final int top = (int) (21 * density);
        canvas.drawBitmap(phoneIcon, left, top, photoPaint);
    } else {
        dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
        canvas.drawBitmap(phoneIcon, null, dst, photoPaint);
    }

    canvas.setBitmap(null);
    return icon;
}

From source file:de.vanita5.twittnuker.util.Utils.java

/**
 * Resizes specific a Bitmap with keeping ratio.
 *//*from  w w w .j a  v a  2  s .c o  m*/
public static Bitmap resizeBitmap(Bitmap orig, final int desireWidth, final int desireHeight) {
    final int width = orig.getWidth();
    final int height = orig.getHeight();

    if (0 < width && 0 < height && desireWidth < width || desireHeight < height) {
        // Calculate scale
        float scale;
        if (width < height) {
            scale = (float) desireHeight / (float) height;
            if (desireWidth < width * scale) {
                scale = (float) desireWidth / (float) width;
            }
        } else {
            scale = (float) desireWidth / (float) width;
        }

        // Draw resized image
        final Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        final Bitmap bitmap = Bitmap.createBitmap(orig, 0, 0, width, height, matrix, true);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bitmap, 0, 0, null);

        orig = bitmap;
    }

    return orig;
}