List of usage examples for android.graphics Canvas drawRect
public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint)
From source file:com.bob.googleplay.view.PagerSlidingTriangleStrip.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return;// w ww .j a v a 2 s . c om } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); // if there is an offset, start interpolating left and right coordinates // between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } // // canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, // height, rectPaint); // --> float x1 = (lineRight - lineLeft) / 2 + lineLeft; float y1 = height - indicatorHeight; float x2 = x1 - triangleWidth / 2f; float y2 = height; float x3 = x1 + triangleWidth / 2f; float y3 = height; Path path = new Path(); path.moveTo(x1, y1);// path.lineTo(x2, y2);// path.lineTo(x3, y3);// path.lineTo(x1, y1);// // rectPaint.setStyle(Style.STROKE); // rectPaint.setStrokeWidth(width) canvas.drawPath(path, rectPaint); // draw underline rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } }
From source file:com.heshun.hslibrary.project.viewpagerindicator.CirclePageIndicator.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mViewPager == null) { return;//from www.j a v a 2 s .co m } final int count = mViewPager.getAdapter().getCount(); if (count == 0) { return; } if (mCurrentPage >= count) { setCurrentItem(count - 1); return; } int longSize; int longPaddingBefore; int longPaddingAfter; int shortPaddingBefore; if (mOrientation == HORIZONTAL) { longSize = getWidth(); longPaddingBefore = getPaddingLeft(); longPaddingAfter = getPaddingRight(); shortPaddingBefore = getPaddingTop(); } else { longSize = getHeight(); longPaddingBefore = getPaddingTop(); longPaddingAfter = getPaddingBottom(); shortPaddingBefore = getPaddingLeft(); } final float threeRadius = mRadius * 3; final float shortOffset = shortPaddingBefore + mRadius; float longOffset = longPaddingBefore + mRadius; if (mCentered) { longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f); } float dX; float dY; float pageFillRadius = mRadius; if (mPaintStroke.getStrokeWidth() > 0) { pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f; } // Draw stroked circles for (int iLoop = 0; iLoop < count; iLoop++) { float drawLong = longOffset + (iLoop * threeRadius); if (mOrientation == HORIZONTAL) { dX = drawLong; dY = shortOffset; } else { dX = shortOffset; dY = drawLong; } // Only paint fill if not completely transparent if (mPaintPageFill.getAlpha() > 0) { // canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill); canvas.drawRect(dX - 10, dY - 3, dX + 10, dY + 3, mPaintPageFill); } // Only paint stroke if a stroke width was non-zero if (pageFillRadius != mRadius) { // canvas.drawCircle(dX, dY, mRadius, mPaintStroke); canvas.drawRect(dX - 10, dY - 3, dX + 10, dY + 3, mPaintStroke); } } // Draw the filled circle according to the current scroll float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius; if (!mSnap) { cx += mPageOffset * threeRadius; } if (mOrientation == HORIZONTAL) { dX = longOffset + cx; dY = shortOffset; } else { dX = shortOffset; dY = longOffset + cx; } // canvas.drawCircle(dX, dY, mRadius, mPaintFill); canvas.drawRect(dX - 10, dY - 3, dX + 10, dY + 3, mPaintFill); }
From source file:cl.monsoon.s1next.widget.PhotoView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw the photo if (mDrawable != null) { int saveCount = canvas.getSaveCount(); canvas.save();// w w w. jav a 2 s .c o m if (mDrawMatrix != null) { canvas.concat(mDrawMatrix); } mDrawable.draw(canvas); canvas.restoreToCount(saveCount); if (mVideoBlob != null) { final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage); final int drawLeft = (getWidth() - videoImage.getWidth()) / 2; final int drawTop = (getHeight() - videoImage.getHeight()) / 2; canvas.drawBitmap(videoImage, drawLeft, drawTop, null); } // Extract the drawable's bounds (in our own copy, to not alter the image) mTranslateRect.set(mDrawable.getBounds()); if (mDrawMatrix != null) { mDrawMatrix.mapRect(mTranslateRect); } if (mAllowCrop) { int previousSaveCount = canvas.getSaveCount(); canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint); canvas.save(); canvas.clipRect(mCropRect); if (mDrawMatrix != null) { canvas.concat(mDrawMatrix); } mDrawable.draw(canvas); canvas.restoreToCount(previousSaveCount); canvas.drawRect(mCropRect, sCropPaint); } } }
From source file:com.chj.indicator.lib.TriangleSlidingIndicator.java
/** * //w w w .j a va 2 s . c o m */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return; } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); // if there is an offset, start interpolating left and right coordinates // between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } // // canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, // height, rectPaint); float left = lineLeft; float top = height - indicatorHeight; float right = lineRight; float bottom = height; // TODO: Path path = new Path(); float x1 = (right - left) / 2 + left;// :(right+left)/2 float y1 = top; float x2 = x1 - triangleWidth / 2; float y2 = bottom; float x3 = x1 + triangleWidth / 2; float y3 = bottom; path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.lineTo(x1, y1); path.close(); rectPaint.setColor(Color.RED);// ? canvas.drawPath(path, rectPaint); // draw underline rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } }
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 + ")"); }//from ww w . j ava2s . c o m 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.unkonw.testapp.libs.view.indicator.CirclePageIndicatorOfHuaLong.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mViewPager == null) { return;//from w ww .j a v a 2 s . c om } final int count = mViewPager.getAdapter().getCount(); if (count == 0) { return; } if (mCurrentPage >= count) { setCurrentItem(count - 1); return; } int longSize; int longPaddingBefore; int longPaddingAfter; int shortPaddingBefore; if (mOrientation == HORIZONTAL) { longSize = getWidth(); longPaddingBefore = getPaddingLeft(); longPaddingAfter = getPaddingRight(); shortPaddingBefore = getPaddingTop(); } else { longSize = getHeight(); longPaddingBefore = getPaddingTop(); longPaddingAfter = getPaddingBottom(); shortPaddingBefore = getPaddingLeft(); } final float threeRadius = mRadius * 3; final float shortOffset = shortPaddingBefore + mRadius; float longOffset = longPaddingBefore + mRadius; if (mCentered) { longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f); } float dX; float dY; float pageFillRadius = mRadius; if (mPaintStroke.getStrokeWidth() > 0) { pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f; } float ft = mPaintText.getTextSize() - 3; mPaintText.setColor(0xffffffff); //Draw stroked circles for (int iLoop = 0; iLoop < count; iLoop++) { float drawLong = longOffset + (iLoop * threeRadius); if (mOrientation == HORIZONTAL) { dX = drawLong; dY = shortOffset; } else { dX = shortOffset; dY = drawLong; } // Only paint fill if not completely transparent if (mPaintPageFill.getAlpha() > 0) { // canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill); canvas.drawRect(dX, dY, dX + ft, dY - ft, mPaintPageFill); } // Only paint stroke if a stroke width was non-zero if (pageFillRadius != mRadius) { // canvas.drawCircle(dX, dY, mRadius, mPaintStroke); canvas.drawRect(dX, dY, dX + ft, dY - ft, mPaintStroke); } canvas.drawText(String.valueOf(iLoop + 1), dX, dY, mPaintText); } //Draw the filled circle according to the current scroll float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius; if (!mSnap) { cx += mPageOffset * threeRadius; } if (mOrientation == HORIZONTAL) { dX = longOffset + cx; dY = shortOffset; } else { dX = shortOffset; dY = longOffset + cx; } // canvas.drawCircle(dX, dY, mRadius, mPaintFill); mPaintText.setColor(0xffffffff); canvas.drawRect(dX, dY, dX + ft, dY - ft, mPaintFill); canvas.drawText(String.valueOf(mCurrentPage + 1), dX, dY, mPaintText); }
From source file:de.hs_bremen.aurora_hunter.ui.views.KpIndexChartView.java
public void onDraw(Canvas canvas) { if (points.size() == 0) { return;//w ww .ja va2s. c o m } Path path = new Path(); int height = canvas.getHeight(); int width = canvas.getWidth(); for (Point point : points) { point.x = point.percentageX * width + 60; // Log.i("scaleFactor", " : " + scaleFactor); //Log.i("percent", " : " + percent); point.y = (float) ((1 - point.percentageY * scaleFactor) * height * 0.7 + 30); } if (points.size() > 1) { //calcuate x/y based on size of canvas for (int i = 0; i < points.size(); i++) { if (i >= 0) { Point point = points.get(i); // Log.i("dx",point.x + " - " + point.y ); if (i == 0) { Point next = points.get(i + 1); point.dx = ((next.x - point.x) / 5); point.dy = ((next.y - point.y) / 5); } else if (i == points.size() - 1) { Point prev = points.get(i - 1); point.dx = ((point.x - prev.x) / 5); point.dy = ((point.y - prev.y) / 5); } else { Point next = points.get(i + 1); Point prev = points.get(i - 1); point.dx = ((next.x - prev.x) / 5); point.dy = ((next.y - prev.y) / 5); } } } } if (points.size() > 0) { path.moveTo(0, (float) (canvas.getHeight() * 0.8)); path.lineTo(0, points.get(0).y); } boolean first = true; for (int i = 0; i < points.size(); i++) { Point point = points.get(i); if (first) { first = false; path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy, point.x, point.y); } else { Point prev = points.get(i - 1); // Log.i("Draw", point.dx + " " + point.dy); path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y); } } path.lineTo(width, (float) (height * 0.8)); path.lineTo(width, (float) (height * 0.9)); path.lineTo(0, (float) (height * 0.9)); canvas.drawPath(path, mGraphPaint); canvas.drawRect(0, (float) (height * 0.9), width, height, mGraphPaint); double Kp1Height = (1 - 0.6666666) * height;//points.get(points.size()-1).y; canvas.drawRect(0, (float) Kp1Height, width, (float) Kp1Height + 1, paintG1Line); canvas.drawText("G1", 2, (float) Kp1Height, paintTxt); int detlaY = 15; for (Point p : points) { int val = (int) Math.round(9 * p.percentageY); //if last element if (points.indexOf(p) == points.size() - 1) { //Log.i("last", p.toString()); // canvas.drawText(val + "", p.x-150,p.y - detlaY, paintTxt); } else { canvas.drawText(val + "", p.x - 10, p.y - detlaY, paintTxt); } //Log.i("point", p.toString()); } // Log.i("Lenght", points.size() + " "); }
From source file:com.kindleren.kandouwo.common.views.PagerSlidingTabStrip.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return;/*w ww.j av a2s . com*/ } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); //change alpha if (indicatorDrawable != null) { ((TextView) currentTab) .setTextColor(tabTextColor.withAlpha((int) (500 * Math.abs(currentPositionOffset - 0.5)))); } currentTab.setSelected(true); float lineLeft = currentTab.getLeft(); float lineRight = currentTab.getRight(); float lineTop = currentTab.getTop(); float lineBottom = currentTab.getBottom(); // if there is an offset, start interpolating left and right coordinates between current and next tab if (currentPositionOffset >= 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); //change alpha if (indicatorDrawable != null) { ((TextView) nextTab) .setTextColor(tabTextColor.withAlpha((int) (500 * Math.abs(currentPositionOffset - 0.5)))); } final float nextTabLeft = nextTab.getLeft(); final float nextTabRight = nextTab.getRight(); lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); //change view state if (currentPositionOffset > 0.5) { currentTab.setSelected(false); nextTab.setSelected(true); } else { currentTab.setSelected(true); nextTab.setSelected(false); } } rectPaint.setColor(underlineColor); if (indicatorDrawable != null) { indicatorDrawable.setBounds((int) lineLeft + getPaddingLeft(), (int) lineTop + getPaddingTop(), (int) lineRight + getPaddingLeft(), (int) lineBottom + getPaddingTop()); indicatorDrawable.draw(canvas); } else { canvas.drawRect(lineLeft + 30, height - indicatorHeight, lineRight - 30, height, rectPaint); } canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } }
From source file:com.actionbarsherlock.custom.widget.VerticalDrawerLayout.java
@Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) { final int width = getWidth(); final boolean drawingContent = isContentView(child); int clipTop = 0, clipBottom = getHeight(); final int restoreCount = canvas.save(); if (drawingContent) { final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View v = getChildAt(i); if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v) || v.getWidth() < width) { continue; }//from w ww .ja va 2s. c o m if (checkDrawerViewGravity(v, Gravity.TOP)) { final int vbottom = v.getBottom(); if (vbottom > clipTop) clipTop = vbottom; } else { final int vtop = v.getTop(); if (vtop < clipBottom) clipBottom = vtop; } } canvas.clipRect(0, clipTop, getWidth(), clipBottom); } final boolean result = super.drawChild(canvas, child, drawingTime); canvas.restoreToCount(restoreCount); if (mScrimOpacity > 0 && drawingContent) { final int baseAlpha = (mScrimColor & 0xff000000) >>> 24; final int imag = (int) (baseAlpha * mScrimOpacity); final int color = imag << 24 | (mScrimColor & 0xffffff); mScrimPaint.setColor(color); canvas.drawRect(0, clipTop, getWidth(), clipBottom, mScrimPaint); } else if (mShadowTop != null && checkDrawerViewGravity(child, Gravity.TOP)) { final int shadowHeight = mShadowTop.getIntrinsicHeight(); final int childBottom = child.getBottom(); final int drawerPeekDistance = mTopDragger.getEdgeSize(); final float alpha = Math.max(0, Math.min((float) childBottom / drawerPeekDistance, 1.f)); mShadowTop.setBounds(child.getLeft(), childBottom, child.getRight(), childBottom + shadowHeight); mShadowTop.setAlpha((int) (0xff * alpha)); mShadowTop.draw(canvas); } else if (mShadowBottom != null && checkDrawerViewGravity(child, Gravity.BOTTOM)) { final int shadowHeight = mShadowBottom.getIntrinsicHeight(); final int childTop = child.getTop(); final int showing = getHeight() - childTop; final int drawerPeekDistance = mBottomDragger.getEdgeSize(); final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f)); mShadowTop.setBounds(child.getLeft(), childTop - shadowHeight, child.getRight(), childTop); mShadowBottom.setAlpha((int) (0xff * alpha)); mShadowBottom.draw(canvas); } return result; }
From source file:com.blestep.sportsbracelet.view.TimelineChartViewSleep.java
private void drawBarItems(Canvas c, SparseArray<Object[]> data, double maxValue) { final float halfItemBarWidth = mBarItemWidth / 2; final float height = mGraphArea.height(); final Paint seriesBgPaint; final Paint highlightSeriesBgPaint; final Paint seriesDeepBgPaint; final Paint highlightSeriesDeepBgPaint; synchronized (mLock) { seriesBgPaint = mBarItemBgPaint; highlightSeriesBgPaint = mHighlightBarItemBgPaint; seriesDeepBgPaint = mBarDeepItemBgPaint; highlightSeriesDeepBgPaint = mHighlightBarDeepItemBgPaint; }//from ww w . j a va2s . c o m // Apply zoom animation final float graphCenterX = mGraphArea.left + (mGraphArea.width() / 2); final int size = data.size() - 1; for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0] && i <= data.size(); i--) { final float barCenterX = graphCenterX + mCurrentOffset - (mBarWidth * (size - i)); // ? double value = (double) data.valueAt(i)[6]; // ? double deepValue = (double) data.valueAt(i)[3]; float barTop = (float) (height - ((height * ((value * 100) / maxValue)) / 100)) + mTopSpaceHeight; float barBottom = height; float barLeft = barCenterX - halfItemBarWidth; float barRight = barCenterX + halfItemBarWidth; final Paint paint; final Paint deepPaint; float deepBarTop = (float) (height - ((height * ((deepValue * 100) / maxValue)) / 100)) + mTopSpaceHeight; // ? paint = barLeft < graphCenterX && barRight > graphCenterX && (mLastPosition == mCurrentPosition || (mState != STATE_SCROLLING)) ? highlightSeriesBgPaint : seriesBgPaint; // ? deepPaint = barLeft < graphCenterX && barRight > graphCenterX && (mLastPosition == mCurrentPosition || (mState != STATE_SCROLLING)) ? highlightSeriesDeepBgPaint : seriesDeepBgPaint; // c.drawRect(barLeft, mGraphArea.top + barTop, barRight, mGraphArea.top + barBottom, paint); c.drawRect(barLeft, mGraphArea.top + deepBarTop, barRight, mGraphArea.top + barBottom, deepPaint); } }