Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

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

Prototype

public void drawRect(@NonNull Rect r, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:com.raspi.chatapp.util.Notification.java

private Bitmap getLargeIcon(int bgColor, char letter, float width, boolean round) {
    Bitmap b = Bitmap.createBitmap((int) width, (int) width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    RectF mInnerRectF = new RectF();
    mInnerRectF.set(0, 0, width, width);
    mInnerRectF.offset(0, 0);//from ww  w.  ja v  a2  s.  c  o  m

    Paint mBgPaint = new Paint();
    mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setStyle(Paint.Style.FILL);
    mBgPaint.setColor(bgColor);

    TextPaint mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(Color.WHITE);
    mTitleTextPaint.setTextSize(width * 0.8f);

    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    int xPos = (int) centerX;
    int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);

    if (round)
        c.drawOval(mInnerRectF, mBgPaint);
    else
        c.drawRect(mInnerRectF, mBgPaint);
    c.drawText(String.valueOf(letter), xPos, yPos, mTitleTextPaint);

    return b;
}

From source file:com.android.leanlauncher.WidgetPreviewLoader.java

public Bitmap generateWidgetPreview(AppWidgetProviderInfo info, int cellHSpan, int cellVSpan,
        int maxPreviewWidth, int maxPreviewHeight, Bitmap preview, int[] preScaledWidthOut) {
    // Load the preview image if possible
    if (maxPreviewWidth < 0)
        maxPreviewWidth = Integer.MAX_VALUE;

    Drawable drawable = null;/*ww  w . j  av  a 2 s . co  m*/
    if (info.previewImage != 0) {
        drawable = mManager.loadPreview(info);
        if (drawable != null) {
            drawable = mutateOnMainThread(drawable);
        } else {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(info.previewImage)
                    + " for provider: " + info.provider);
        }
    }

    int previewWidth;
    int previewHeight;
    Bitmap defaultPreview = null;
    boolean widgetPreviewExists = (drawable != null);
    if (widgetPreviewExists) {
        previewWidth = drawable.getIntrinsicWidth();
        previewHeight = drawable.getIntrinsicHeight();
    } else {
        // Generate a preview image if we couldn't load one
        if (cellHSpan < 1)
            cellHSpan = 1;
        if (cellVSpan < 1)
            cellVSpan = 1;

        // This Drawable is not directly drawn, so there's no need to mutate it.
        BitmapDrawable previewDrawable = (BitmapDrawable) mContext.getResources()
                .getDrawable(R.drawable.widget_tile);
        final int previewDrawableWidth = previewDrawable.getIntrinsicWidth();
        final int previewDrawableHeight = previewDrawable.getIntrinsicHeight();
        previewWidth = previewDrawableWidth * cellHSpan;
        previewHeight = previewDrawableHeight * cellVSpan;

        defaultPreview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        c.setBitmap(defaultPreview);
        Paint p = mDefaultAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setShader(new BitmapShader(previewDrawable.getBitmap(), Shader.TileMode.REPEAT,
                    Shader.TileMode.REPEAT));
            mDefaultAppWidgetPreviewPaint.set(p);
        }
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        dest.set(0, 0, previewWidth, previewHeight);
        c.drawRect(dest, p);
        c.setBitmap(null);

        // Draw the icon in the top left corner
        int minOffset = (int) (mAppIconSize * WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE);
        int smallestSide = Math.min(previewWidth, previewHeight);
        float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);

        try {
            Bitmap icon = mIconCache.getIconForComponent(info.configure, UserHandleCompat.myUserHandle());
            if (icon != null) {
                int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
                int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
                renderBitmapIconOnPreview(icon, defaultPreview, hoffset, yoffset,
                        (int) (mAppIconSize * iconScale), (int) (mAppIconSize * iconScale));
            }
        } catch (Resources.NotFoundException ignored) {
        }
    }

    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    float scale = 1f;
    if (preScaledWidthOut != null) {
        preScaledWidthOut[0] = previewWidth;
    }
    if (previewWidth > maxPreviewWidth) {
        scale = maxPreviewWidth / (float) previewWidth;
    }
    if (scale != 1f) {
        previewWidth = (int) (scale * previewWidth);
        previewHeight = (int) (scale * previewHeight);
    }

    // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size
    if (preview == null) {
        preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
    }

    // Draw the scaled preview into the final bitmap
    int x = (preview.getWidth() - previewWidth) / 2;
    if (widgetPreviewExists) {
        renderDrawableToBitmap(drawable, preview, x, 0, previewWidth, previewHeight);
    } else {
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        final Rect src = mCachedAppWidgetPreviewSrcRect.get();
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        c.setBitmap(preview);
        src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
        dest.set(x, 0, x + previewWidth, previewHeight);

        Paint p = mCachedAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setFilterBitmap(true);
            mCachedAppWidgetPreviewPaint.set(p);
        }
        c.drawBitmap(defaultPreview, src, dest, p);
        c.setBitmap(null);
    }
    return preview;
}

