Java Code Examples for android.graphics.Canvas#drawRect()

The following examples show how to use android.graphics.Canvas#drawRect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScannerFinderView.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    if (isInEditMode()) {
        return;
    }
    Rect frame = mFrameRect;
    if (frame == null) {
        return;
    }
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    // 绘制焦点框外边的暗色背景
    mPaint.setColor(mMaskColor);
    canvas.drawRect(0, 0, width, frame.top, mPaint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, mPaint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, mPaint);
    canvas.drawRect(0, frame.bottom + 1, width, height, mPaint);

    drawFocusRect(canvas, frame);
    drawAngle(canvas, frame);
    drawText(canvas, frame);
    drawLaser(canvas, frame);
}
 
Example 2
Source File: NeopixelComponentWBar.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    // Draw the bar.
    canvas.drawRect(mBarRect, mBarPaint);

    // Calculate the center of the pointer.
    int cX, cY;
    if (mOrientation == ORIENTATION_HORIZONTAL) {
        cX = mBarPointerPosition;
        cY = mBarPointerHaloRadius;
    } else {
        cX = mBarPointerHaloRadius;
        cY = mBarPointerPosition;
    }

    // Draw the pointer halo.
    canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint);
    // Draw the pointer.
    canvas.drawCircle(cX, cY, mBarPointerRadius, mBarPointerPaint);
}
 
Example 3
Source File: NumberProgressBar.java    From HHComicViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

  if (mIfDrawText) {
    calculateDrawRectF();
  } else {
    calculateDrawRectFWithoutProgressText();
  }

  if (mDrawReachedBar) {
    canvas.drawRect(mReachedRectF, mReachedBarPaint);
  }

  if (mDrawUnreachedBar) {
    canvas.drawRect(mUnreachedRectF, mUnreachedBarPaint);
  }

  if (mIfDrawText) {
    canvas.drawText(mCurrentDrawText, mDrawTextStart, mDrawTextEnd, mTextPaint);
  }
}
 
Example 4
Source File: ColorPanelView.java    From HomeGenie-Android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	
	final RectF	rect = mColorRect;
			
	if(BORDER_WIDTH_PX > 0){
		mBorderPaint.setColor(mBorderColor);
		canvas.drawRect(mDrawingRect, mBorderPaint);		
	}
	
	if(mAlphaPattern != null){
		mAlphaPattern.draw(canvas);
	}
				
	mColorPaint.setColor(mColor);
	
	canvas.drawRect(rect, mColorPaint);
}
 
Example 5
Source File: CropAreaView.java    From GestureViews with Apache License 2.0 6 votes vote down vote up
private void drawRectHole(Canvas canvas) {
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(backColor);

    // Top part
    tmpRectF.set(0f, 0f, canvas.getWidth(), areaRect.top);
    canvas.drawRect(tmpRectF, paint);

    // Bottom part
    tmpRectF.set(0f, areaRect.bottom, canvas.getWidth(), canvas.getHeight());
    canvas.drawRect(tmpRectF, paint);

    // Left part
    tmpRectF.set(0f, areaRect.top, areaRect.left, areaRect.bottom);
    canvas.drawRect(tmpRectF, paint);

    // Right part
    tmpRectF.set(areaRect.right, areaRect.top, canvas.getWidth(), areaRect.bottom);
    canvas.drawRect(tmpRectF, paint);
}
 
Example 6
Source File: CropOverlayView.java    From cloudinary_android with MIT License 5 votes vote down vote up
private void dimSurroundingBackground(Canvas canvas) {
    // left
    canvas.drawRect(0, overlay.top - HANDLE_OFFSET_FROM_OVERLAY, overlay.left - HANDLE_OFFSET_FROM_OVERLAY, overlay.bottom + HANDLE_OFFSET_FROM_OVERLAY, dimBackgroundPaint);
    // top
    canvas.drawRect(0, 0, getWidth(), overlay.top - HANDLE_OFFSET_FROM_OVERLAY, dimBackgroundPaint);
    // right
    canvas.drawRect(overlay.right + HANDLE_OFFSET_FROM_OVERLAY, overlay.top - HANDLE_OFFSET_FROM_OVERLAY, getWidth(), overlay.bottom + HANDLE_OFFSET_FROM_OVERLAY, dimBackgroundPaint);
    // bottom
    canvas.drawRect(0, overlay.bottom + HANDLE_OFFSET_FROM_OVERLAY, getWidth(), getHeight(), dimBackgroundPaint);
}
 
