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

The following examples show how to use android.graphics.Canvas#clipRect() . 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: ScrollPageAnim.java    From NovelReader with MIT License 6 votes vote down vote up
@Override
    public void draw(Canvas canvas) {
        //进行布局
        onLayout();

        //绘制背景
        canvas.drawBitmap(mBgBitmap, 0, 0, null);
        //绘制内容
        canvas.save();
        //移动位置
        canvas.translate(0, mMarginHeight);
        //裁剪显示区域
        canvas.clipRect(0, 0, mViewWidth, mViewHeight);
/*        //设置背景透明
        canvas.drawColor(0x40);*/
        //绘制Bitmap
        for (int i = 0; i < mActiveViews.size(); ++i) {
            tmpView = mActiveViews.get(i);
            canvas.drawBitmap(tmpView.bitmap, tmpView.srcRect, tmpView.destRect, null);
        }
        canvas.restore();
    }
 
Example 2
Source File: DragLayer.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    // Draw the background below children.
    if (mBackgroundAlpha > 0.0f) {
        // Update the scroll position first to ensure scrim cutout is in the right place.
        mLauncher.getWorkspace().computeScrollWithoutInvalidation();

        int alpha = (int) (mBackgroundAlpha * 255);
        CellLayout currCellLayout = mLauncher.getWorkspace().getCurrentDragOverlappingLayout();
        canvas.save();
        if (currCellLayout != null && currCellLayout != mLauncher.getHotseat().getLayout()) {
            // Cut a hole in the darkening scrim on the page that should be highlighted, if any.
            getDescendantRectRelativeToSelf(currCellLayout, mHighlightRect);
            canvas.clipRect(mHighlightRect, Region.Op.DIFFERENCE);
        }
        canvas.drawColor((alpha << 24) | SCRIM_COLOR);
        canvas.restore();
    }

    mFocusIndicatorHelper.draw(canvas);
    super.dispatchDraw(canvas);
}
 
Example 3
Source File: AlertDrawable.java    From BubbleAlert with MIT License 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {

    canvas.clipRect(borderRect);
    processPath(borderPath);
    canvas.drawPath(borderPath, mBackGroundPaint);
    canvas.drawCircle(borderRect.right / 2, borderRect.top + circleRadius, circleRadius, mOuterCircleFillPaint);
    canvas.drawCircle(borderRect.right / 2, borderRect.top + circleRadius, circleRadius - outerCircleOffset, mCircleFillPaint);

    float textHeight = mOuterCircleFillPaint.descent() - mOuterCircleFillPaint.ascent();
    float textOffset = (textHeight / 2) - mOuterCircleFillPaint.descent();
    if (null != bitmapIcon)
        canvas.drawBitmap(bitmapIcon, borderRect.right / 2 - bitmapIcon.getWidth() / 2,
                borderRect.top + circleRadius - bitmapIcon.getHeight() / 2, mBackGroundPaint);
    //canvas.drawText(mDrawText, borderRect.right / 2 - (mOuterCircleFillPaint.measureText(mDrawText)/2), borderRect.top + circleRadius + textOffset, mOuterCircleFillPaint);
}
 
Example 4
Source File: FreeScrollingTextField.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.save();

    //translate clipping region to create padding around edges
    canvas.clipRect(getScrollX() + getPaddingLeft(),
            getScrollY() + getPaddingTop(),
            getScrollX() + getWidth() - getPaddingRight(),
            getScrollY() + getHeight() - getPaddingBottom());
    canvas.translate(getPaddingLeft(), getPaddingTop());
    realDraw(canvas);

    canvas.restore();

    _navMethod.onTextDrawComplete(canvas);
}
 
