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:cl.monsoon.s1next.widget.BezelImageView.java

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

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

    if (!mCacheValid || mCachedWidth != width || mCachedHeight != height) {
        // need to redraw the cache
        if (mCachedWidth == width && mCachedHeight == height) {
            // have a correct-sized bitmap cache already allocated
            // just erase it
            mCacheBitmap.eraseColor(Color.TRANSPARENT);
        } 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 saveCount = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(saveCount);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

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

From source file:com.hxsn.witwork.ui.ViewfinderView.java

@Override
public void onDraw(Canvas canvas) {
    // ??????CameraManager?
    if (CameraManager.get() == null) {
        return;/*from  w w w . j  a va  2  s.  c o  m*/
    }
    Rect frame = CameraManager.get().getFramingRect();
    if (frame == null) {
        return;
    }

    // ?
    if (!isFirst) {
        isFirst = true;
        slideTop = frame.top;
        slideBottom = frame.bottom;
    }

    // ??
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    paint.setColor(resultBitmap != null ? resultColor : maskColor);

    // ?????????????
    // ?????????
    canvas.drawRect(0, 0, width, frame.top + 15, paint);
    canvas.drawRect(0, frame.top + 15, frame.left + 15, frame.bottom - 14, paint);
    canvas.drawRect(frame.right - 14, frame.top + 15, width, frame.bottom - 14, paint);
    canvas.drawRect(0, frame.bottom - 14, width, height, paint);

    if (resultBitmap != null) {
        // Draw the opaque result bitmap over the scanning rectangle
        paint.setAlpha(OPAQUE);
        canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
    } else {

        // ??8
        paint.setColor(Color.rgb(139, 193, 17));
        canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint);
        canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint);
        canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint);
        canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint);
        canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint);
        canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint);
        canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom,
                paint);
        canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom,
                paint);

        // ,??SPEEN_DISTANCE
        slideTop += SPEEN_DISTANCE;
        if (slideTop >= frame.bottom) {
            slideTop = frame.top;
        }
        // canvas.drawRect(frame.left + MIDDLE_LINE_PADDING, slideTop -
        // MIDDLE_LINE_WIDTH/2, frame.right - MIDDLE_LINE_PADDING,slideTop +
        // MIDDLE_LINE_WIDTH/2, paint);
        Rect lineRect = new Rect();

        lineRect.left = frame.left;
        lineRect.right = frame.right;
        lineRect.top = slideTop;
        lineRect.bottom = slideTop + 18;
        canvas.drawBitmap(((BitmapDrawable) (getResources().getDrawable(R.drawable.fgx))).getBitmap(), null,
                lineRect, paint);

        // ???
        paint.setColor(Color.WHITE);
        paint.setTextSize(TEXT_SIZE * density);
        // paint.setAlpha(0x40);
        // paint.setTypeface(Typeface.create("System", Typeface.BOLD));

        canvas.drawText("?/??????", frame.left,
                (float) (frame.bottom + (float) TEXT_PADDING_TOP * density), paint);
        canvas.drawText("??" + num + "", frame.left,
                (float) (frame.bottom + (float) TEXT_PADDING_TOP * density + 50), paint);

        int max = 7;
        if (name.size() < max) {
            max = name.size();
            for (int i = 0; i < max; i++) {
                canvas.drawText(name.get(i), frame.left,
                        (float) (frame.bottom + (float) TEXT_PADDING_TOP * density + 100 + 50 * i), paint);
            }
        } else {
            for (int i = 6; i >= 0; i--) {
                canvas.drawText(name.get((name.size() - 1) - (6 - i)), frame.left,
                        (float) (frame.bottom + (float) TEXT_PADDING_TOP * density + 100 + 50 * i), paint);
            }
        }

        paint.setStyle(Style.STROKE);
        canvas.drawRect(frame.left + 15, frame.top + 15, frame.right - 15, frame.bottom - 15, paint);
        setViewY(frame.bottom);
        paint.setStyle(Style.FILL);

        Collection<ResultPoint> currentPossible = possibleResultPoints;
        Collection<ResultPoint> currentLast = lastPossibleResultPoints;
        if (currentPossible.isEmpty()) {
            lastPossibleResultPoints = null;
        } else {
            possibleResultPoints = new HashSet<ResultPoint>(5);
            lastPossibleResultPoints = currentPossible;
            paint.setAlpha(OPAQUE);
            paint.setColor(resultPointColor);
            for (ResultPoint point : currentPossible) {
                canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint);
            }
        }
        if (currentLast != null) {
            paint.setAlpha(OPAQUE / 2);
            paint.setColor(resultPointColor);
            for (ResultPoint point : currentLast) {
                canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint);
            }
        }

        // ????
        postInvalidate();
        // postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top,
        // frame.right, frame.bottom);

    }
}

From source file:android.support.design.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from   w  w  w . jav a  2 s.  co m*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.digreamon.android.widget.SlidingPaneLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    if (Build.VERSION.SDK_INT >= 11) { // HC
        return super.drawChild(canvas, child, drawingTime);
    }/*from ww w .  j av  a2 s  .c  o m*/

    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    if (lp.dimWhenOffset && mSlideOffset > 0) {
        if (!child.isDrawingCacheEnabled()) {
            child.setDrawingCacheEnabled(true);
        }
        final Bitmap cache = child.getDrawingCache();
        canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
        return false;
    } else {
        if (child.isDrawingCacheEnabled()) {
            child.setDrawingCacheEnabled(false);
        }
        return super.drawChild(canvas, child, drawingTime);
    }
}