Example 7
Source File: CanvasDrawer.java    From DrawableView with Apache License 2.0 5 votes vote down vote up
public void onDraw(Canvas canvas) {
  canvasLogger.log(canvas, canvasRect, viewRect, scaleFactor);
  if (showCanvasBounds) {
    canvas.drawRect(canvasRect, paint);
  }
  canvas.translate(-viewRect.left, -viewRect.top);
  canvas.scale(scaleFactor, scaleFactor);
}
 
Example 8
Source File: RoundedCornersTransformation.java    From glide-transformations with Apache License 2.0 5 votes vote down vote up
private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
                                               float bottom) {
  canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
      radius, paint);
  canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
      radius, paint);
  canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
  canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
}
 
Example 9
Source File: ImageLoader.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap createUnknownImage(int size, int primaryColor, String topText, String bottomText) {
	Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
	Canvas canvas = new Canvas(bitmap);

	Paint color = new Paint();
	color.setColor(primaryColor);
	canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color);

	color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK, Shader.TileMode.MIRROR));
	canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color);

	if(topText != null || bottomText != null) {
		Paint font = new Paint();
		font.setFlags(Paint.ANTI_ALIAS_FLAG);
		font.setColor(Color.WHITE);
		font.setTextSize(3.0f + size * 0.07f);

		if(topText != null) {
			canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
		}

		if(bottomText != null) {
			canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
		}
	}

	return bitmap;
}
 
Example 10
Source File: RoundVideoPlayingDrawable.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    paint.setColor(Theme.getColor(Theme.key_chat_mediaTimeText));
    int x = getBounds().left;
    int y = getBounds().top;
    for (int a = 0; a < 3; a++) {
        canvas.drawRect(x + AndroidUtilities.dp(2), y + AndroidUtilities.dp(2 + 7 * progress1), x + AndroidUtilities.dp(4), y + AndroidUtilities.dp(10), paint);
        canvas.drawRect(x + AndroidUtilities.dp(5), y + AndroidUtilities.dp(2 + 7 * progress2), x + AndroidUtilities.dp(7), y + AndroidUtilities.dp(10), paint);
        canvas.drawRect(x + AndroidUtilities.dp(8), y + AndroidUtilities.dp(2 + 7 * progress3), x + AndroidUtilities.dp(10), y + AndroidUtilities.dp(10), paint);
    }
    if (started) {
        update();
    }
}
 
Example 11
Source File: SlidingTabStrip.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    final int height = getHeight();
    final int childCount = getChildCount();
    final SlidingTabLayout.TabColorizer tabColorizer = mCustomTabColorizer != null
            ? mCustomTabColorizer
            : mDefaultTabColorizer;

    // Thick colored underline below the current selection
    if (childCount > 0) {
        View selectedTitle = getChildAt(mSelectedPosition);
        int left = selectedTitle.getLeft();
        int right = selectedTitle.getRight();
        int color = tabColorizer.getIndicatorColor(mSelectedPosition);

        if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) {
            int nextColor = tabColorizer.getIndicatorColor(mSelectedPosition + 1);
            if (color != nextColor) {
                color = blendColors(nextColor, color, mSelectionOffset);
            }

            // Draw the selection partway between the tabs
            View nextTitle = getChildAt(mSelectedPosition + 1);
            left = (int) (mSelectionOffset * nextTitle.getLeft() +
                    (1.0f - mSelectionOffset) * left);
            right = (int) (mSelectionOffset * nextTitle.getRight() +
                    (1.0f - mSelectionOffset) * right);
        }

        mSelectedIndicatorPaint.setColor(color);

        canvas.drawRect(left, height - mSelectedIndicatorThickness, right,
                height, mSelectedIndicatorPaint);
    }

    // Thin underline along the entire bottom edge
    canvas.drawRect(0, height - mBottomBorderThickness, getWidth(), height, mBottomBorderPaint);
}
 