From source file:de.madvertise.android.sdk.MadvertiseView.java

/**
 * Draw the ad background for a text banner
 *
 * @param canvas/*  w  w w .j a v a2s.co m*/
 * @param rectangle
 * @param backgroundColor
 * @param mTextColor
 */
private void drawTextBannerBackground(final Canvas canvas, final Rect rectangle, final int backgroundColor,
        int shineColor) {
    Paint paint = new Paint();
    paint.setColor(backgroundColor);
    paint.setAntiAlias(true);
    canvas.drawRect(rectangle, paint);

    int upperColor = Color.argb(GRADIENT_TOP_ALPHA, Color.red(shineColor), Color.green(shineColor),
            Color.blue(shineColor));
    int[] gradientColors = { upperColor, shineColor };
    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            gradientColors);

    int stop = (int) (rectangle.height() * GRADIENT_STOP) + rectangle.top;
    gradientDrawable.setBounds(rectangle.left, rectangle.top, rectangle.right, stop);
    gradientDrawable.draw(canvas);

    Rect shadowRect = new Rect(rectangle.left, stop, rectangle.right, rectangle.bottom);
    Paint shadowPaint = new Paint();
    shadowPaint.setColor(shineColor);
    canvas.drawRect(shadowRect, shadowPaint);
}

From source file:com.blestep.sportsbracelet.view.TimelineChartViewSleep.java

/**
 * {@inheritDoc}/*from  www. j a  v  a2s. c  om*/
 */
@Override
protected void onDraw(Canvas c) {
    // 
    c.clipRect(mViewArea);
    mViewAreaBgShader = new LinearGradient(0, 0, 0, mViewArea.height(),
            ContextCompat.getColor(getContext(), R.color.tlcSleepGraphBgColorStart),
            ContextCompat.getColor(getContext(), R.color.tlcSleepGraphBgColorEnd), Shader.TileMode.CLAMP);
    mViewAreaBgPaint.setShader(mViewAreaBgShader);
    // 
    c.drawRect(mViewArea, mViewAreaBgPaint);
    c.drawRect(mGraphArea, mGraphAreaBgPaint);
    c.drawRect(mFooterArea, mFooterAreaBgPaint);
    // 
    c.drawLine(0, mGraphArea.height(), mGraphArea.width(), mGraphArea.height(), mGraphBottomLinePaint);

    final SparseArray<Object[]> data;
    final double maxValue;
    synchronized (mLock) {
        data = mData;
        maxValue = mMaxValue;
    }
    boolean hasData = data.size() > 0;
    if (hasData) {

        // ???
        computeItemsOnScreen(data.size());
        // 
        drawBarItems(c, data, maxValue);
        // 
        drawTickLabels(c, data);

    }
}

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

/**
 * {@inheritDoc}/*from www  . j  a  v a2 s .  c o  m*/
 */
