Example usage for android.graphics Canvas drawColor

List of usage examples for android.graphics Canvas drawColor

Introduction

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

Prototype

public void drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode) 

Source Link

Document

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and porter-duff xfermode.

Usage

From source file:butter.droid.base.fragments.BaseVideoPlayerFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int i = item.getItemId();
    if (i == R.id.action_reload) {
        mMediaPlayer.stop();/*from w w w.ja va2 s  .  c o  m*/
        Canvas canvas = new Canvas();
        canvas.drawColor(0, PorterDuff.Mode.CLEAR);
        getVideoSurface().draw(canvas);
        loadMedia();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:eu.janmuller.android.simplecropimage.CropImage.java

private void onSaveClicked() throws Exception {
    // TODO this code needs to change to use the decode/crop/encode single
    // step api so that we don't require that the whole (possibly large)
    // bitmap doesn't have to be read into memory
    if (mSaving)/*from  w w w  .j a v  a2s.  c  om*/
        return;

    if (mCrop == null) {

        return;
    }

    mSaving = true;

    Rect r = mCrop.getCropRect();

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

    // If we are circle cropping, we want alpha channel, which is the
    // third param here.
    Bitmap croppedImage;
    try {

        croppedImage = Bitmap.createBitmap(width, height,
                mCircleCrop ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    } catch (Exception e) {
        throw e;
    }
    if (croppedImage == null) {

        return;
    }

    {
        Canvas canvas = new Canvas(croppedImage);
        Rect dstRect = new Rect(0, 0, width, height);
        canvas.drawBitmap(mBitmap, r, dstRect, null);
    }

    if (mCircleCrop) {

        // OK, so what's all this about?
        // Bitmaps are inherently rectangular but we want to return
        // something that's basically a circle.  So we fill in the
        // area around the circle with alpha.  Note the all important
        // PortDuff.Mode.CLEAR.
        Canvas c = new Canvas(croppedImage);
        Path p = new Path();
        p.addCircle(width / 2F, height / 2F, width / 2F, Path.Direction.CW);
        c.clipPath(p, Region.Op.DIFFERENCE);
        c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
    }

    /* If the output is required to a specific size then scale or fill */
    if (mOutputX != 0 && mOutputY != 0) {

        if (mScale) {

            /* Scale the image to the required dimensions */
            Bitmap old = croppedImage;
            croppedImage = Util.transform(new Matrix(), croppedImage, mOutputX, mOutputY, mScaleUp);
            if (old != croppedImage) {

                old.recycle();
            }
        } else {

            /* Don't scale the image crop it to the size requested.
                 * Create an new image with the cropped image in the center and
             * the extra space filled.
             */

            // Don't scale the image but instead fill it so it's the
            // required dimension
            Bitmap b = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(b);

            Rect srcRect = mCrop.getCropRect();
            Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);

            int dx = (srcRect.width() - dstRect.width()) / 2;
            int dy = (srcRect.height() - dstRect.height()) / 2;

            /* If the srcRect is too big, use the center part of it. */
            srcRect.inset(Math.max(0, dx), Math.max(0, dy));

            /* If the dstRect is too big, use the center part of it. */
            dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

            /* Draw the cropped bitmap in the center */
            canvas.drawBitmap(mBitmap, srcRect, dstRect, null);

            /* Set the cropped bitmap as the new bitmap */
            croppedImage.recycle();
            croppedImage = b;
        }
    }

    // Return the cropped image directly or save it to the specified URI.
    Bundle myExtras = getIntent().getExtras();
    if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean(RETURN_DATA))) {

        Bundle extras = new Bundle();
        extras.putParcelable(RETURN_DATA_AS_BITMAP, croppedImage);
        setResult(RESULT_OK, (new Intent()).setAction(ACTION_INLINE_DATA).putExtras(extras));
        finish();
    } else {
        final Bitmap b = croppedImage;
        Util.startBackgroundJob(this, null, getString(R.string.saving_image), new Runnable() {
            public void run() {

                saveOutput(b);
            }
        }, mHandler);
    }
}