Example 5
Source File: SimpleFooter.java    From ZrcListView with MIT License 6 votes vote down vote up
@Override
public boolean draw(Canvas canvas, int left, int top, int right, int bottom) {
    final int width = right - left;
    final int height = mHeight;
    final int viewHeight = bottom - top;
    canvas.save();
    canvas.clipRect(left + 5, top + 1, right + 5, bottom - 1);
    mPaint.setColor(mCircleColor);
    for (int i = 0; i < mPice; i++) {
        int angleParam = mTime * 5;
        float angle = -(i * (360 / mPice) - angleParam) * PI / 180;
        float radius = height / 4;
        float x = (float) (width / 2 + radius * Math.cos(angle));
        float y = (float) (top + Math.max(height, viewHeight) / 2 + radius * Math.sin(angle));
        canvas.drawCircle(x, y, height / 15, mPaint);
    }
    mTime++;
    canvas.restore();
    return true;
}
 
Example 6
Source File: WindmillDrawable.java    From Folivora with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
  final Rect bounds = getBounds();
  canvas.save();
  canvas.clipRect(bounds);
  canvas.translate(bounds.centerX(), bounds.centerY());
  canvas.rotate(mRotateDegrees);
  float radius = Math.min(bounds.width(), bounds.height()) / (float) 2;
  mRectF.set(0, -radius / 2, radius, radius / 2);
  for (int i = 0; i < mColors.length; i++) {
    mPaint.setColor(mColors[i]);
    canvas.rotate(90 * i);
    canvas.drawArc(mRectF, 0, 180, true, mPaint);
  }
  mPaint.setColor(mCenterDotColor);
  canvas.drawCircle(0, 0, mCenterDotRadius, mPaint);
  canvas.restore();
}
 
Example 7
Source File: AbsSeekBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
void drawTrack(Canvas canvas) {
    final Drawable thumbDrawable = mThumb;
    if (thumbDrawable != null && mSplitTrack) {
        final Insets insets = thumbDrawable.getOpticalInsets();
        final Rect tempRect = mTempRect;
        thumbDrawable.copyBounds(tempRect);
        tempRect.offset(mPaddingLeft - mThumbOffset, mPaddingTop);
        tempRect.left += insets.left;
        tempRect.right -= insets.right;

        final int saveCount = canvas.save();
        canvas.clipRect(tempRect, Op.DIFFERENCE);
        super.drawTrack(canvas);
        drawTickMarks(canvas);
        canvas.restoreToCount(saveCount);
    } else {
        super.drawTrack(canvas);
        drawTickMarks(canvas);
    }
}
 
Example 8
Source File: RevealLayout.java    From android-art-res with Apache License 2.0 5 votes vote down vote up
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    if (!mShouldDoAnimation || mTargetWidth <= 0 || mTouchTarget == null) {
        return;
    }

    if (mRevealRadius > mMinBetweenWidthAndHeight / 2) {
        mRevealRadius += mRevealRadiusGap * 4;
    } else {
        mRevealRadius += mRevealRadiusGap;
    }
    this.getLocationOnScreen(mLocationInScreen);
    int[] location = new int[2];
    mTouchTarget.getLocationOnScreen(location);
    int left = location[0] - mLocationInScreen[0];
    int top = location[1] - mLocationInScreen[1];
    int right = left + mTouchTarget.getMeasuredWidth();
    int bottom = top + mTouchTarget.getMeasuredHeight();

    canvas.save();
    canvas.clipRect(left, top, right, bottom);
    canvas.drawCircle(mCenterX, mCenterY, mRevealRadius, mPaint);
    canvas.restore();

    if (mRevealRadius <= mMaxRevealRadius) {
        postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
    } else if (!mIsPressed) {
        mShouldDoAnimation = false;
        postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
    }
}
 
Example 9
Source File: BossRHellfireBullet.java    From StormPlane with Apache License 2.0 5 votes vote down vote up
@Override
public void drawSelf(Canvas canvas) {
	if (isAlive) {
		canvas.save();
		canvas.clipRect(object_x, object_y, object_x + object_width,
				object_y + object_height);
		canvas.drawBitmap(bullet, object_x, object_y, paint);
		canvas.restore();
		logic();
	}
}
 