@Override
protected void onDraw(Canvas c) {
    // 
    c.clipRect(mViewArea);
    mViewAreaBgShader = new LinearGradient(0, 0, 0, mViewArea.height(),
            ContextCompat.getColor(getContext(), R.color.tlcStepsGraphBgColorStart),
            ContextCompat.getColor(getContext(), R.color.tlcStepsGraphBgColorEnd), Shader.TileMode.CLAMP);
    mViewAreaBgPaint.setShader(mViewAreaBgShader);
    // 
    c.drawRect(mViewArea, mViewAreaBgPaint);
    c.drawRect(mGraphArea, mGraphAreaBgPaint);
    c.drawRect(mFooterArea, mFooterAreaBgPaint);
    // 
    c.drawLine(0, mGraphArea.height(), mGraphArea.width(), mGraphArea.height(), mGraphBottomLinePaint);

    final SparseArray<Object[]> data;
    final double maxValue;
    synchronized (mLock) {
        data = mData;
        if (mIsShowTargetDashedLine && mTargetValue > mMaxValue) {
            maxValue = mTargetValue;
        } else {
            maxValue = mMaxValue;
        }
    }
    boolean hasData = data.size() > 0;
    if (hasData) {

        // ???
        computeItemsOnScreen(data.size());
        // 
        drawTargetLine(c);
        // 
        drawBarItems(c, data, maxValue);
        // 
        drawTickLabels(c, data);

    }
}

From source file:com.appsummary.luoxf.myappsummary.navigationtabstrip.NavigationTabStrip.java

@Override
protected void onDraw(final Canvas canvas) {
    // Set bound of strip
    mStripBounds.set(mStripLeft - (mStripType == StripType.POINT ? mStripWeight * 0.5F : 0.0F),
            mStripGravity == StripGravity.BOTTOM ? mBounds.height() - mStripWeight : 0.0F,
            mStripRight - (mStripType == StripType.POINT ? mStripWeight * 0.5F : 0.0F),
            mStripGravity == StripGravity.BOTTOM ? mBounds.height() : mStripWeight);

    // Draw strip
    if (mCornersRadius == 0)
        canvas.drawRect(mStripBounds, mStripPaint);
    else/*from w  w w  .  ja  va 2s  . c o  m*/
        canvas.drawRoundRect(mStripBounds, mCornersRadius, mCornersRadius, mStripPaint);

    // Draw tab titles
    for (int i = 0; i < mTitles.length; i++) {
        final String title = mTitles[i];

        final float leftTitleOffset = (mTabSize * i) + (mTabSize * 0.5F);

        mTitlePaint.getTextBounds(title, 0, title.length(), mTitleBounds);
        final float topTitleOffset = (mBounds.height() - mStripWeight) * 0.5F + mTitleBounds.height() * 0.5F
                - mTitleBounds.bottom;

        // Get interpolated fraction for left last and current tab
        final float interpolation = mResizeInterpolator.getResizeInterpolation(mFraction, true);
        final float lastInterpolation = mResizeInterpolator.getResizeInterpolation(mFraction, false);

        // Check if we handle tab from touch on NTS or from ViewPager
        // There is a strange logic of ViewPager onPageScrolled method, so it is
        if (mIsSetIndexFromTabBar) {
            if (mIndex == i)
                updateCurrentTitle(interpolation);
            else if (mLastIndex == i)
                updateLastTitle(lastInterpolation);
            else
                updateInactiveTitle();
        } else {
            if (i != mIndex && i != mIndex + 1)
                updateInactiveTitle();
            else if (i == mIndex + 1)
                updateCurrentTitle(interpolation);
            else if (i == mIndex)
                updateLastTitle(lastInterpolation);
        }

        canvas.drawText(title, leftTitleOffset,
                topTitleOffset + (mStripGravity == StripGravity.TOP ? mStripWeight : 0.0F), mTitlePaint);
    }
}