From source file:com.android.screenspeak.contextmenu.RadialMenuView.java

@Override
public void invalidate() {
    super.invalidate();

    final SurfaceHolder holder = mHolder;
    if (holder == null) {
        return;/*w w w. j  a va2s  .  co m*/
    }

    final Canvas canvas = holder.lockCanvas();
    if (canvas == null) {
        return;
    }

    // Clear the canvas.
    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

    if (getVisibility() != View.VISIBLE) {
        holder.unlockCanvasAndPost(canvas);
        return;
    }

    final int width = getWidth();
    final int height = getHeight();

    if (!mDisplayWedges) {
        mCenter.x = (width / 2.0f);
        mCenter.y = (height / 2.0f);
    }

    // Draw the pretty gradient background.
    mGradientBackground.setGradientCenter((mCenter.x / width), (mCenter.y / height));
    mGradientBackground.setBounds(0, 0, width, height);
    mGradientBackground.draw(canvas);

    final RadialMenu menu = (mSubMenu != null) ? mSubMenu : mRootMenu;
    final float center = mExtremeRadius;

    if (mDisplayWedges) {
        final int wedges = menu.size();
        final float degrees = 360.0f / wedges;

        // Refresh cached wedge shapes if necessary.
        if (0 != menu.size()) {
            invalidateCachedWedgeShapes();
        }

        // Draw the cancel dot.
        drawCancel(canvas);

        // Draw wedges.
        for (int i = 0; i < wedges; i++) {
            drawWedge(canvas, center, i, menu, degrees);
        }
    } else {
        // Draw the center dot.
        drawCenterDot(canvas, width, height);
    }

    // Draw corners.
    for (int i = 0; i < 4; i++) {
        drawCorner(canvas, width, height, center, i);
    }

    holder.unlockCanvasAndPost(canvas);
}

From source file:com.googlecode.eyesfree.widget.RadialMenuView.java

@Override
public void invalidate() {
    super.invalidate();

    final SurfaceHolder holder = mHolder;
    if (holder == null) {
        return;/*from w w w . j  a v  a 2s .  com*/
    }

    final Canvas canvas = holder.lockCanvas();
    if (canvas == null) {
        return;
    }

    // Clear the canvas.
    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

    if (getVisibility() != View.VISIBLE) {
        holder.unlockCanvasAndPost(canvas);
        return;
    }

    final int width = getWidth();
    final int height = getHeight();

    if (!mDisplayWedges) {
        mCenter.x = (width / 2.0f);
        mCenter.y = (height / 2.0f);
    }

    // Draw the pretty gradient background.
    mGradientBackground.setGradientCenter((mCenter.x / width), (mCenter.y / height));
    mGradientBackground.setBounds(0, 0, width, height);
    mGradientBackground.draw(canvas);

    final RadialMenu menu = (mSubMenu != null) ? mSubMenu : mRootMenu;
    final float center = mExtremeRadius;

    if (mDisplayWedges) {
        final int wedges = menu.size();
        final float degrees = 360.0f / wedges;

        // Refresh cached wedge shapes if necessary.
        if (mCachedMenuSize != menu.size()) {
            invalidateCachedWedgeShapes();
        }

        // Draw the cancel dot.
        drawCancel(canvas, width, height, center);

        // Draw wedges.
        for (int i = 0; i < wedges; i++) {
            drawWedge(canvas, width, height, center, i, menu, degrees);
        }
    } else {
        // Draw the center dot.
        drawCenterDot(canvas, width, height);
    }

    // Draw corners.
    for (int i = 0; i < 4; i++) {
        drawCorner(canvas, width, height, center, i);
    }

    holder.unlockCanvasAndPost(canvas);
}

From source file:com.mylikes.likes.etchasketch.Slate.java