From source file:com.android.mail.browse.ConversationItemView.java

private void drawSendersImage(final Canvas canvas) {
    if (!mSendersImageView.isFlipping()) {
        final boolean showSenders = !mChecked;
        mSendersImageView.reset(showSenders);
    }/*from w  ww.j a va2s  .  c  o  m*/
    canvas.translate(mCoordinates.contactImagesX, mCoordinates.contactImagesY);
    if (mPhotoBitmap == null) {
        mSendersImageView.draw(canvas);
    } else {
        canvas.drawBitmap(mPhotoBitmap, null, mPhotoRect, sPaint);
    }
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

public static Bitmap graphics_addWaterMarkToImage(Bitmap src, String watermark, Point location, int size,
        boolean underline) {
    Bitmap result = null;/*www  .  j a  va 2  s .  c o m*/

    try {
        int w = src.getWidth();
        int h = src.getHeight();
        result = Bitmap.createBitmap(w, h, src.getConfig());

        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(src, 0, 0, null);

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        //paint.setAlpha(alpha);
        paint.setTextSize(size);
        paint.setAntiAlias(true);
        paint.setUnderlineText(underline);
        canvas.drawText(watermark, location.x, location.y, paint);
    } catch (Exception e) {
        result = null;
        if (LOG_ENABLE)
            Log.e(TAG, "ERROR: graphics_addWaterMarkToImage() [" + e.getMessage() + "]", e);
    }

    return result;
}

From source file:cl.monsoon.s1next.widget.PhotoView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // draw the photo
    if (mDrawable != null) {
        int saveCount = canvas.getSaveCount();
        canvas.save();//  w  ww .  j  a  v a2s.c o m

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);
        }
        mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);

        if (mVideoBlob != null) {
            final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage);
            final int drawLeft = (getWidth() - videoImage.getWidth()) / 2;
            final int drawTop = (getHeight() - videoImage.getHeight()) / 2;
            canvas.drawBitmap(videoImage, drawLeft, drawTop, null);
        }

        // Extract the drawable's bounds (in our own copy, to not alter the image)
        mTranslateRect.set(mDrawable.getBounds());
        if (mDrawMatrix != null) {
            mDrawMatrix.mapRect(mTranslateRect);
        }

        if (mAllowCrop) {
            int previousSaveCount = canvas.getSaveCount();
            canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint);
            canvas.save();
            canvas.clipRect(mCropRect);

            if (mDrawMatrix != null) {
                canvas.concat(mDrawMatrix);
            }

            mDrawable.draw(canvas);
            canvas.restoreToCount(previousSaveCount);
            canvas.drawRect(mCropRect, sCropPaint);
        }
    }
}

From source file:com.android.andryyu.lifehelper.widget.RippleView.java

private void doRippleWork(Canvas canvas, int offect) {
    canvas.save();/*from   w ww.  j  a v a2  s.c o m*/
    if (rippleDuration <= timer * frameRate) {
        // There is problem on Android M where canvas.restore() seems to be called automatically
        // For now, don't call canvas.restore() manually on Android M (API 23)
        canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint);

        if (onCompletionListener != null && rippleStatus != RIPPLE_ACTION_MOVE
                && rippleStatus != RIPPLE_LONG_PRESS) {
            onCompletionListener.onComplete(this);
        }
        if (rippleStatus != RIPPLE_LONG_PRESS) {
            animationRunning = false;
            rippleStatus = RIPPLE_NORMAL;
            timer = 0;
            durationEmpty = -1;
            timerEmpty = 0;
            if (Build.VERSION.SDK_INT != 23) {
                canvas.restore();
            }
        }
        invalidate();
        return;
    } else
        canvasHandler.postDelayed(runnable, frameRate);

    if (timer == 0)
        canvas.save();

    canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint);

    paint.setColor(Color.parseColor("#ffff4444"));

    if (rippleType == 1 && originBitmap != null && (((float) timer * frameRate) / rippleDuration) > 0.4f) {
        if (durationEmpty == -1)
            durationEmpty = rippleDuration - timer * frameRate;

        timerEmpty++;
        final Bitmap tmpBitmap = getCircleBitmap(
                (int) ((radiusMax) * (((float) timerEmpty * frameRate) / (durationEmpty))));
        canvas.drawBitmap(tmpBitmap, 0, 0, paint);
        tmpBitmap.recycle();
    }

    paint.setColor(rippleColor);
    if (!isListMode) {
        if (rippleType == 1) {
            if ((((float) timer * frameRate) / rippleDuration) > 0.6f)
                paint.setAlpha((int) (rippleAlpha
                        - ((rippleAlpha) * (((float) timerEmpty * frameRate) / (durationEmpty)))));
            else
                paint.setAlpha(rippleAlpha);
        } else
            paint.setAlpha(
                    (int) (rippleAlpha - ((rippleAlpha) * (((float) timer * frameRate) / rippleDuration))));
    }
    timer += offect;
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Convers a Bitmap to grayscale./*from   w w  w .j a v a2  s  .c om*/
 * 
 * @param bmpOriginal
 * @return
 */
public static Bitmap media_getGrayScale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:com.android.contacts.common.list.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.
 *//*  www .  java  2s .c o  m*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    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
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (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
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}