From source file:com.tiange.hz.wheelview.widget.WheelView.java

/**
 * Draws rect for current value/*from  w ww  . j a  va  2 s . c  o  m*/
 * @param canvas the canvas for drawing
 */
private void drawCenterRect(Canvas canvas) {
    int center = getHeight() / 2;
    int offset = (int) (getItemHeight() / 2 * 1.2);
    if (centerDrawable != null) {
        centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);
        centerDrawable.draw(canvas);
    }
    //dip2px
    if (wheelForegroundColor != Color.TRANSPARENT) {
        Paint paint = new Paint();
        paint.setColor(wheelForegroundColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(new Rect(0, center - offset, getWidth(), center + offset), paint);
    }

    if (wheelForeDividerColor != Color.TRANSPARENT) {
        //wheelForeDividerWidth==

        Paint paint = new Paint();
        paint.setColor(wheelForeDividerColor);
        canvas.drawLine(0, center - offset, getWidth(), center - offset, paint);
        canvas.drawLine(0, center + offset, getWidth(), center + offset, paint);
    }
}

From source file:net.networksaremadeofstring.rhybudd.ZenossWidgetGraph.java

private Bitmap RenderBarGraph(int CritCount, int ErrCount, int WarnCount) {

    //Log.i("Counts", Integer.toString(CritCount) + " / " + Integer.toString(ErrCount) + " / " + Integer.toString(WarnCount));
    Bitmap emptyBmap = Bitmap.createBitmap(290, 150, Config.ARGB_8888);

    int width = emptyBmap.getWidth();
    int height = emptyBmap.getHeight();
    Bitmap charty = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(charty);
    //final int color = 0xff0B0B61; 
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);//from  w  ww .j  a  v  a  2 s  .c  o m

    //y
    canvas.drawLine(25, 0, 25, 289, paint);
    //x
    canvas.drawLine(25, 149, 289, 149, paint);

    paint.setAntiAlias(true);
    int Max = 0;

    if (CritCount > ErrCount && CritCount > WarnCount)
        Max = CritCount;
    else if (ErrCount > CritCount && ErrCount > WarnCount)
        Max = ErrCount;
    else if (WarnCount > CritCount && WarnCount > ErrCount)
        Max = WarnCount;
    else
        Max = CritCount;

    if (Max > 0)
        canvas.drawText(Integer.toString(Max), 0, 10, paint);

    if (Max > 1)
        canvas.drawText(Integer.toString(Max / 2), 0, 75, paint);

    canvas.drawText("0", 0, 148, paint);

    double divisor = 148 / (double) Max;

    paint.setAlpha(128);

    Rect rect = new Rect(32, (int) (148 - (divisor * CritCount)), 64, 148);
    paint.setColor(Color.argb(200, 208, 0, 0)); //red

    if (CritCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    rect = new Rect(128, (int) (148 - (divisor * ErrCount)), 160, 148);
    paint.setColor(Color.argb(200, 255, 102, 0));//orange

    if (ErrCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    rect = new Rect(224, (int) (148 - (divisor * WarnCount)), 256, 148);
    paint.setColor(Color.argb(200, 255, 224, 57)); //yellow
    if (WarnCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    //Return
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    charty.compress(CompressFormat.PNG, 50, out);

    return BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
}

From source file:cn.androidy.androiddevelopmentpatterns.interactivechart.InteractiveLineGraphView.java

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

    // Draws axes and text labels
    drawAxes(canvas);//from w  w w .j  a  va 2  s . c o m

    // Clips the next few drawing operations to the content area
    int clipRestoreCount = canvas.save();
    canvas.clipRect(mContentRect);

    drawDataSeriesUnclipped(canvas);
    drawEdgeEffectsUnclipped(canvas);

    // Removes clipping rectangle
    canvas.restoreToCount(clipRestoreCount);

    // Draws chart container
    canvas.drawRect(mContentRect, mAxisPaint);
}