Example 12
Source File: AlphaPatternDrawable.java    From Lucid-Browser with Apache License 2.0 5 votes vote down vote up
/**
 * This will generate a bitmap with the pattern 
 * as big as the rectangle we were allow to draw on.
 * We do this to chache the bitmap so we don't need to
 * recreate it each time draw() is called since it 
 * takes a few milliseconds.
 */
private void generatePatternBitmap(){
	
	if(getBounds().width() <= 0 || getBounds().height() <= 0){
		return;
	}
	
	mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);			
	Canvas canvas = new Canvas(mBitmap);
	
	Rect r = new Rect();
	boolean verticalStartWhite = true;
	for (int i = 0; i <= numRectanglesVertical; i++) {

		boolean isWhite = verticalStartWhite;
		for (int j = 0; j <= numRectanglesHorizontal; j++) {

			r.top = i * mRectangleSize;
			r.left = j * mRectangleSize;
			r.bottom = r.top + mRectangleSize;
			r.right = r.left + mRectangleSize;
			
			canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);

			isWhite = !isWhite;
		}

		verticalStartWhite = !verticalStartWhite;

	}
	
}
 
Example 13
Source File: ReflectItemView.java    From Android-tv-widget with Apache License 2.0 5 votes vote down vote up
public void drawReflection4_3_18(Canvas canvas) {
    canvas.save();
    canvas.clipRect(0, 0, getWidth(), mRefHeight);
    canvas.save();
    canvas.scale(1, -1);
    canvas.translate(0, -getHeight());
    super.draw(canvas);
    canvas.restore();
    canvas.drawRect(0, 0, getWidth(), mRefHeight, mRefPaint);
    canvas.restore();
}
 
Example 14
Source File: BitmapUtil.java    From AndroidMathKeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * 改变Bitmap背景色
 * @param color
 * @param originBitmap
 * @return
 */
public static Bitmap drawBgBitmap(int color, Bitmap originBitmap) {
    Paint paint = new Paint();
    paint.setColor(color);
    Bitmap bitmap = Bitmap.createBitmap(originBitmap.getWidth(),
            originBitmap.getHeight(), originBitmap.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawRect(0, 0, originBitmap.getWidth(), originBitmap.getHeight(), paint);
    canvas.drawBitmap(originBitmap, 0, 0, paint);
    return bitmap;
}
 
Example 15
Source File: RoundedDrawable.java    From Android with MIT License 5 votes vote down vote up
private void redrawBitmapForSquareCorners(Canvas canvas) {
  if (all(mCornersRounded)) {
    // no square corners
    return;
  }

  if (mCornerRadius == 0) {
    return; // no round corners
  }

  float left = mDrawableRect.left;
  float top = mDrawableRect.top;
  float right = left + mDrawableRect.width();
  float bottom = top + mDrawableRect.height();
  float radius = mCornerRadius;

  if (!mCornersRounded[Corner.TOP_LEFT]) {
    mSquareCornersRect.set(left, top, left + radius, top + radius);
    canvas.drawRect(mSquareCornersRect, mBitmapPaint);
  }

  if (!mCornersRounded[Corner.TOP_RIGHT]) {
    mSquareCornersRect.set(right - radius, top, right, radius);
    canvas.drawRect(mSquareCornersRect, mBitmapPaint);
  }

  if (!mCornersRounded[Corner.BOTTOM_RIGHT]) {
    mSquareCornersRect.set(right - radius, bottom - radius, right, bottom);
    canvas.drawRect(mSquareCornersRect, mBitmapPaint);
  }

  if (!mCornersRounded[Corner.BOTTOM_LEFT]) {
    mSquareCornersRect.set(left, bottom - radius, left + radius, bottom);
    canvas.drawRect(mSquareCornersRect, mBitmapPaint);
  }
}
 
Example 16
Source File: MarkDownQuoteSpan.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end,
                              boolean first, Layout layout) {
    Paint.Style style = p.getStyle();
    int color = p.getColor();
    p.setStyle(Paint.Style.FILL);
    p.setColor(getColor());
    c.drawRect(x, top, x + dir * STRIPE_WIDTH, bottom, p);
    p.setStyle(style);
    p.setColor(color);
}
 
Example 17
Source File: CollapsingTextHelper.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
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;
        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }
        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta 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);
}
 