private void drawStrokeDebugInfo(Canvas c) {
    final int ROW_HEIGHT = 24;
    final int ROW_MARGIN = 6;
    final int COLUMN_WIDTH = 55;

    final float FIRM_PRESSURE_LOW = 0.85f;
    final float FIRM_PRESSURE_HIGH = 1.25f;

    if (mStrokeDebugGraph == null) {
        final int width = c.getWidth() - 128;
        final int height = ROW_HEIGHT * mStrokes.length + 2 * ROW_MARGIN;
        mStrokeDebugGraph = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (mStrokeDebugGraph == null) {
            throw new RuntimeException(
                    "drawStrokeDebugInfo: couldn't create debug bitmap (" + width + "x" + height + ")");
        }/*  w  w  w.  j a v  a 2s  .c om*/
        mGraphPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    Canvas graph = new Canvas(mStrokeDebugGraph);
    graph.save();
    graph.clipRect(new Rect(0, 0, COLUMN_WIDTH, graph.getHeight()));
    graph.drawColor(0, PorterDuff.Mode.CLEAR);
    graph.restore();

    int left = 4;
    int bottom = graph.getHeight() - ROW_MARGIN;
    final int STEP = 4;
    for (MarkersPlotter st : mStrokes) {
        float r = st.getLastPressure();

        if (r >= FIRM_PRESSURE_LOW && r <= FIRM_PRESSURE_HIGH)
            mGraphPaint1.setColor(0xFF33FF33);
        else if (r < FIRM_PRESSURE_LOW)
            mGraphPaint1.setColor(0xFF808080);
        else
            mGraphPaint1.setColor(0xFFFF8000);

        String s = (r < 0) ? "--"
                : String.format("%s %.4f", ((st.getLastTool() == MotionEvent.TOOL_TYPE_STYLUS) ? "S" : "F"), r);

        graph.drawText(s, left, bottom - 2, mGraphPaint1);

        if (mGraphX + COLUMN_WIDTH > graph.getWidth()) {
            mGraphX = 0;
            graph.save();
            graph.clipRect(new Rect(30, 0, graph.getWidth(), graph.getHeight()));
            graph.drawColor(0, PorterDuff.Mode.CLEAR);
            graph.restore();
        }

        if (r >= 0) {
            int barsize = (int) (r * ROW_HEIGHT);
            graph.drawRect(mGraphX + COLUMN_WIDTH, bottom - barsize, mGraphX + COLUMN_WIDTH + STEP, bottom,
                    mGraphPaint1);
        } else {
            graph.drawPoint(mGraphX + COLUMN_WIDTH + STEP, bottom, mGraphPaint1);
        }
        bottom -= (ROW_HEIGHT + ROW_MARGIN);
    }

    mGraphX += STEP;

    final int x = 96;
    final int y = 64;

    c.drawBitmap(mStrokeDebugGraph, x, y, null);
    invalidate(new Rect(x, y, x + c.getWidth(), y + c.getHeight()));
}

From source file:com.android.launcher2.AsyncTaskCallback.java

private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
    Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
    final Canvas c = mCachedShortcutPreviewCanvas.get();
    if (tempBitmap == null || tempBitmap.getWidth() != maxWidth || tempBitmap.getHeight() != maxHeight) {
        tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
        mCachedShortcutPreviewBitmap.set(tempBitmap);
    } else {//ww w .  ja  v  a 2 s .co m
        c.setBitmap(tempBitmap);
        c.drawColor(0, PorterDuff.Mode.CLEAR);
        c.setBitmap(null);
    }
    // Render the icon
    Drawable icon = mIconCache.getFullResIcon(info);

    int paddingTop = getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
    int paddingLeft = getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
    int paddingRight = getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);

    int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);

    renderDrawableToBitmap(icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);

    Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
    c.setBitmap(preview);
    Paint p = mCachedShortcutPreviewPaint.get();
    if (p == null) {
        p = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        p.setAlpha((int) (255 * 0.06f));
        //float density = 1f;
        //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
        mCachedShortcutPreviewPaint.set(p);
    }
    c.drawBitmap(tempBitmap, 0, 0, p);
    c.setBitmap(null);

    renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);

    return preview;
}

