下面列出了android.graphics.Canvas#drawRect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
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);
}
@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);
}
@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);
}
}
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);
}
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);
}
@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);
}
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);
}
}
/**
* 改变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;
}
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();
}
/**
* 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;
}
}
@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);
}
@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();
}
}
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;
}
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);
}
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);
}
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);
}
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);
}
@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();
}
}