Example 10
Source File: SmoothProgressDrawable.java    From Klyph with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    mBounds = getBounds();
    canvas.clipRect(mBounds);

    int boundsWidth = mBounds.width();

    if (mReversed) {
        canvas.translate(boundsWidth, 0);
        canvas.scale(-1, 1);
    }

    drawStrokes(canvas);
}
 
Example 11
Source File: FreeScrollingTextField.java    From CodeEditor with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    //translate clipping region to create padding around edges
    canvas.clipRect(getScrollX() + getPaddingLeft(),
            getScrollY() + getPaddingTop(),
            getScrollX() + getWidth() - getPaddingRight(),
            getScrollY() + getHeight() - getPaddingBottom());
    canvas.translate(getPaddingLeft(), getPaddingTop());
    realDraw(canvas);

    canvas.restore();
    _navMethod.onTextDrawComplete(canvas);
}
 
Example 12
Source File: BossYHellfireBullet.java    From StormPlane with Apache License 2.0 5 votes vote down vote up
@Override
public void drawSelf(Canvas canvas) {
	if (isAlive) {
		canvas.save();
		canvas.clipRect(object_x, object_y, object_x + object_width,
				object_y + object_height);
		canvas.drawBitmap(bullet, object_x, object_y, paint);
		canvas.restore();
		logic();
	}
}
 
Example 13
Source File: DividerItemDecoration.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();
    int top;
    int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    int childCount = parent.getChildCount();

    for (int i = 0; i < childCount; ++i) {
        View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, this.mBounds);
        int right = this.mBounds.right + Math.round(child.getTranslationX());
        int left = right - this.mDivider.getIntrinsicWidth();
        this.mDivider.setBounds(left, top, right, bottom);
        this.mDivider.draw(canvas);
    }

    canvas.restore();
}
 
Example 14
Source File: PinnedSectionListView.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (mPinnedSection != null) {

        // prepare variables
        int pLeft = getListPaddingLeft();
        int pTop = getListPaddingTop();
        View view = mPinnedSection.view;

        // draw child
        canvas.save();

        int clipHeight = view.getHeight() +
                (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
        canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight);

        canvas.translate(pLeft, pTop + mTranslateY);
        drawChild(canvas, mPinnedSection.view, getDrawingTime());

        if (mShadowDrawable != null && mSectionsDistanceY > 0) {
            mShadowDrawable.setBounds(mPinnedSection.view.getLeft(),
                    mPinnedSection.view.getBottom(),
                    mPinnedSection.view.getRight(),
                    mPinnedSection.view.getBottom() + mShadowHeight);
            mShadowDrawable.draw(canvas);
        }

        canvas.restore();
    }
}
 
Example 15
Source File: ParallaxRecyclerAdapter.java    From Bailan with Apache License 2.0 5 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    if (mShouldClip) {
        canvas.clipRect(new Rect(getLeft(), getTop(), getRight(), getBottom() + mOffset));
    }
    super.dispatchDraw(canvas);
}
 
Example 16
Source File: RotationDrawStrategy.java    From styT with Apache License 2.0 5 votes vote down vote up
@Override
public void drawAppName(Canvas canvas, float fraction, String name, int colorOfAppName,
                        WidthAndHeightOfView widthAndHeightOfView) {
    canvas.save();
    int width = widthAndHeightOfView.getWidth();
    int height = widthAndHeightOfView.getHeight();
    canvas.clipRect(width / 2 - 150, height / 2 - 80,
            width / 2 + 150 * fraction, height / 2 + 65 * fraction);
    Paint paint = new Paint();
    paint.setColor(colorOfAppName);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(50);
    canvas.drawText(name, width / 2, height / 2 + 50, paint);
    canvas.restore();
}
 