From source file:net.toload.main.hd.candidate.CandidateView.java

private void doDraw(Canvas canvas) {

    if (mSuggestions == null)
        return;//from   w ww.ja va 2s .  c  om
    if (DEBUG)
        Log.i(TAG, "CandidateView:doDraw():Suggestion mCount:" + mCount + " mSuggestions.size:"
                + mSuggestions.size());
    mTotalWidth = 0;

    updateFontSize();

    if (mBgPadding == null) {
        mBgPadding = new Rect(0, 0, 0, 0);
        if (getBackground() != null) {
            getBackground().getPadding(mBgPadding);
        }
    }

    final int height = mHeight;
    final Rect bgPadding = mBgPadding;
    final Paint candidatePaint = mCandidatePaint;
    final Paint candidateEmojiPaint = mCandidatePaint;
    candidateEmojiPaint.setTextSize((float) (candidateEmojiPaint.getTextSize() * 0.9));

    final Paint selKeyPaint = mSelKeyPaint;
    final int touchX = mTouchX;
    final int scrollX = getScrollX();
    final boolean scrolled = mScrolled;

    final int textBaseLine = (int) (((height - mCandidatePaint.getTextSize()) / 2) - mCandidatePaint.ascent());

    // Modified by jeremy '10, 3, 29.  Update mselectedindex if touched and build wordX[i] and wordwidth[i]
    int x = 0;
    final int count = mCount; //Cache count here '11,8,18
    for (int i = 0; i < count; i++) {
        if (count != mCount || mSuggestions == null || count != mSuggestions.size() || mSuggestions.size() == 0
                || i >= mSuggestions.size())
            return; // mSuggestion is updated, force abort

        String suggestion = mSuggestions.get(i).getWord();
        if (i == 0 && mSuggestions.size() > 1 && mSuggestions.get(1).isRuntimeBuiltPhraseRecord()
                && suggestion.length() > 8) {
            suggestion = suggestion.substring(0, 2) + "..";
        }
        float base = (suggestion == null) ? 0 : candidatePaint.measureText("");
        float textWidth = (suggestion == null) ? 0 : candidatePaint.measureText(suggestion);

        if (textWidth < base) {
            textWidth = base;
        }

        final int wordWidth = (int) textWidth + X_GAP * 2;

        mWordX[i] = x;

        mWordWidth[i] = wordWidth;

        if (touchX + scrollX >= x && touchX + scrollX < x + wordWidth && !scrolled) {
            mSelectedIndex = i;
        }
        x += wordWidth;
    }

    mTotalWidth = x;

    if (DEBUG)
        Log.i(TAG,
                "CandidateView:doDraw():mTotalWidth :" + mTotalWidth + "  this.getWidth():" + this.getWidth());

    //Jeremy '11,8,11. If the candidate list is within 1 page and has more records, get full records first.
    if (mTotalWidth < this.getWidth())
        checkHasMoreRecords();

    // Paint all the suggestions and lines.
    if (canvas != null) {

        // Moved from above by jeremy '10 3, 29. Paint mSelectedindex in highlight here
        if (count > 0 && mSelectedIndex >= 0) {
            //    candidatePaint.setColor(mColorComposingCode);
            //    canvas.drawRect(mWordX[mSelectedIndex],bgPadding.top, mWordWidth[mSelectedIndex] , height, candidatePaint);

            canvas.translate(mWordX[mSelectedIndex], 0);
            mDrawableSuggestHighlight.setBounds(0, bgPadding.top, mWordWidth[mSelectedIndex], height);
            mDrawableSuggestHighlight.draw(canvas);
            canvas.translate(-mWordX[mSelectedIndex], 0);

        }
        if (mTransparentCandidateView) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

            Paint backgroundPaint = new Paint();
            backgroundPaint.setColor(ContextCompat.getColor(mContext, R.color.third_background_light));
            backgroundPaint.setAlpha(33);
            backgroundPaint.setStyle(Paint.Style.FILL);

            canvas.drawRect(0.5f, bgPadding.top, mScreenWidth, height, backgroundPaint);
        }

        for (int i = 0; i < count; i++) {

            if (count != mCount || mSuggestions == null || count != mSuggestions.size()
                    || mSuggestions.size() == 0 || i >= mSuggestions.size())
                break;

            boolean isEmoji = mSuggestions.get(i).isEmojiRecord();
            String suggestion = mSuggestions.get(i).getWord();
            if (i == 0 && mSuggestions.size() > 1 && mSuggestions.get(1).isRuntimeBuiltPhraseRecord()
                    && suggestion.length() > 8) {
                suggestion = suggestion.substring(0, 2) + "..";
            }

            int c = i + 1;
            switch (mSuggestions.get(i).getRecordType()) {
            case Mapping.RECORD_COMPOSING_CODE:
                if (mSelectedIndex == 0) {

                    if (mTransparentCandidateView) {
                        candidatePaint.setColor(mColorInvertedTextTransparent);
                    } else {
                        candidatePaint.setColor(mColorComposingCodeHighlight);
                    }
                } else
                    candidatePaint.setColor(mColorComposingCode);
                break;
            case Mapping.RECORD_CHINESE_PUNCTUATION_SYMBOL:
            case Mapping.RECORD_RELATED_PHRASE:
                selKeyPaint.setColor(mColorSelKeyShifted);
                if (i == mSelectedIndex)
                    candidatePaint.setColor(mColorNormalTextHighlight);
                else
                    candidatePaint.setColor(mColorNormalText);
                break;
            case Mapping.RECORD_EXACT_MATCH_TO_CODE:
            case Mapping.RECORD_PARTIAL_MATCH_TO_CODE:
            case Mapping.RECORD_RUNTIME_BUILT_PHRASE:
            case Mapping.RECORD_ENGLISH_SUGGESTION:
            default:
                selKeyPaint.setColor(mColorSelKey);
                if (i == mSelectedIndex)
                    candidatePaint.setColor(mColorNormalTextHighlight);
                else
                    candidatePaint.setColor(mColorNormalText);
                break;

            }

            if (isEmoji) {
                canvas.drawText(suggestion, mWordX[i] + X_GAP, Math.round(textBaseLine * 0.95),
                        candidateEmojiPaint);
            } else {
                canvas.drawText(suggestion, mWordX[i] + X_GAP, textBaseLine, candidatePaint);
            }
            if (mShowNumber) {
                //Jeremy '11,6,17 changed from <=10 to mDisplaySelkey length. The length maybe 11 or 12 if shifted with space.
                if (c <= mDisplaySelkey.length()) {
                    //Jeremy '11,6,11 Drawing text using relative font dimensions.
                    canvas.drawText(mDisplaySelkey.substring(c - 1, c),
                            mWordX[i] + mWordWidth[i] - height * 0.3f, height * 0.4f, selKeyPaint);
                }
            }
            //Draw spacer
            candidatePaint.setColor(mColorSpacer);
            canvas.drawLine(mWordX[i] + mWordWidth[i] + 0.5f, bgPadding.top + (mVerticalPadding / 2),
                    mWordX[i] + mWordWidth[i] + 0.5f, height - (mVerticalPadding / 2), candidatePaint);
            candidatePaint.setFakeBoldText(false);

        }

        if (mTargetScrollX != getScrollX()) {
            if (DEBUG)
                Log.i(TAG, "CandidateView:doDraw():mTargetScrollX :" + mTargetScrollX + "  getScrollX():"
                        + getScrollX());
            scrollToTarget();
        }

    }

}