List of usage examples for android.graphics Canvas getWidth
public int getWidth()
From source file:com.google.zxing.client.android.ViewfinderView.java
@SuppressLint("DrawAllocation") @Override// w w w . j a v a2s . c om public void onDraw(Canvas canvas) { if (cameraManager == null) { return; // not ready yet, early draw before done configuring } Rect frame = cameraManager.getFramingRect(); Rect previewFrame = cameraManager.getFramingRectInPreview(); if (frame == null || previewFrame == null) { return; } 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, paint); canvas.drawRect(frame.right, frame.top, width, frame.bottom, paint); canvas.drawRect(0, frame.bottom, width, height, paint); /// // paint.setColor(getResources().getColor(R.color.encode_view)); int w = 16; int h = 2; int margin = 2; // canvas.drawRect(frame.left - margin - h, frame.top - margin - h, frame.left - margin + w - h, frame.top - margin + h - h, paint); canvas.drawRect(frame.left - margin - h, frame.top - margin - h, frame.left - margin + h - h, frame.top - margin + w - h, paint); // ? canvas.drawRect(frame.right + margin - w + h, frame.top - margin - h, frame.right + margin + h, frame.top - margin + h - h, paint); canvas.drawRect(frame.right + margin - h + h, frame.top - margin - h, frame.right + margin + h, frame.top - margin + w - h, paint); // canvas.drawRect(frame.left - margin - h, frame.bottom + margin - h + h, frame.left - margin + w - h, frame.bottom + margin + h, paint); canvas.drawRect(frame.left - margin - h, frame.bottom + margin - w + h, frame.left - margin + h - h, frame.bottom + margin + h, paint); // ? canvas.drawRect(frame.right + margin - w + h, frame.bottom + margin - h + h, frame.right + margin + h, frame.bottom + margin + h, paint); canvas.drawRect(frame.right + margin - h + h, frame.bottom + margin - w + h, frame.right + margin + h, frame.bottom + margin + h, paint); ///? drawLineRound(canvas, frame, paint); if (statusText != null) { RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) statusText.getLayoutParams(); lp.topMargin = frame.top - statusText.getMeasuredHeight() - 28; statusText.setLayoutParams(lp); statusText.setVisibility(View.VISIBLE); } if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { paint.setAlpha(CURRENT_POINT_OPACITY); } canvas.drawBitmap(resultBitmap, null, frame, paint); } else { // ?? if ((i += 5) < frame.bottom - frame.top) { /* ?? */ mRect.set(frame.left, frame.top + i - 1, frame.right, frame.top + i + 1); lineDrawable.setBounds(mRect); lineDrawable.draw(canvas); // invalidate(); } else { i = 0; } float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height(); List<ResultPoint> currentPossible = possibleResultPoints; List<ResultPoint> currentLast = lastPossibleResultPoints; int frameLeft = frame.left; int frameTop = frame.top; if (currentPossible.isEmpty()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new ArrayList<>(5); lastPossibleResultPoints = currentPossible; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { paint.setAlpha(CURRENT_POINT_OPACITY); } paint.setColor(resultPointColor); synchronized (currentPossible) { for (ResultPoint point : currentPossible) { canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint); } } } if (currentLast != null) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { paint.setAlpha(CURRENT_POINT_OPACITY / 2); } paint.setColor(resultPointColor); synchronized (currentLast) { float radius = POINT_SIZE / 2.0f; for (ResultPoint point : currentLast) { canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), radius, paint); } } } // Request another update at the animation interval, but only repaint the laser line, // not the entire viewfinder mask. postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE, frame.top - POINT_SIZE, frame.right + POINT_SIZE, frame.bottom + POINT_SIZE); } }
From source file:de.hs_bremen.aurora_hunter.ui.views.PredictionGraphView.java
public void onDraw(Canvas canvas) { if (mPoints.size() == 0) { return;//from w w w . j a v a 2 s.c om } int yOffsetForLabels = 0; if (mMax * mScaleFactor > 0.8) { yOffsetForLabels = 15; } Path path = new Path(); int height = canvas.getHeight(); int width = canvas.getWidth(); for (Point point : mPoints) { point.x = point.percentageX * width + 60; // Log.i("mScaleFactor", " : " + mScaleFactor); //Log.i("percent", " : " + percent); point.y = (float) ((1 - point.percentageY * mScaleFactor) * height) + yOffsetForLabels * 3; } if (mPoints.size() > 1) { //calcuate x/y based on size of canvas for (int i = 0; i < mPoints.size(); i++) { if (i >= 0) { Point point = mPoints.get(i); // Log.i("dx",point.x + " - " + point.y ); if (i == 0) { Point next = mPoints.get(i + 1); point.dx = ((next.x - point.x) / 5); point.dy = ((next.y - point.y) / 5); } else if (i == mPoints.size() - 1) { Point prev = mPoints.get(i - 1); point.dx = ((point.x - prev.x) / 5); point.dy = ((point.y - prev.y) / 5); } else { Point next = mPoints.get(i + 1); Point prev = mPoints.get(i - 1); point.dx = ((next.x - prev.x) / 5); point.dy = ((next.y - prev.y) / 5); } } } } if (mPoints.size() > 0) { path.moveTo(0, (float) (canvas.getHeight() * 0.8)); path.lineTo(0, mPoints.get(0).y); } boolean first = true; for (int i = 0; i < mPoints.size(); i++) { Point point = mPoints.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 = mPoints.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); } } if (mPoints.size() > 0) { path.lineTo(width, mPoints.get(mPoints.size() - 1).y); } path.lineTo(width, height); path.lineTo(0, height); canvas.drawPath(path, mGraphPaint); int detlaY = 30; for (Point p : mPoints) { int val = (int) Math.round(p.percentageY * 100); //if last element if (mPoints.indexOf(p) == mPoints.size() - 1) { //Log.i("last", p.toString()); if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 150, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 150, p.y - detlaY + yOffsetForLabels, mTextPaint); } } else { if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 20, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 20, p.y - detlaY + yOffsetForLabels, mTextPaint); } } //Log.i("point", p.toString()); } // Log.i("Lenght", mPoints.size() + " "); float levelStart = (float) (canvas.getHeight() - (mNotificationLevel * canvas.getHeight())); float lineHeight = DpToPixelUtil.convertDpToPixel(2, getContext()); if (mViewMode == PredictionFragment.NOTIFICATION_MODE.SET_NOTIFICATION_LEVEL) { mNotificationLevelPaint.setAlpha(150); float leftOffset = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { float arcHeight = DpToPixelUtil.convertDpToPixel(30, getContext()); leftOffset = arcHeight; RectF rectF = new RectF(getWidth() - arcHeight, levelStart - arcHeight / 2, getWidth(), levelStart + arcHeight / 2); canvas.drawArc(rectF, 0, 360, true, mNotificationLevelPaint); } canvas.drawRect(0, levelStart, canvas.getWidth() - leftOffset, levelStart + lineHeight, mNotificationLevelPaint); mNotificationLevelPaint.setAlpha(20); canvas.drawRect(0, levelStart + lineHeight, canvas.getWidth(), canvas.getHeight(), mNotificationLevelPaint); String text = Math.round(mNotificationLevel * 100) + "%"; canvas.drawText(text, DpToPixelUtil.convertDpToPixel(10, getContext()), levelStart + DpToPixelUtil.convertDpToPixel(25, getContext()), mNotificationLevelTextPaint); } else { mNotificationLevelPaint.setAlpha(30); canvas.drawRect(0, levelStart, canvas.getWidth(), levelStart + lineHeight, mNotificationLevelPaint); } }
From source file:com.waz.zclient.controllers.notifications.NotificationsController.java
private Bitmap getAppIcon() { try {//from w ww .j a v a 2 s .co m Drawable icon = context.getPackageManager().getApplicationIcon(context.getPackageName()); if (icon instanceof BitmapDrawable) { return ((BitmapDrawable) icon).getBitmap(); } Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); icon.draw(canvas); return bitmap; } catch (PackageManager.NameNotFoundException e) { return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher_wire); } }
From source file:com.lyue.aw_jitpack_aar.app.zxing.zxing.view.ViewfinderView.java
@Override public void onDraw(Canvas canvas) { //CameraManager Rect frame = CameraManager.get().getFramingRect(); if (frame == null) { return;//from ww w . j a v a2 s. c o m } // if (!isFirst) { isFirst = true; slideTop = frame.top; slideBottom = frame.bottom; } // int width = canvas.getWidth(); int height = canvas.getHeight(); 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); if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle paint.setAlpha(OPAQUE); canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint); } else { //8 paint.setColor(Color.GREEN); canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint); canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint); canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint); canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint); canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint); //,SPEEN_DISTANCE slideTop += SPEEN_DISTANCE; if (slideTop >= frame.bottom) { slideTop = frame.top; } Rect lineRect = new Rect(); lineRect.left = frame.left; lineRect.right = frame.right; lineRect.top = slideTop; lineRect.bottom = slideTop + 18; canvas.drawBitmap( ((BitmapDrawable) (getResources().getDrawable(R.drawable.aar_qrcode_scan_line))).getBitmap(), null, lineRect, paint); // paint.setColor(Color.WHITE); paint.setTextSize(TEXT_SIZE * density); paint.setAlpha(0x40); paint.setTypeface(Typeface.create("System", Typeface.BOLD)); String text = getResources().getString(R.string.scan_text); float textWidth = paint.measureText(text); canvas.drawText(text, (width - textWidth) / 2, (float) (frame.bottom + (float) TEXT_PADDING_TOP * density), paint); Collection<ResultPoint> currentPossible = possibleResultPoints; Collection<ResultPoint> currentLast = lastPossibleResultPoints; if (currentPossible.isEmpty()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new HashSet<ResultPoint>(5); lastPossibleResultPoints = currentPossible; paint.setAlpha(OPAQUE); paint.setColor(resultPointColor); for (ResultPoint point : currentPossible) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint); } } if (currentLast != null) { paint.setAlpha(OPAQUE / 2); paint.setColor(resultPointColor); for (ResultPoint point : currentLast) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint); } } // postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); } }
From source file:com.example.firstocr.ViewfinderView.java
@SuppressWarnings("unused") @Override//from ww w.j a v a2 s .c om public void onDraw(Canvas canvas) { Rect frame = cameraManager.getFramingRect(); if (frame == null) { return; } int width = canvas.getWidth(); int height = canvas.getHeight(); // Draw the exterior (i.e. outside the framing rect) darkened paint.setColor(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); // If we have an OCR result, overlay its information on the viewfinder. if (resultText != null) { // Only draw text/bounding boxes on viewfinder if it hasn't been resized since the OCR was requested. Point bitmapSize = resultText.getBitmapDimensions(); previewFrame = cameraManager.getFramingRectInPreview(); if (bitmapSize.x == previewFrame.width() && bitmapSize.y == previewFrame.height()) { float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height(); if (DRAW_REGION_BOXES) { regionBoundingBoxes = resultText.getRegionBoundingBoxes(); for (int i = 0; i < regionBoundingBoxes.size(); i++) { paint.setAlpha(0xA0); paint.setColor(Color.MAGENTA); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); rect = regionBoundingBoxes.get(i); canvas.drawRect(frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_TEXTLINE_BOXES) { // Draw each textline textlineBoundingBoxes = resultText.getTextlineBoundingBoxes(); paint.setAlpha(0xA0); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < textlineBoundingBoxes.size(); i++) { rect = textlineBoundingBoxes.get(i); canvas.drawRect(frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_STRIP_BOXES) { stripBoundingBoxes = resultText.getStripBoundingBoxes(); paint.setAlpha(0xFF); paint.setColor(Color.YELLOW); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < stripBoundingBoxes.size(); i++) { rect = stripBoundingBoxes.get(i); canvas.drawRect(frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_WORD_BOXES || DRAW_WORD_TEXT) { // Split the text into words wordBoundingBoxes = resultText.getWordBoundingBoxes(); // for (String w : words) { // Log.e("ViewfinderView", "word: " + w); // } //Log.d("ViewfinderView", "There are " + words.length + " words in the string array."); //Log.d("ViewfinderView", "There are " + wordBoundingBoxes.size() + " words with bounding boxes."); } if (DRAW_WORD_BOXES) { paint.setAlpha(0xFF); paint.setColor(0xFF00CCFF); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < wordBoundingBoxes.size(); i++) { // Draw a bounding box around the word rect = wordBoundingBoxes.get(i); canvas.drawRect(frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_WORD_TEXT) { words = resultText.getText().replace("\n", " ").split(" "); int[] wordConfidences = resultText.getWordConfidences(); for (int i = 0; i < wordBoundingBoxes.size(); i++) { boolean isWordBlank = true; try { if (!words[i].equals("")) { isWordBlank = false; } } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } // Only draw if word has characters if (!isWordBlank) { // Draw a white background around each word rect = wordBoundingBoxes.get(i); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); if (DRAW_TRANSPARENT_WORD_BACKGROUNDS) { // Higher confidence = more opaque, less transparent background paint.setAlpha(wordConfidences[i] * 255 / 100); } else { paint.setAlpha(255); } canvas.drawRect(frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); // Draw the word in black text paint.setColor(Color.BLACK); paint.setAlpha(0xFF); paint.setAntiAlias(true); paint.setTextAlign(Align.LEFT); // Adjust text size to fill rect paint.setTextSize(100); paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text Rect bounds = new Rect(); paint.getTextBounds(words[i], 0, words[i].length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // figure out what textSize setting would create that height of text float size = (((float) (rect.height()) / h) * 100f); // and set it into the paint paint.setTextSize(size); // Now set the scale. // do calculation with scale of 1.0 (no scale) paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text. paint.getTextBounds(words[i], 0, words[i].length(), bounds); // determine the width int w = bounds.right - bounds.left; // calculate the baseline to use so that the entire text is visible including the descenders int text_h = bounds.bottom - bounds.top; int baseline = bounds.bottom + ((rect.height() - text_h) / 2); // determine how much to scale the width to fit the view float xscale = ((float) (rect.width())) / w; // set the scale for the text paint paint.setTextScaleX(xscale); canvas.drawText(words[i], frame.left + rect.left * scaleX, frame.top + rect.bottom * scaleY - baseline, paint); } } } // if (DRAW_CHARACTER_BOXES || DRAW_CHARACTER_TEXT) { // characterBoundingBoxes = resultText.getCharacterBoundingBoxes(); // } // // if (DRAW_CHARACTER_BOXES) { // // Draw bounding boxes around each character // paint.setAlpha(0xA0); // paint.setColor(0xFF00FF00); // paint.setStyle(Style.STROKE); // paint.setStrokeWidth(1); // for (int c = 0; c < characterBoundingBoxes.size(); c++) { // Rect characterRect = characterBoundingBoxes.get(c); // canvas.drawRect(frame.left + characterRect.left * scaleX, // frame.top + characterRect.top * scaleY, // frame.left + characterRect.right * scaleX, // frame.top + characterRect.bottom * scaleY, paint); // } // } // // if (DRAW_CHARACTER_TEXT) { // // Draw letters individually // for (int i = 0; i < characterBoundingBoxes.size(); i++) { // Rect r = characterBoundingBoxes.get(i); // // // Draw a white background for every letter // int meanConfidence = resultText.getMeanConfidence(); // paint.setColor(Color.WHITE); // paint.setAlpha(meanConfidence * (255 / 100)); // paint.setStyle(Style.FILL); // canvas.drawRect(frame.left + r.left * scaleX, // frame.top + r.top * scaleY, // frame.left + r.right * scaleX, // frame.top + r.bottom * scaleY, paint); // // // Draw each letter, in black // paint.setColor(Color.BLACK); // paint.setAlpha(0xFF); // paint.setAntiAlias(true); // paint.setTextAlign(Align.LEFT); // String letter = ""; // try { // char c = resultText.getText().replace("\n","").replace(" ", "").charAt(i); // letter = Character.toString(c); // // if (!letter.equals("-") && !letter.equals("_")) { // // // Adjust text size to fill rect // paint.setTextSize(100); // paint.setTextScaleX(1.0f); // // // ask the paint for the bounding rect if it were to draw this text // Rect bounds = new Rect(); // paint.getTextBounds(letter, 0, letter.length(), bounds); // // // get the height that would have been produced // int h = bounds.bottom - bounds.top; // // // figure out what textSize setting would create that height of text // float size = (((float)(r.height())/h)*100f); // // // and set it into the paint // paint.setTextSize(size); // // // Draw the text as is. We don't really need to set the text scale, because the dimensions // // of the Rect should already be suited for drawing our letter. // canvas.drawText(letter, frame.left + r.left * scaleX, frame.top + r.bottom * scaleY, paint); // } // } catch (StringIndexOutOfBoundsException e) { // e.printStackTrace(); // } catch (Exception e) { // e.printStackTrace(); // } // } // } } } // Draw a two pixel solid border inside the framing rect paint.setAlpha(0); paint.setStyle(Style.FILL); paint.setColor(frameColor); canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint); canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint); canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint); canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint); // Draw the framing rect corner UI elements paint.setColor(cornerColor); canvas.drawRect(frame.left - 15, frame.top - 15, frame.left + 15, frame.top, paint); canvas.drawRect(frame.left - 15, frame.top, frame.left, frame.top + 15, paint); canvas.drawRect(frame.right - 15, frame.top - 15, frame.right + 15, frame.top, paint); canvas.drawRect(frame.right, frame.top - 15, frame.right + 15, frame.top + 15, paint); canvas.drawRect(frame.left - 15, frame.bottom, frame.left + 15, frame.bottom + 15, paint); canvas.drawRect(frame.left - 15, frame.bottom - 15, frame.left, frame.bottom, paint); canvas.drawRect(frame.right - 15, frame.bottom, frame.right + 15, frame.bottom + 15, paint); canvas.drawRect(frame.right, frame.bottom - 15, frame.right + 15, frame.bottom + 15, paint); // Request another update at the animation interval, but don't repaint the entire viewfinder mask. //postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); }
From source file:im.neon.util.VectorUtils.java
/** * Create an avatar bitmap from a text.//from ww w. ja v a2 s . c o m * * @param backgroundColor the background color. * @param text the text to display. * @param pixelsSide the avatar side in pixels * @return the generated bitmap */ private static Bitmap createAvatar(int backgroundColor, String text, int pixelsSide) { android.graphics.Bitmap.Config bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; Bitmap bitmap = Bitmap.createBitmap(pixelsSide, pixelsSide, bitmapConfig); Canvas canvas = new Canvas(bitmap); canvas.drawColor(backgroundColor); // prepare the text drawing Paint textPaint = new Paint(); textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); textPaint.setColor(Color.WHITE); // the text size is proportional to the avatar size. // by default, the avatar size is 42dp, the text size is 28 dp (not sp because it has to be fixed). textPaint.setTextSize(pixelsSide * 2 / 3); // get its size Rect textBounds = new Rect(); textPaint.getTextBounds(text, 0, text.length(), textBounds); // draw the text in center canvas.drawText(text, (canvas.getWidth() - textBounds.width() - textBounds.left) / 2, (canvas.getHeight() + textBounds.height() - textBounds.bottom) / 2, textPaint); // Return the avatar return bitmap; }
From source file:org.gateshipone.malp.application.background.NotificationManager.java
public synchronized void updateNotification(MPDTrack track, MPDCurrentStatus.MPD_PLAYBACK_STATE state) { if (track != null) { mNotificationBuilder = new NotificationCompat.Builder(mService); // Open application intent Intent contentIntent = new Intent(mService, MainActivity.class); contentIntent.putExtra(MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW, MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW_NOWPLAYINGVIEW); contentIntent.addFlags(//from w w w . ja v a2 s . c o m Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NO_HISTORY); PendingIntent contentPendingIntent = PendingIntent.getActivity(mService, INTENT_OPENGUI, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); mNotificationBuilder.setContentIntent(contentPendingIntent); // Set pendingintents // Previous song action Intent prevIntent = new Intent(BackgroundService.ACTION_PREVIOUS); PendingIntent prevPendingIntent = PendingIntent.getBroadcast(mService, INTENT_PREVIOUS, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action prevAction = new NotificationCompat.Action.Builder( R.drawable.ic_skip_previous_48dp, "Previous", prevPendingIntent).build(); // Pause/Play action PendingIntent playPauseIntent; int playPauseIcon; if (state == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING) { Intent pauseIntent = new Intent(BackgroundService.ACTION_PAUSE); playPauseIntent = PendingIntent.getBroadcast(mService, INTENT_PLAYPAUSE, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); playPauseIcon = R.drawable.ic_pause_48dp; } else { Intent playIntent = new Intent(BackgroundService.ACTION_PLAY); playPauseIntent = PendingIntent.getBroadcast(mService, INTENT_PLAYPAUSE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); playPauseIcon = R.drawable.ic_play_arrow_48dp; } NotificationCompat.Action playPauseAction = new NotificationCompat.Action.Builder(playPauseIcon, "PlayPause", playPauseIntent).build(); // Stop action Intent stopIntent = new Intent(BackgroundService.ACTION_STOP); PendingIntent stopPendingIntent = PendingIntent.getBroadcast(mService, INTENT_STOP, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action stopActon = new NotificationCompat.Action.Builder( R.drawable.ic_stop_black_48dp, "Stop", stopPendingIntent).build(); // Next song action Intent nextIntent = new Intent(BackgroundService.ACTION_NEXT); PendingIntent nextPendingIntent = PendingIntent.getBroadcast(mService, INTENT_NEXT, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder( R.drawable.ic_skip_next_48dp, "Next", nextPendingIntent).build(); // Quit action Intent quitIntent = new Intent(BackgroundService.ACTION_QUIT_BACKGROUND_SERVICE); PendingIntent quitPendingIntent = PendingIntent.getBroadcast(mService, INTENT_QUIT, quitIntent, PendingIntent.FLAG_UPDATE_CURRENT); mNotificationBuilder.setDeleteIntent(quitPendingIntent); mNotificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); mNotificationBuilder.setSmallIcon(R.drawable.ic_notification_24dp); mNotificationBuilder.addAction(prevAction); mNotificationBuilder.addAction(playPauseAction); mNotificationBuilder.addAction(stopActon); mNotificationBuilder.addAction(nextAction); NotificationCompat.MediaStyle notificationStyle = new NotificationCompat.MediaStyle(); notificationStyle.setShowActionsInCompactView(1, 2); mNotificationBuilder.setStyle(notificationStyle); String title; if (track.getTrackTitle().isEmpty()) { title = FormatHelper.getFilenameFromPath(track.getPath()); } else { title = track.getTrackTitle(); } mNotificationBuilder.setContentTitle(title); String secondRow; if (!track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) { secondRow = track.getTrackArtist() + mService.getString(R.string.track_item_separator) + track.getTrackAlbum(); } else if (track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) { secondRow = track.getTrackAlbum(); } else if (track.getTrackAlbum().isEmpty() && !track.getTrackArtist().isEmpty()) { secondRow = track.getTrackArtist(); } else { secondRow = track.getPath(); } // Set the media session metadata updateMetadata(track, state); mNotificationBuilder.setContentText(secondRow); // Remove unnecessary time info mNotificationBuilder.setWhen(0); // Cover but only if changed if (mNotification == null || !track.getTrackAlbum().equals(mLastTrack.getTrackAlbum())) { mLastTrack = track; mLastBitmap = null; mCoverLoader.getImage(mLastTrack, true); } // Only set image if an saved one is available if (mLastBitmap != null) { mNotificationBuilder.setLargeIcon(mLastBitmap); } else { /** * Create a dummy placeholder image for versions greater android 7 because it * does not automatically show the application icon anymore in mediastyle notifications. */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Drawable icon = mService.getDrawable(R.drawable.notification_placeholder_256dp); Bitmap iconBitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(iconBitmap); DrawFilter filter = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG, 1); canvas.setDrawFilter(filter); icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); icon.setFilterBitmap(true); icon.draw(canvas); mNotificationBuilder.setLargeIcon(iconBitmap); } else { /** * For older android versions set the null icon which will result in a dummy icon * generated from the application icon. */ mNotificationBuilder.setLargeIcon(null); } } // Build the notification mNotification = mNotificationBuilder.build(); // Send the notification away mNotificationManager.notify(NOTIFICATION_ID, mNotification); } }
From source file:net.gsantner.opoc.util.ContextUtils.java
/** * Get a {@link Bitmap} out of a {@link Drawable} *///from w ww. ja v a 2s .co m public Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof VectorDrawableCompat || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } 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); } else if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } return bitmap; }
From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java
private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) { Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // Create a canvas, that will draw on to canvasBitmap. Canvas imageCanvas = new Canvas(canvasBitmap); // Draw the image to our canvas backgroundImage.draw(imageCanvas);/*from ww w .j a v a 2 s .com*/ // Set up the paint for use with our Canvas TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG); textPaint.setTextAlign(TextPaint.Align.CENTER); textPaint.setTypeface(Typeface.DEFAULT); textPaint.setTextSize(100f); textPaint.setColor(context.getResources().getColor(android.R.color.white)); int xPos = (imageCanvas.getWidth() / 2); int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)); Rect r = new Rect(); textPaint.getTextBounds(text, 0, text.length(), r); // yPos += (Math.abs(r.height()))/2; // Draw the text on top of our image imageCanvas.drawText(text, xPos, yPos, textPaint); // Combine background and text to a LayerDrawable LayerDrawable layerDrawable = new LayerDrawable( new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) }); Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); layerDrawable.setBounds(0, 0, width, height); layerDrawable.draw(new Canvas(newBitmap)); return newBitmap; }