Example 18
Source File: TriangleFillDrawer.java    From meter with Apache License 2.0 4 votes vote down vote up
public void draw(Canvas c){
        super.draw(c);

        paint.setAntiAlias(true);

        // Background
        paint.setColor(color_background);
        c.drawRect(0, 0, c.getWidth(), c.getHeight(), paint);


        int x = c.getWidth()/2;
        int y = c.getHeight()/2 - (int)(30f*pixelDensity);


        float triangleSize = (float) (c.getWidth()*0.7);
        if(c.getWidth() > c.getHeight()){
            triangleSize = (float) (c.getHeight()*0.7);
        }

        if(connected) {
            // Outer triangle
            paint.setColor(color_triangle_background);
            c.drawPath(createTriangle(x, y, triangleSize, 1, false), paint);

            // Inner triangle
            paint.setColor(color_triangle_foreground);
            Path insidePath;
            if(_percent > 0.995){
                insidePath = createTriangle(x, y, triangleSize, _percent, false);
                vel = new Vector2D(0,0);
                pos = new Vector2D(0,1);
                _pos = new Vector2D(0,1);
            } else {
                insidePath = createInsideTriangle(x, y, triangleSize, _percent, false);
            }
            c.drawPath(insidePath, paint);
        } else {
            c.save();
            c.translate(x, y);
            c.rotate(180);
            paint.setColor(color_triangle_critical);
            c.drawPath(createTriangle(0, 0, triangleSize, 1, true), paint);
            c.restore();
        }

        // Text
/*        String labelPost = Integer.toString(Math.round(percent *100))+"%";
        if(!connected){
            labelPost = "Not connected";
        }*/

        if(DEBUG_DRAW){
            drawDebug(c, x, y);
        }

        drawText(label1, label2, x, (int) (y + triangleSize/2+10), c);
    }
 
Example 19
Source File: HoloCircularProgressBar.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {

	// All of our positions are using our internal coordinate system.
	// Instead of translating
	// them we let Canvas do the work for us.
	canvas.translate(mTranslationOffsetX, mTranslationOffsetY);

	final float progressRotation = getCurrentRotation();

	// draw the background
	if (!mOverrdraw) {
		canvas.drawArc(mCircleBounds, 270, -(360 - progressRotation), false, mBackgroundColorPaint);
	}

	// draw the progress or a full circle if overdraw is true
	canvas.drawArc(mCircleBounds, 270, mOverrdraw ? 360 : progressRotation, false, mProgressColorPaint);

	// draw the marker at the correct rotated position
	if (mIsMarkerEnabled) {
		final float markerRotation = getMarkerRotation();

		canvas.save();
		canvas.rotate(markerRotation - 90);
		canvas.drawLine((float) (mThumbPosX + mThumbRadius / 2 * 1.4), mThumbPosY,
				(float) (mThumbPosX - mThumbRadius / 2 * 1.4), mThumbPosY, mMarkerColorPaint);
		canvas.restore();
	}

	if (isThumbEnabled()) {
		// draw the thumb square at the correct rotated position
		canvas.save();
		canvas.rotate(progressRotation - 90);
		// rotate the square by 45 degrees
		canvas.rotate(45, mThumbPosX, mThumbPosY);
		mSquareRect.left = mThumbPosX - mThumbRadius / 3;
		mSquareRect.right = mThumbPosX + mThumbRadius / 3;
		mSquareRect.top = mThumbPosY - mThumbRadius / 3;
		mSquareRect.bottom = mThumbPosY + mThumbRadius / 3;
		canvas.drawRect(mSquareRect, mThumbColorPaint);
		canvas.restore();
	}
}
 
Example 20
Source File: HighlightView.java    From CloudPan with Apache License 2.0 4 votes vote down vote up
private void drawOutsideFallback(Canvas canvas) {
    canvas.drawRect(0, 0, canvas.getWidth(), drawRect.top, outsidePaint);
    canvas.drawRect(0, drawRect.bottom, canvas.getWidth(), canvas.getHeight(), outsidePaint);
    canvas.drawRect(0, drawRect.top, drawRect.left, drawRect.bottom, outsidePaint);
    canvas.drawRect(drawRect.right, drawRect.top, canvas.getWidth(), drawRect.bottom, outsidePaint);
}