public void Draw(Canvas canvas) { canvas.drawBitmap(ret, pos.x, canvas.getHeight() - (pos.y + scale.y), null); // testing line------------------------------------------------// if (state == STATE.DRAGGING) { paint.setColor(Color.RED); paint.setStrokeWidth(10); // From startPos to current dragging pos canvas.drawLine( startPos.x, canvas.getHeight() - startPos.y, fingerPos.x, canvas.getHeight() - fingerPos.y, paint); } // draw the rubbish------------------------------------------// for (int i = 0; i < totalDragged; ++i) { if (rubbishPile.get(i).GetActive()) rubbishPile.get(i).Draw(canvas); } paint.setARGB(255, 255, 0, 0); paint.setStrokeWidth(120); // how thick you want the text to be in terms of pixel paint.setTextSize(60); canvas.drawText("Active: " + totalDragged, 130, 80, paint); bound.DrawDebug(canvas); }
public synchronized void render(Canvas canvas) { if (canvas.getWidth() != surfaceWidth || canvas.getHeight() != surfaceHeight) { surfaceWidth = canvas.getWidth(); surfaceHeight = canvas.getHeight(); setWindowScale(); } /* while (surface == null) { try { wait(); } catch(InterruptedException e) {} } */ canvas.save(); canvas.scale(windowScale, windowScale, 0, 0); canvas.drawBitmap(screen, windowX / windowScale, windowY / windowScale, bilinearPaint); canvas.restore(); // HUD - caption, subtitles, buttons if (gameState == GAMESTATE_LEVEL) { moreButtonsAnim(); saveEffectRender(canvas); controls.render(canvas); activeFish.render(canvas, 800 - 96, 480 - 96, surfaceWidth, surfaceHeight); } // window caption renderCaption(canvas); // notifyAll(); }
@Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // canvas canvas.drawLine( canvas.getWidth() / 2 - 200, canvas.getHeight() / 2 + 50, canvas.getWidth(), canvas.getHeight() / 2 + 50, red); // xaxis canvas.drawLine( canvas.getWidth() / 2 - 150, canvas.getHeight() / 2 - 800, canvas.getWidth() / 2 - 150, canvas.getHeight() / 2 + 100, red); // yaxis for (int i = 0; i <= 720; i++) // curve { int x = i + (canvas.getWidth() / 2 - 150); int y = (int) ((canvas.getHeight() / 2 + 50) + ((float) Math.sin(Math.toRadians(2 * i))) * 100); canvas.drawPoint(x, y, blue); } }
void drawFrame(Mat modified) { // Partly from OpenCV CameraBridgeViewBase.java if (mCacheBitmap == null) { mCacheBitmap = Bitmap.createBitmap(modified.width(), modified.height(), Bitmap.Config.ARGB_8888); } boolean bmpValid = true; if (modified != null) { try { Utils.matToBitmap(modified, mCacheBitmap); } catch (Exception e) { Log.e(TAG, "Mat type: " + modified); Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight()); Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage()); bmpValid = false; } } if (bmpValid && mCacheBitmap != null) { Canvas canvas = view.getHolder().lockCanvas(); if (canvas != null) { canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR); canvas.drawBitmap( mCacheBitmap, new Rect(0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight()), new Rect( (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(), (canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null); view.getHolder().unlockCanvasAndPost(canvas); } } }
public void drawSignLine() { mCanvas.drawLine( 0, (int) (mCanvas.getHeight() * .7), mCanvas.getWidth(), (int) (mCanvas.getHeight() * .7), paint); }
@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); }
private void drawBackground(Canvas canvas, float fromX, float toX) { int count = canvas.save(); canvas.clipRect( fromX, (int) ((canvas.getHeight() - mStrokeWidth) / 2), toX, (int) ((canvas.getHeight() + mStrokeWidth) / 2)); mBackgroundDrawable.draw(canvas); canvas.restoreToCount(count); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mInnerRectF.set(0, 0, canvas.getWidth(), canvas.getHeight()); final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f); mInnerRectF.inset(halfBorder, halfBorder); canvas.drawArc(mInnerRectF, 0, 360, true, mBackgroundPaint); switch (mProgressFillType) { case FILL_TYPE_RADIAL: float sweepAngle = 360 * mProgress / mMax; canvas.drawArc(mInnerRectF, mStartAngle, sweepAngle, true, mProgressPaint); break; case FILL_TYPE_CENTER: float centerX = canvas.getWidth() / 2; float centerY = canvas.getHeight() / 2; float radius = (canvas.getWidth() / 2) * ((float) mProgress / mMax); canvas.drawCircle( centerX, centerY, radius + 0.5f - mStrokePaint.getStrokeWidth(), mProgressPaint); break; default: throw new IllegalArgumentException("Invalid Progress Fill = " + mProgressFillType); } if (!TextUtils.isEmpty(mText) && mShowText) { if (!TextUtils.isEmpty(mTypeface)) { Typeface typeface = sTypefaceCache.get(mTypeface); if (null == typeface && null != getResources()) { AssetManager assets = getResources().getAssets(); if (null != assets) { typeface = Typeface.createFromAsset(assets, mTypeface); sTypefaceCache.put(mTypeface, typeface); } } mTextPaint.setTypeface(typeface); } int xPos = canvas.getWidth() / 2; int yPos = (int) ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)); canvas.drawText(mText, xPos, yPos, mTextPaint); } if (null != mImage && mShowImage) { int drawableSize = mImage.getIntrinsicWidth(); mImageRect.set(0, 0, drawableSize, drawableSize); mImageRect.offset((getWidth() - drawableSize) / 2, (getHeight() - drawableSize) / 2); mImage.setBounds(mImageRect); mImage.draw(canvas); } if (mShowStroke) { canvas.drawOval(mInnerRectF, mStrokePaint); } }
@Override public void draw(Canvas canvas, float offset) { if (leftClickSize == null) { leftClickSize = new RectF(0, 5 * canvas.getHeight() / 6, canvas.getWidth() / 2, canvas.getHeight()); } if (isLeftClickPressed) { drawButton(canvas, leftClickButton_pressed, rectWithOffset(leftClickSize, offset)); } else { drawButton(canvas, leftClickButton, rectWithOffset(leftClickSize, offset)); } }
@Override protected void onDraw(Canvas canvas) { if (imageOffset != 0) { canvas.save(); canvas.translate(0f, imageOffset); canvas.clipRect(0f, 0f, canvas.getWidth(), canvas.getHeight() + imageOffset); super.onDraw(canvas); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), scrimPaint); canvas.restore(); } else { super.onDraw(canvas); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), scrimPaint); } }
@Override public void draw(Canvas canvas) { mCanvasWidth = canvas.getWidth(); mCanvasHeight = canvas.getHeight(); mRadius = map( getLevel(), 0, DRAWABLE_MAX_LEVEL, 0, diagonalLength(canvas.getWidth(), canvas.getHeight()) / 2); canvas.drawCircle(canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f, mRadius, mPaint); }
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap( 1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
@Override /** Draw the UAV, the position desired, and the camera POI */ protected void onDraw(Canvas canvas) { draw( canvas, R.drawable.map_positioner_uav, (int) m_to_px(this, pos_x), (int) m_to_px(this, pos_y)); draw( canvas, R.drawable.map_positioner_waypoint, (int) m_to_px(this, desired_x), (int) -m_to_px(this, desired_y)); // draw(canvas, R.drawable.map_positioner_poi, (int) m_to_px(this, poi_x), (int) // -m_to_px(this, poi_y)); // Annotate the desired position canvas.drawText( "(" + String.format("%.1f", desired_x) + "," + String.format("%.1f", desired_y) + "," + String.format("%.1f", desired_z) + ")", m_to_px(this, desired_x) + canvas.getWidth() / 2 + 25, -m_to_px(this, desired_y) + canvas.getHeight() / 2, paint); }
// DIRECT repaint method public void repaint2() { Canvas c = null; try { c = getHolder().lockCanvas(); if (c != null) { // enable anti-aliasing c.setDrawFilter(new PaintFlagsDrawFilter(1, Paint.ANTI_ALIAS_FLAG)); // clean background Paint paint = new Paint(); paint.setColor(drawable.getBackground().getAndroidColor()); c.drawRect(0, 0, c.getWidth(), c.getHeight(), paint); // set dimensions drawable.setWidth(getWidth()); drawable.setHeight(getHeight()); // do the drawing drawable.draw(new Graphics(c)); } } catch (Exception e) { // ignore } finally { if (c != null) { getHolder().unlockCanvasAndPost(c); } } }
@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); if (mService.isScrimSrcModeEnabled()) { // We need to ensure that our window is always drawn fully even when we have paddings, // since we simulate it to be opaque. int paddedBottom = getHeight() - getPaddingBottom(); int paddedRight = getWidth() - getPaddingRight(); if (getPaddingTop() != 0) { canvas.drawRect(0, 0, getWidth(), getPaddingTop(), mTransparentSrcPaint); } if (getPaddingBottom() != 0) { canvas.drawRect(0, paddedBottom, getWidth(), getHeight(), mTransparentSrcPaint); } if (getPaddingLeft() != 0) { canvas.drawRect(0, getPaddingTop(), getPaddingLeft(), paddedBottom, mTransparentSrcPaint); } if (getPaddingRight() != 0) { canvas.drawRect( paddedRight, getPaddingTop(), getWidth(), paddedBottom, mTransparentSrcPaint); } } if (DEBUG) { Paint pt = new Paint(); pt.setColor(0x80FFFF00); pt.setStrokeWidth(12.0f); pt.setStyle(Paint.Style.STROKE); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), pt); } }
@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); int count = mPagesCount; if (count == 0) { return; } float cx = canvas.getWidth() >> 1; float cy = canvas.getHeight() >> 1; int totalCount = 2 * count - 1; int halfCount = totalCount / 2; float itemWithOffset = mIndicatorElementSize + mIndicatorElementSpacing; float halfItemWithOffset = mIndicatorElementSize / 2 + mIndicatorElementSpacing; float halfSize = mIndicatorElementSize / 2f; float halfSpacing = mIndicatorElementSpacing / 2f; float startX = cx - halfCount * (mIndicatorElementSize + mIndicatorElementSpacing); float top = cy - halfSize; float bottom = cy + halfSize; // if we have odd elements - need to set indicators in center if (totalCount % 2 != 0) { startX -= halfSize + halfSpacing; } int i; for (i = 0; i < totalCount; i++) { // skip empty spaces if (i % 2 != 0) { continue; } float left = startX + (mIndicatorElementSize + mIndicatorElementSpacing) * i; float right = left + mIndicatorElementSize; mElementBounds.set(left, top, right, bottom); mRenderer.draw(canvas, mElementBounds, mIndicatorPaint, false); } // multiply on 2 because there are spaces between elements float activeItemOffset = (mSelectedPosition + mScrolledOffset) * 2; float left = startX + (mIndicatorElementSize + mIndicatorElementSpacing) * activeItemOffset; float right = left + mIndicatorElementSize; mElementBounds.set(left, top, right, bottom); canvas.clipRect(mClipBounds); canvas.save(); canvas.rotate(ANGLE_360 * mScrolledOffset, mElementBounds.centerX(), mElementBounds.centerY()); mRenderer.draw(canvas, mElementBounds, mIndicatorSelectedPaint, true); canvas.restore(); if (mIsInfiniteScroll && mSelectedPosition == mPagesCount - 1) { // isInfinite && isEnd activeItemOffset = (1f - mScrolledOffset) * 2; left = mClipBounds.left - itemWithOffset * activeItemOffset + halfItemWithOffset; right = left + mIndicatorElementSize; mElementBounds.set(left, top, right, bottom); canvas.save(); canvas.rotate( ANGLE_360 * mScrolledOffset, mElementBounds.centerX(), mElementBounds.centerY()); mRenderer.draw(canvas, mElementBounds, mIndicatorSelectedPaint, true); canvas.restore(); } }
public final boolean draw(Canvas canvas, float left, float top, Paint paint) { if (bitmapArray != null) { for (int i = 0; i < bitmapArray.length; i++) { for (int j = 0; j < bitmapArray[i].length; j++) { Bitmap bmp = bitmapArray[i][j]; if (bmp != null && !bmp.isRecycled()) { float dleft = left + j * bmp.getWidth(); if (dleft > canvas.getWidth() || dleft + bmp.getWidth() < 0) { continue; } float dtop = top + i * bmp.getHeight(); if (dtop > canvas.getHeight() || dtop + bmp.getHeight() < 0) { continue; } canvas.drawBitmap(bmp, dleft, dtop, paint); } } } return true; } else if (bitmap != null && !bitmap.isRecycled()) { canvas.drawBitmap(bitmap, left, top, paint); return true; } return false; }
public void run() { Log.i(TAG, "Starting processing thread"); while (true) { Bitmap bmp = null; synchronized (this) { if (mCamera == null) break; if (!mCamera.grab()) { Log.e(TAG, "mCamera.grab() failed"); break; } bmp = processFrame(mCamera); } if (bmp != null) { Canvas canvas = mHolder.lockCanvas(); if (canvas != null) { canvas.drawBitmap( bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); mHolder.unlockCanvasAndPost(canvas); } // bmp.recycle(); } } Log.i(TAG, "Finishing processing thread"); }
@Override protected void dispatchDraw(Canvas canvas) { final int saveCount = canvas.save(); final Matrix m = mController.getEffectsMatrix(); if (!m.isIdentity()) canvas.concat(m); final float alpha = mController.getEffectsAlpha(); if (alpha != 1f) canvas.saveLayerAlpha( 0, 0, canvas.getWidth(), canvas.getHeight(), (int) (255 * alpha), Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); super.dispatchDraw(canvas); final int fadeFactor = mController.getFadeFactor(); if (fadeFactor > 0f) { mFadePaint.setColor(Color.argb(fadeFactor, 0, 0, 0)); canvas.drawRect(0, 0, getWidth(), getHeight(), mFadePaint); } canvas.restoreToCount(saveCount); }
@SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { switch (this.showViewTypeEnum) { case Download_Status: // 下载状态 drawOnSuccessfulConnect(canvas, true); break; case Suspended_state: // 暂停状态 drawOnSuccessfulConnect(canvas, false); break; case Networking_status: // 联网状态 canvas.rotate(degress, canvas.getWidth() / 2, canvas.getHeight() / 2); degress += 10; drawOnConnecting(canvas); Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { handler.post( new Runnable() { @Override public void run() { CircleProgressBar.this.invalidate(); } }); } }, 25); break; default: break; } }
@Override protected void afterDraw(Canvas canvas, Paint paint, GameView gameView) { // 在afterDraw中不调用super.afterDraw方法 if (!isDestroyed()) { // 在绘制一定次数后要改变方向或速度 int canvasHeight = canvas.getHeight(); if (status != STATUS_DOWN3) { float maxY = getY() + getHeight(); if (status == STATUS_DOWN1) { // 第一次向下 if (maxY >= canvasHeight * 0.25) { // 当第一次下降到临界值时改变方向,向上 setSpeed(-5); status = STATUS_UP2; } } else if (status == STATUS_UP2) { // 第二次向上 if (maxY + this.getSpeed() <= 0) { // 第二次上升到临界值时改变方向,向下 setSpeed(13); status = STATUS_DOWN3; } } } if (status == STATUS_DOWN3) { if (getY() >= canvasHeight) { destroy(); } } } }
/** Draws the generated bitmap to the canvas */ public void run() { mThreadRun = true; while (mThreadRun) { Bitmap bmp = null; synchronized (this) { try { this.wait(); bmp = processFrame(mFrame); } catch (InterruptedException e) { e.printStackTrace(); } } if (bmp != null) { Canvas canvas = mHolder.lockCanvas(); if (canvas != null) { canvas.drawBitmap( bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null); mHolder.unlockCanvasAndPost(canvas); } bmp.recycle(); } } }
@Override protected void onDraw(Canvas canvas) { float left = getPaddingLeft() + mUnreachedWidth / 2; float top = getPaddingTop() + mUnreachedWidth / 2; float right = canvas.getWidth() - getPaddingRight() - mUnreachedWidth / 2; float bottom = canvas.getHeight() - getPaddingBottom() - mUnreachedWidth / 2; float centerX = (left + right) / 2; float centerY = (top + bottom) / 2; float wheelRadius = (canvas.getWidth() - getPaddingLeft() - getPaddingRight()) / 2 - mUnreachedWidth / 2; if (isHasCache) { if (mCacheCanvas == null) { buildCache(centerX, centerY, wheelRadius); } canvas.drawBitmap(mCacheBitmap, 0, 0, null); } else { canvas.drawCircle(centerX, centerY, wheelRadius, mWheelPaint); } // 画选中区域 canvas.drawArc( new RectF(left, top, right, bottom), -90, (float) mCurAngle, false, mReachedPaint); // 画锚点 canvas.drawCircle(mWheelCurX, mWheelCurY, mPointerRadius, mPointerPaint); }
/* * Drawing */ @Override public void draw(Canvas canvas) { final boolean positionChanged = adapterPositionChanged(); if (rippleOverlay) { if (!positionChanged) { rippleBackground.draw(canvas); } super.draw(canvas); if (!positionChanged) { if (rippleRoundedCorners != 0) { Path clipPath = new Path(); RectF rect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight()); clipPath.addRoundRect( rect, rippleRoundedCorners, rippleRoundedCorners, Path.Direction.CW); canvas.clipPath(clipPath); } canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint); } } else { if (!positionChanged) { rippleBackground.draw(canvas); canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint); } super.draw(canvas); } }
@Override protected void dispatchDraw(Canvas canvas) { if (mShowcaseX < 0 || mShowcaseY < 0 || mIsRedundant) { super.dispatchDraw(canvas); return; } boolean recalculatedCling = mShowcaseDrawer.calculateShowcaseRect(mShowcaseX, mShowcaseY); boolean recalculateText = recalculatedCling || mAlteredText; mAlteredText = false; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB && !mHasNoTarget) { Path path = new Path(); path.addCircle(mShowcaseX, mShowcaseY, mShowcaseRadius, Path.Direction.CW); canvas.clipPath(path, Op.DIFFERENCE); } // Draw background color canvas.drawColor(mBackgroundColor); // Draw the showcase drawable if (!mHasNoTarget) { mShowcaseDrawer.drawShowcase( canvas, mShowcaseX, mShowcaseY, mScaleMultiplier, mShowcaseRadius); } // Draw the text on the screen, recalculating its position if necessary if (recalculateText) { mTextDrawer.calculateTextPosition(canvas.getWidth(), canvas.getHeight(), this); } mTextDrawer.draw(canvas, recalculateText); super.dispatchDraw(canvas); }
/** * The graphical representation of a path. * * @param canvas the canvas to paint to * @param points the points that are contained in the path to paint * @param paint the paint to be used for painting * @param circular if the path ends with the start point */ protected void drawPath(Canvas canvas, float[] points, Paint paint, boolean circular) { Path path = new Path(); int height = canvas.getHeight(); int width = canvas.getWidth(); float[] tempDrawPoints; if (points.length < 4) { return; } tempDrawPoints = calculateDrawPoints(points[0], points[1], points[2], points[3], height, width); path.moveTo(tempDrawPoints[0], tempDrawPoints[1]); path.lineTo(tempDrawPoints[2], tempDrawPoints[3]); for (int i = 4; i < points.length; i += 2) { if ((points[i - 1] < 0 && points[i + 1] < 0) || (points[i - 1] > height && points[i + 1] > height)) { continue; } tempDrawPoints = calculateDrawPoints( points[i - 2], points[i - 1], points[i], points[i + 1], height, width); if (!circular) { path.moveTo(tempDrawPoints[0], tempDrawPoints[1]); } path.lineTo(tempDrawPoints[2], tempDrawPoints[3]); } if (circular) { path.lineTo(points[0], points[1]); } canvas.drawPath(path, paint); }
@SuppressLint("DrawAllocation") @Override public void onDraw(Canvas canvas) { // load the bitmap loadBitmap(); // init shader if (image != null) { shader = new BitmapShader( Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); paint.setShader(shader); int circleCenter = viewWidth / 2; // circleCenter is the x or y of the view's center // radius is the radius in pixels of the cirle to be drawn // paint contains the shader that will texture the shape canvas.drawCircle( circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder); canvas.drawCircle( circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint); } }
// TODO control the width of the image so no exceed the layout width private Bitmap composeImage(int maxHeight) { Bitmap source = BitmapFactory.decodeFile(photoFile.getAbsolutePath()); int rgbAvg[] = getAverageColorRGB(source); Log.d(TAG, "Avg color = " + rgbAvg); String nearestColor = Colors.nearestMaterialColor(Color.rgb(rgbAvg[0], rgbAvg[1], rgbAvg[2])); Log.d(TAG, "nearestColor = " + nearestColor); int complColor = Colors.getMaterialColor(Colors.complementMaterialColor(nearestColor)); Log.d(TAG, "complColor = " + Colors.complementMaterialColor(nearestColor)); double ratio = source.getWidth() / (double) source.getHeight(); int newWidth = (int) (ratio * maxHeight); Matrix matrix = new Matrix(); matrix.postTranslate(5, 5); matrix.postScale( ((float) newWidth) / source.getWidth(), ((float) maxHeight) / source.getHeight()); // TODO use dp instead of px in the image border Bitmap imageBitmap = Bitmap.createBitmap(newWidth + 10, maxHeight + 10, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(imageBitmap); Paint paintComp = new Paint(); paintComp.setStyle(Paint.Style.FILL); paintComp.setAntiAlias(true); paintComp.setColor(complColor); Path path = getRoundedRectPath(0, 0, canvas.getWidth(), canvas.getHeight(), 5, 5); canvas.drawPath(path, paintComp); canvas.drawBitmap(source, matrix, null); return imageBitmap; }
private Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); } else { bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth() + 1, drawable.getIntrinsicHeight() + 1, BITMAP_CONFIG); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } }
@Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawColor(Color.BLACK); Paint textPaint = new Paint(); textPaint.setARGB(50, 254, 100, 45); textPaint.setTextAlign(Align.CENTER); textPaint.setTextSize(50); textPaint.setTypeface(font); canvas.drawText("Ajith is ..", canvas.getWidth() / 2, 200, textPaint); canvas.drawBitmap(star, (canvas.getWidth() / 2), y, null); if (y < canvas.getHeight()) { y += 10; } else { y = 0; } Rect middlRect = new Rect(); middlRect.set(0, 400, canvas.getWidth(), 550); Paint ourBlue = new Paint(); ourBlue.setColor(Color.BLUE); canvas.drawRect(middlRect, ourBlue); invalidate(); }