Example 17
Source File: BigPlaneBullet.java    From StormPlane with Apache License 2.0 5 votes vote down vote up
@Override
public void drawSelf(Canvas canvas) {
	if (isAlive) {
		canvas.save();
		canvas.clipRect(object_x, object_y, object_x + object_width,
				object_y + object_height);
		canvas.drawBitmap(bullet, object_x, object_y, paint);
		canvas.restore();
		logic();
	}
}
 
Example 18
Source File: AllAppsRecyclerView.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * We need to override the draw to ensure that we don't draw the overscroll effect beyond the
 * background bounds.
 */
@Override
protected void dispatchDraw(Canvas canvas) {
    canvas.clipRect(mBackgroundPadding.left, mBackgroundPadding.top,
            getWidth() - mBackgroundPadding.right,
            getHeight() - mBackgroundPadding.bottom);
    super.dispatchDraw(canvas);
}
 
Example 19
Source File: CropImageView.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
/** 绘制焦点框 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (Style.RECTANGLE == mStyle) {
        mFocusPath.addRect(mFocusRect, Path.Direction.CCW);
        canvas.save();
        canvas.clipRect(0, 0, getWidth(), getHeight());
        canvas.clipPath(mFocusPath, Region.Op.DIFFERENCE);
        canvas.drawColor(mMaskColor);
        canvas.restore();
    } else if (Style.CIRCLE == mStyle) {
        float radius = Math.min((mFocusRect.right - mFocusRect.left) / 2, (mFocusRect.bottom - mFocusRect.top) / 2);
        mFocusPath.addCircle(mFocusMidPoint.x, mFocusMidPoint.y, radius, Path.Direction.CCW);
        canvas.save();
        canvas.clipRect(0, 0, getWidth(), getHeight());
        canvas.clipPath(mFocusPath, Region.Op.DIFFERENCE);
        canvas.drawColor(mMaskColor);
        canvas.restore();
    }
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setStrokeWidth(mBorderWidth);
    mBorderPaint.setAntiAlias(true);
    canvas.drawPath(mFocusPath, mBorderPaint);
    mFocusPath.reset();
}
 
Example 20
Source File: SplitImageView.java    From SplitImageView with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mForegroundDrawable == null && mBackgroundDrawable == null) {
        return; // couldn't resolve the URI
    }

    if (mMaxDrawableWidth == 0 || mMaxDrawableHeight == 0) {
        return;     // nothing to draw (empty bounds)
    }

    if (mDrawMatrix == null && getPaddingTop() == 0 && getPaddingLeft() == 0) {
        if (mBackgroundDrawable != null) {
            mBackgroundDrawable.draw(canvas);
        }

        if (mSplitDrawPath != null) {
            canvas.clipPath(mSplitDrawPath);
        }

        if (mForegroundDrawable != null) {
            mForegroundDrawable.draw(canvas);
        }

        if (mEnableDebugDraw && mSplitDrawPath != null) {
            canvas.drawPath(mSplitDrawPath, mDebugDrawPaint);
        }
    } else {
        int saveCount = canvas.getSaveCount();
        canvas.save();

        if (mCropToPadding) {
            final int scrollX = getScrollX();
            final int scrollY = getScrollY();
            canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(),
                    scrollX + getRight() - getLeft() - getPaddingRight(),
                    scrollY + getBottom() - getTop() - getPaddingBottom());
        }

        canvas.translate(getPaddingLeft(), getPaddingTop());

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

        /**
         * Add a clipping path
         */
        if (mSplitDrawPath != null) {
            canvas.clipPath(mSplitDrawPath);
        }

        if (mForegroundDrawable != null) {
            mForegroundDrawable.draw(canvas);
        }

        if (mEnableDebugDraw && mSplitDrawPath != null) {
            canvas.drawPath(mSplitDrawPath, mDebugDrawPaint);
        }
        canvas.restoreToCount(saveCount);
    }
}