Java Code Examples for android.graphics.Canvas#getWidth()
The following examples show how to use
android.graphics.Canvas#getWidth() .
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: ColorPickerView.java From timecat with Apache License 2.0 | 6 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(backgroundColor); if (colorWheel != null) canvas.drawBitmap(colorWheel, 0, 0, null); if (currentColorCircle != null) { float maxRadius = canvas.getWidth() / 2f - STROKE_RATIO * (1f + ColorWheelRenderer.GAP_PERCENTAGE); float size = maxRadius / density / 2; colorWheelFill.setColor(Color.HSVToColor(currentColorCircle.getHsvWithLightness(this.lightness))); colorWheelFill.setAlpha((int) (alpha * 0xff)); canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size * STROKE_RATIO, selectorStroke1); canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size * (1 + (STROKE_RATIO - 1) / 2), selectorStroke2); canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size, alphaPatternPaint); canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size, colorWheelFill); } }
Example 2
Source File: ColorPickerView.java From openlauncher with Apache License 2.0 | 6 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(backgroundColor); float maxRadius = canvas.getWidth() / (1f + ColorWheelRenderer.GAP_PERCENTAGE); float size = maxRadius / density / 2; if (colorWheel != null && currentColorCircle != null) { colorWheelFill.setColor(Color.HSVToColor(currentColorCircle.getHsvWithLightness(this.lightness))); colorWheelFill.setAlpha((int) (alpha * 0xff)); // a separate canvas is used to erase an issue with the alpha pattern around the edges // draw circle slightly larger than it needs to be, then erase edges to proper dimensions currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + 4, alphaPatternPaint); currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + 4, colorWheelFill); selectorStroke = PaintBuilder.newPaint().color(0xffffffff).style(Paint.Style.STROKE).stroke(size * (STROKE_RATIO - 1)).xPerMode(PorterDuff.Mode.CLEAR).build(); if (showBorder) colorWheelCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + (selectorStroke.getStrokeWidth() / 2f), selectorStroke); canvas.drawBitmap(colorWheel, 0, 0, null); currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + (selectorStroke.getStrokeWidth() / 2f), selectorStroke); canvas.drawBitmap(currentColor, 0, 0, null); } }
Example 3
Source File: GraphicOverlay.java From Android-face-filters with Apache License 2.0 | 6 votes |
/** * Draws the overlay with its associated graphic objects. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); synchronized (mLock) { if ((mPreviewWidth != 0) && (mPreviewHeight != 0)) { mWidthScaleFactor = (float) canvas.getWidth() / (float) mPreviewWidth; mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight; } for (Graphic graphic : mGraphics) { graphic.draw(canvas); } } }
Example 4
Source File: ViewfinderView.java From BarcodeScanner with Apache License 2.0 | 6 votes |
/** * 绘制遮掩层 * * @param canvas * @param frame */ private void drawCover(Canvas canvas, Rect frame) { // 获取屏幕的宽和高 int width = canvas.getWidth(); int height = canvas.getHeight(); // Draw the exterior (i.e. outside the framing rect) darkened paint.setColor(resultBitmap != null ? resultColor : maskColor); // 画出扫描框外面的阴影部分,共四个部分,扫描框的上面到屏幕上面,扫描框的下面到屏幕下面 // 扫描框的左边面到屏幕左边,扫描框的右边到屏幕右边 canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint); }
Example 5
Source File: GraphicOverlay.java From Questor with MIT License | 6 votes |
/** * Draws the overlay with its associated graphic objects. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); synchronized (mLock) { if ((mPreviewWidth != 0) && (mPreviewHeight != 0)) { mWidthScaleFactor = (float) canvas.getWidth() / (float) mPreviewWidth; mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight; } for (Graphic graphic : mGraphics) { graphic.draw(canvas); } } }
Example 6
Source File: SeekBar.java From Metronome-Android with Apache License 2.0 | 6 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); secondaryPaint.setAlpha(255); canvas.drawRect(0, 0, canvas.getWidth(), 2, secondaryPaint); int currentWidth = (int) (canvas.getWidth() * ((float) progress / maxProgress)); for (int i = 0; i < maxProgress; i += 10) { int width = (int) (canvas.getWidth() * ((float) i / maxProgress)); secondaryPaint.setAlpha(Math.max(255 - (int) ((float) Math.abs(width - currentWidth) * 1000 / canvas.getWidth()), 0)); canvas.drawRect(width - 1, 0, width + 1, ConversionUtils.getPixelsFromDp(i % 20 == 0 ? 6 : 4), secondaryPaint); } canvas.drawRect(0, 0, currentWidth, 2, accentPaint); canvas.drawRect(currentWidth - 1, 0, currentWidth + 1, ConversionUtils.getPixelsFromDp(10), accentPaint); }
Example 7
Source File: WatchFaceGenerator.java From xDrip with GNU General Public License v3.0 | 5 votes |
private Bitmap drawNoDataBitmap() { Bitmap resultBitmap = mainWatchfaceImage.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(resultBitmap); int width = canvas.getWidth(); int height = canvas.getHeight(); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(textColor); paint.setAntiAlias(true); paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)); paint.setTextSize(noDataTextSize); Rect bounds = new Rect(); String noDataText = "No Data"; paint.getTextBounds(noDataText, 0, noDataText.length(), bounds); float x = width / 2f - bounds.width() / 2f - bounds.left; canvas.drawText(noDataText, x, height - 100, paint); //draw bgValueText //draw timestamp int timeStampTextPosY = height - 37;//px String timeStampText = "at " + hourMinuteString(JoH.tsl()); paint.setTextSize(timeStampTextSize); paint.getTextBounds(timeStampText, 0, timeStampText.length(), bounds); canvas.drawText(timeStampText, width - bounds.width(), timeStampTextPosY, paint); return resultBitmap; }
Example 8
Source File: TapMeasureTool.java From geopaparazzi with GNU General Public License v3.0 | 5 votes |
public void onToolDraw(Canvas canvas) { int cWidth = canvas.getWidth(); canvas.drawPath(measurePath, measureHaloPaint); canvas.drawPath(measurePath, measurePaint); int upper = 200; int delta = 5; measureTextPaint.getTextBounds(distanceString, 0, distanceString.length(), rect); int textWidth = rect.width(); int textHeight = rect.height(); int x = cWidth / 2 - textWidth / 2; canvas.drawText(distanceString, x, upper, measureTextPaint); textBuilder.setLength(0); if (doImperial) { double distanceInFeet = MercatorUtils.toFeet(measuredDistance); textBuilder.append((int) distanceInFeet); textBuilder.append(" ft"); //$NON-NLS-1$ } else { textBuilder.append((int) measuredDistance); textBuilder.append(" m"); //$NON-NLS-1$ } String distanceText = textBuilder.toString(); measureTextPaint.getTextBounds(distanceText, 0, distanceText.length(), rect); textWidth = rect.width(); x = cWidth / 2 - textWidth / 2; canvas.drawText(distanceText, x - 2, upper + delta + textHeight - 2, measureTextHaloPaint); canvas.drawText(distanceText, x, upper + delta + textHeight, measureTextPaint); if (GPLog.LOG_HEAVY) GPLog.addLogEntry(this, "Drawing measure path text: " + upper); //$NON-NLS-1$ }
Example 9
Source File: VoiceView.java From imsdk-android with MIT License | 5 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); int x = (width - bitmapSize)/2; int y = (height - bitmapSize)/2; if(mCurrentRadius > mMinRadius&&mState!=STATE_CANCEL){ canvas.drawCircle(width / 2, height / 2, mCurrentRadius, mPaint); canvas.drawCircle(width/2,height/2,(mCurrentRadius+mMinRadius)/2,iPaint); } switch (mState){ case STATE_NORMAL: if(mNormalBitmap!= null) canvas.drawBitmap(mNormalBitmap, x,y, wPaint); break; case STATE_PRESSED: if(mPressedBitmap!=null) canvas.drawBitmap(mPressedBitmap, x, y, wPaint); break; case STATE_CANCEL: if(mCancelBitmap!=null) canvas.drawBitmap(mCancelBitmap, x, y, wPaint); break; } }
Example 10
Source File: TaperChart.java From WidgetCase with Apache License 2.0 | 5 votes |
/** * 数据为空的时候,绘制文案 * @param canvas */ private void drawEmptyData(Canvas canvas) { int canvasW = canvas.getWidth(); int canvasH = canvas.getHeight(); int emptyTxtW = Util.getTextWidth(NOT_DATA, mNullPaint); int emptyTxtH = Util.getTextHeight(NOT_DATA, mNullPaint); // canvas.drawText(NOT_DATA, canvasW / 2 - emptyTxtL / 2, canvasH / 2 - emptyTxtL / 2, mNullPaint); // 画文字的时候,y值是文字的底线 canvas.drawText(NOT_DATA, canvasW / 2 - emptyTxtW / 2, canvasH / 2 + emptyTxtH / 2 - DensityUtil.dp2px(1.5f), mNullPaint); // 测试 // canvas.drawLine(0, canvasH / 2 - mNullPaint.getStrokeWidth(), canvasW, canvasH / 2 - mNullPaint.getStrokeWidth(), mNullPaint); }
Example 11
Source File: SVG.java From XDroidAnimation with Apache License 2.0 | 5 votes |
/** * Renders this SVG document to a Canvas object. * * @param canvas the canvas to which the document should be rendered. * @param viewPort the bounds of the area on the canvas you want the SVG rendered, or null for the whole canvas. */ public void renderToCanvas(Canvas canvas, RectF viewPort) { Box svgViewPort; if (viewPort != null) { svgViewPort = Box.fromLimits(viewPort.left, viewPort.top, viewPort.right, viewPort.bottom); } else { svgViewPort = new Box(0f, 0f, (float) canvas.getWidth(), (float) canvas.getHeight()); } SVGAndroidRenderer renderer = new SVGAndroidRenderer(canvas, svgViewPort, this.renderDPI); renderer.renderDocument(this, null, null, true); }
Example 12
Source File: VideoDecoderOutput.java From GIFCompressor with Apache License 2.0 | 5 votes |
private void drawBitmap(@NonNull Bitmap bitmap) { Canvas canvas = mSurface.lockCanvas(null); if (bitmap.getWidth() != canvas.getWidth() || bitmap.getHeight() != canvas.getHeight()) { throw new RuntimeException("Unexpected width / height." + " bw:" + bitmap.getWidth() + " bh:" + bitmap.getHeight() + " cw:" + canvas.getWidth() + " ch:" + canvas.getHeight()); } canvas.drawBitmap(bitmap, 0, 0, null); mSurface.unlockCanvasAndPost(canvas); }
Example 13
Source File: BubbleRelativeLayout.java From BubblePopupWindow with Apache License 2.0 | 5 votes |
@Override protected void onDraw(Canvas canvas) { final float width = canvas.getWidth(); final float height = canvas.getHeight(); mPath.rewind(); mPath.addRoundRect(new RectF(PADDING, PADDING, width - PADDING, height - PADDING), CORNER_RADIUS, CORNER_RADIUS, Direction.CW); mPath.addPath(mBubbleLegPrototype, renderBubbleLegMatrix(width, height)); canvas.drawPath(mPath, mPaint); canvas.scale((width - STROKE_WIDTH) / width, (height - STROKE_WIDTH) / height, width / 2f, height / 2f); canvas.drawPath(mPath, mFillPaint); }
Example 14
Source File: DrawableUtils.java From Xndroid with GNU General Public License v3.0 | 5 votes |
/** * Creates a rounded square of a certain color with * a character imprinted in white on it. * * @param character the character to write on the image. * @param width the width of the final image. * @param height the height of the final image. * @param color the background color of the rounded square. * @return a valid bitmap of a rounded square with a character on it. */ @NonNull public static Bitmap getRoundedLetterImage(@NonNull Character character, int width, int height, int color) { Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); Paint paint = new Paint(); paint.setColor(color); Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD); paint.setTypeface(boldText); paint.setTextSize(Utils.dpToPx(14)); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); int radius = Utils.dpToPx(2); RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight()); canvas.drawRoundRect(outer, radius, radius, paint); int xPos = (canvas.getWidth() / 2); int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)); paint.setColor(Color.WHITE); canvas.drawText(character.toString(), xPos, yPos, paint); return image; }
Example 15
Source File: Sign.java From android_maplibui with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected void onDraw(Canvas canvas) { if (mNotInitialized && mPreviousSignBitmap != null) canvas.drawBitmap(mPreviousSignBitmap, 0, 0, null); int posX = canvas.getWidth() - mClearImageSize - mClearBuff; mCleanImage.setBounds(posX, mClearBuff, posX + mClearImageSize, mClearImageSize + mClearBuff); mCleanImage.draw(canvas); if (!mNotInitialized) for (Path path : mPaths) canvas.drawPath(path, mPaint); }
Example 16
Source File: FloatingSessionsButton.java From focus-android with Mozilla Public License 2.0 | 5 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final float x = canvas.getWidth() / 2f; final float y = (canvas.getHeight() / 2f) - ((textPaint.descent() + textPaint.ascent()) / 2f); final String text = tabCount < TOO_MANY_TABS ? String.valueOf(tabCount) : TOO_MANY_TABS_SYMBOL; canvas.drawText(text, x, y, textPaint); }
Example 17
Source File: CircularImageView.java From CircularImageView with MIT License | 4 votes |
@Override public void onDraw(Canvas canvas) { // Don't draw anything without an image if(image == null) return; // Nothing to draw (Empty bounds) if(image.getHeight() == 0 || image.getWidth() == 0) return; // Compare canvas sizes int oldCanvasSize = canvasSize; canvasSize = canvas.getWidth(); if(canvas.getHeight() < canvasSize) canvasSize = canvas.getHeight(); // Reinitialize shader, if necessary if(oldCanvasSize != canvasSize) refreshBitmapShader(); // Apply shader to paint paint.setShader(shader); // Keep track of selectorStroke/border width int outerWidth = 0; // Get the exact X/Y axis of the view int center = canvasSize / 2; if(hasSelector && isSelected) { // Draw the selector stroke & apply the selector filter, if applicable outerWidth = selectorStrokeWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(selectorFilter); canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintSelectorBorder); } else if(hasBorder) { // If no selector was drawn, draw a border and clear the filter instead... if enabled outerWidth = borderWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(null); canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintBorder); } else // Clear the color filter if no selector nor border were drawn paint.setColorFilter(null); // Draw the circular image itself canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) - 4.0f, paint); }
Example 18
Source File: Sushi.java From android-slidr with Apache License 2.0 | 4 votes |
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); { final float paddingLeft = settings.paddingCorners; final float paddingRight = settings.paddingCorners; final float radiusCorner = settings.barHeight / 2f; final float indicatorCenterX = indicatorX + paddingLeft; { //background final float centerCircleLeft = paddingLeft; final float centerCircleRight = getWidth() - paddingRight; //grey background settings.paintBar.setColor(settings.colorBackground); canvas.drawCircle(centerCircleLeft, barCenterY, radiusCorner, settings.paintBar); canvas.drawCircle(centerCircleRight, barCenterY, radiusCorner, settings.paintBar); canvas.drawRect(centerCircleLeft, barY, centerCircleRight, barY + settings.barHeight, settings.paintBar); //color before indicator settings.paintBar.setColor(settings.foregroundColor); canvas.drawCircle(centerCircleLeft, barCenterY, radiusCorner, settings.paintBar); canvas.drawRect(centerCircleLeft, barY, indicatorCenterX, barY + settings.barHeight, settings.paintBar); } if (settings.displayMinMax) { //texts top (values) final float textY = barY - DISTANCE_TEXT_BAR; drawIndicatorsTextAbove(canvas, formatValue(min), settings.paintTextTop, 0 + paddingLeft, textY, Layout.Alignment.ALIGN_CENTER); drawIndicatorsTextAbove(canvas, formatValue(max), settings.paintTextTop, canvas.getWidth(), textY, Layout.Alignment.ALIGN_CENTER); } //bubble { float bubbleCenterX = indicatorCenterX; float trangleCenterX; bubble.x = bubbleCenterX - bubble.width / 2f; if (bubbleCenterX > canvas.getWidth() - bubble.width / 2f) { bubbleCenterX = canvas.getWidth() - bubble.width / 2f; } else if (bubbleCenterX - bubble.width / 2f < 0) { bubbleCenterX = bubble.width / 2f; } trangleCenterX = (bubbleCenterX + indicatorCenterX) / 2f; drawBubble(canvas, bubbleCenterX, trangleCenterX, bubble.getY()); } } canvas.restore(); }
Example 19
Source File: FlipCard.java From FlipGank with Apache License 2.0 | 4 votes |
@Override public void draw(Canvas canvas) { if (mPercent == 0) { super.draw(canvas); return; } final int height = canvas.getHeight(); final int width = canvas.getWidth(); final float percent = mPercent > 0 ? mPercent : -mPercent; if (mIsForground) { // clip card effect for forground view // draw part1 int save1 = canvas.save(); if (mPercent > 0) { canvas.clipRect(0, 0, width, height / 2); } else { canvas.clipRect(0, height / 2, width, height); } super.draw(canvas); canvas.restoreToCount(save1); // draw part2 if (mPercent < 0) { canvas.clipRect(0, 0, width, height / 2); } else { canvas.clipRect(0, height / 2, width, height); } mCamera.save(); mCamera.setLocation(0f, 0f, -80); mCamera.rotateX(mPercent * 180); mCamera.getMatrix(mMatrix); mCamera.restore(); mMatrix.preTranslate(-width / 2, -height / 2); mMatrix.postTranslate(width / 2, height / 2); canvas.concat(mMatrix); super.draw(canvas); mScrimPaint.setColor(0x08000000); canvas.drawRect(0, 0, width, height, mScrimPaint); } else { // draw shadow for underground view final int scrimColor = (int) (0xff * (1 - percent * 2)) << 24; mScrimPaint.setColor(scrimColor); if (mPercent < 0) { canvas.clipRect(0, 0, width, height / 2); } else { canvas.clipRect(0, height / 2, width, height); } super.draw(canvas); canvas.drawRect(0, 0, width, height, mScrimPaint); } }
Example 20
Source File: CircularImageView.java From buddycloud-android with Apache License 2.0 | 4 votes |
@Override public void onDraw(Canvas canvas) { // Don't draw anything without an image if (image == null) return; // Nothing to draw (Empty bounds) if (image.getHeight() == 0 || image.getWidth() == 0) return; // Compare canvas sizes int oldCanvasSize = canvasSize; canvasSize = canvas.getWidth(); if (canvas.getHeight() < canvasSize) canvasSize = canvas.getHeight(); // Reinitialize shader, if necessary if (oldCanvasSize != canvasSize) refreshBitmapShader(); // Apply shader to paint paint.setShader(shader); // Keep track of selectorStroke/border width int outerWidth = 0; // Get the exact X/Y axis of the view int center = canvasSize / 2; if (hasSelector && isSelected) { // Draw the selector stroke & apply the // selector filter, if applicable outerWidth = selectorStrokeWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(selectorFilter); canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintSelectorBorder); } else if (hasBorder) { // If no selector was drawn, draw a border and // clear the filter instead... if enabled outerWidth = borderWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(null); canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintBorder); } else // Clear the color filter if no selector nor border were drawn paint.setColorFilter(null); // Draw the circular image itself canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) - 4.0f, paint); }