List of usage examples for android.graphics Canvas drawText
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
From source file:com.mobisci_lab.virtualkeyboard.softkeyboard.LatinKeyboardView.java
@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); TextPaint paint = new TextPaint(); paint.setAntiAlias(true);//from w w w. j a v a 2s.c o m paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); paint.setTextSize(11 * getResources().getDisplayMetrics().density); paint.setColor(ContextCompat.getColor(this.getContext(), R.color.candidate_other)); List<Key> keys = getKeyboard().getKeys(); for (Key key : keys) { float x = key.x + key.width / 2 + key.width / 5; float y = key.y + key.width / 2; String text = ""; switch (key.codes[0]) { case 113: text = "1"; break; case 119: text = "2"; break; case 101: text = "3"; break; case 114: text = "4"; break; case 116: text = "5"; break; case 121: text = "6"; break; case 117: text = "7"; break; case 105: text = "8"; break; case 111: text = "9"; break; case 112: text = "0"; break; } canvas.drawText(text, x, y, paint); } }
From source file:jp.gr.java_conf.piropiroping.bluetoothcommander.MainActivity.java
private void convertBitmapCommand(String command, int start, int stop) { //???//from w w w . jav a 2 s . co m Paint objPaint = new Paint(); Bitmap objBitmap; Canvas objCanvas; int textSize = 40; int textWidth = textSize * command.length(), textHeight = textSize; objPaint.setAntiAlias(true); objPaint.setColor(Color.BLUE); objPaint.setTextSize(textSize); Paint.FontMetrics fm = objPaint.getFontMetrics(); objPaint.getTextBounds(command, 0, command.length(), new Rect(0, 0, textWidth, textHeight)); //? textWidth = (int) objPaint.measureText(command); textHeight = (int) (Math.abs(fm.top) + fm.bottom); objBitmap = Bitmap.createBitmap(textWidth, textHeight, Bitmap.Config.ARGB_8888); //??? objCanvas = new Canvas(objBitmap); objCanvas.drawText(command, 0, Math.abs(fm.top), objPaint); Editable editable = mOutEditText.getText(); SpannableStringBuilder ssb = (SpannableStringBuilder) editable; ImageSpan is = new ImageSpan(getApplicationContext(), objBitmap); ssb.setSpan(is, start, stop + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
From source file:com.todddavies.components.progressbar.ProgressWheel.java
protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Draw the inner circle canvas.drawArc(circleBounds, 360, 360, false, circlePaint); //Draw the rim canvas.drawArc(circleBounds, 360, 360, false, rimPaint); canvas.drawArc(circleOuterContour, 360, 360, false, contourPaint); canvas.drawArc(circleInnerContour, 360, 360, false, contourPaint); //Draw the bar if (isSpinning) { canvas.drawArc(circleBounds, m_Degree - 90, barLength, false, barPaint); } else {/* ww w. j a v a 2 s . c o m*/ canvas.drawArc(circleBounds, -90, m_Degree, false, barPaint); } //Draw the text (attempts to center it horizontally and vertically) float textHeight = textPaint.descent() - textPaint.ascent(); float verticalTextOffset = (textHeight / 2) - textPaint.descent(); for (String s : splitText) { float horizontalTextOffset = textPaint.measureText(s) / 2; canvas.drawText(s, this.getWidth() / 2 - horizontalTextOffset, this.getHeight() / 2 + verticalTextOffset, textPaint); } }
From source file:com.codetroopers.shakemytours.ui.activity.TripActivity.java
private Bitmap createMarker(int radius, int strokeWidth, String letter, int strokeColor, int backgroundColor) { int width = (radius * 2) + (strokeWidth * 2); Bitmap marker = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(marker); Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); strokePaint.setColor(strokeColor);/*from w ww . ja va 2 s . c o m*/ strokePaint.setShadowLayer(strokeWidth, 1.0f, 1.0f, Color.BLACK); canvas.drawCircle(width / 2, width / 2, radius, strokePaint); Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(backgroundColor); canvas.drawCircle(width / 2, width / 2, radius - strokeWidth, backgroundPaint); if (letter != null) { Rect result = new Rect(); Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.map_marker_text_size)); textPaint.setColor(strokeColor); textPaint.getTextBounds(letter, 0, letter.length(), result); int yOffset = result.height() / 2; canvas.drawText(letter, width / 2, (width / 2) + yOffset, textPaint); } return marker; }
From source file:com.cssweb.android.view.PriceMini.java
public void drawIndex(Canvas canvas) { //canvas.restore(); Paint paint = this.mPaint; paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true);/*from w w w . j ava 2s. c o m*/ if (quoteData != null) { try { JSONArray jArr = quoteData.getJSONArray("data"); JSONObject jo = jArr.getJSONObject(0); double zrsp = jo.getDouble("zrsp"); double zjcj = jo.getDouble("zjcj"); paint.setTextSize(mTextSize * 2); setColor(paint, zjcj, zrsp); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(DX * 0.5f, DY * 2f); canvas.drawText(Utils.dataFormation(zjcj, stockdigit), x, y, paint); paint.setStyle(Paint.Style.STROKE); paint.setTextSize(mTextSize); double zhangd = jo.getDouble("zd"); if (zhangd < 0) { paint.setColor(GlobalColor.colorPriceDown); } else if (zhangd > 0) { paint.setColor(GlobalColor.colorpriceUp); } else { paint.setColor(GlobalColor.colorPriceEqual); } canvas.translate(0, DY * 0.8f); String zhangdie = Utils.dataFormation(zhangd, stockdigit); if (zhangdie.equals("-")) canvas.drawText("", x, y, paint); else canvas.drawText(zhangdie, x, y, paint); double zhangf = jo.getDouble("zf"); if (zhangf < 0) { paint.setColor(GlobalColor.colorPriceDown); } else if (zhangf > 0) { paint.setColor(GlobalColor.colorpriceUp); } else { paint.setColor(GlobalColor.colorPriceEqual); } canvas.translate(DX * 3.5f, 0); String zhangfu = Utils.dataFormation(zhangf * 100, stockdigit); if (zhangfu.equals("-")) canvas.drawText("", x, y, paint); else canvas.drawText(zhangfu + "%", x, y, paint); paint.setColor(GlobalColor.colorLabelName); canvas.translate(-DX * 3.5f, DY * 1.2f); //canvas.translate(-DX*0.5f, DY*2f); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); paint.setColor(GlobalColor.colorLabelName); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(width / 2 - DX, -DY * 7); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); paint.setTextAlign(Paint.Align.RIGHT); paint.setColor(GlobalColor.colorLabelName); canvas.translate(0, 0); canvas.drawText(Utils.dataFormation(zrsp, stockdigit), x, y, paint); canvas.translate(width / 2, -DY * 3); paint.setTextAlign(Paint.Align.RIGHT); double jrkp = jo.getDouble("jrkp"); setColor(paint, jrkp, zrsp); canvas.drawText(Utils.dataFormation(jrkp, stockdigit), x, y, paint); canvas.translate(0, DY); double zg = jo.getDouble("zgcj"); setColor(paint, zg, zrsp); canvas.drawText(Utils.dataFormation(zg, stockdigit), x, y, paint); canvas.translate(0, DY); double zd = jo.getDouble("zdcj"); setColor(paint, zd, zrsp); canvas.drawText(Utils.dataFormation(zd, stockdigit), x, y, paint); paint.setColor(GlobalColor.colorStockName); canvas.translate(0, DY); double amp = jo.getDouble("amp"); if (amp == 0) canvas.drawText("", x, y, paint); else canvas.drawText(Utils.dataFormation(jo.getDouble("amp") * 100, 1) + "%", x, y, paint); paint.setColor(GlobalColor.colorStockName); canvas.translate(0, DY); canvas.drawText(Utils.getAmountFormat(jo.getDouble("cjsl"), true), x, y, paint); canvas.translate(0, DY); canvas.drawText(Utils.getAmountFormat(jo.getDouble("cjje"), true), x, y, paint); canvas.translate(0, DY); paint.setColor(GlobalColor.colorpriceUp); canvas.drawText(String.valueOf(jo.getInt("zj")), x, y, paint); canvas.translate(0, DY); paint.setColor(GlobalColor.colorPriceDown); canvas.drawText(String.valueOf(jo.getInt("dj")), x, y, paint); } catch (JSONException e) { Log.e(TAG, e.toString()); } } else {//????? paint.setTextAlign(Paint.Align.LEFT); paint.setColor(GlobalColor.colorLabelName); canvas.translate(DX, DY * 2f); canvas.drawText("-", x, y, paint); paint.setStyle(Paint.Style.STROKE); paint.setTextSize(mTextSize); canvas.translate(-DX / 2, DY * 0.8f); canvas.drawText("-", x, y, paint); canvas.translate(DX * 2.5f, 0); canvas.drawText("-", x, y, paint); canvas.translate(-DX * 2.5f, DY * 1.2f); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(width / 2 - DX, -DY * 7); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); } }
From source file:com.viewpagerindicator.RichTitlePageIndicator.java
private void drawItem(Canvas canvas, int pos, Rect bound, Paint paint, boolean drawIcon) { String title = getTitle(pos); Bitmap icon = getIcon(pos);//from w w w. ja va 2 s. c o m // TODO: more flexible int textWidth = (int) paint.measureText(title); int iconWidth = 0, iconHeight = 0; if (icon != null) { iconWidth = icon.getWidth(); iconHeight = icon.getHeight(); } int offset = 0; if (drawIcon) { int boundH = bound.bottom - bound.top; float t = iconHeight; canvas.drawBitmap(icon, bound.left, mTopPadding, paint); offset = iconWidth; } canvas.drawText(title, bound.left + offset, bound.bottom + mTopPadding, paint); }
From source file:com.github.jokar.rxupload.widget.ProgressDownloadView.java
/** * MARK: Overrides//w ww .j a v a 2 s. c o m */ @Override protected void onDraw(Canvas canvas) { if (mPathWhite != null && mPathBlack != null) { float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f)); float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY(); switch (mState) { case STATE_WORKING: // Save and restore prevent the rest of the canvas to not be rotated canvas.save(); float speed = (getProgress() - mTarget) / 20; mBubbleAngle += speed * 10; if (mBubbleAngle > 20) { mBubbleAngle = 20; } if (mBubbleAngle < -20) { mBubbleAngle = -20; } if (Math.abs(speed) < 1) { mSpeedAngle -= mBubbleAngle / 20; mSpeedAngle *= .9f; } mBubbleAngle += mSpeedAngle; canvas.rotate(mBubbleAngle, bubbleAnchorX, bubbleAnchorY); canvas.drawPath(mPathBubble, mPaintBubble); canvas.drawText(String.valueOf((int) mProgress) + " %", textX, textY, mPaintText); canvas.restore(); break; case STATE_FAILED: canvas.save(); canvas.rotate(mFailAngle, bubbleAnchorX, bubbleAnchorY); canvas.drawPath(mPathBubble, mPaintBubble); canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7); // mPaintText.setColor(getResources().getColor(R.color.red_wine)); textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f)); canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText); canvas.restore(); break; case STATE_SUCCESS: canvas.save(); // mPaintText.setColor(getResources().getColor(R.color.green_grass)); textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f)); Matrix flipMatrix = new Matrix(); flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY); canvas.concat(flipMatrix); canvas.drawPath(mPathBubble, mPaintBubble); canvas.concat(flipMatrix); canvas.drawText(getResources().getString(R.string.done), textX, textY, mPaintText); canvas.restore(); break; } canvas.drawPath(mPathBlack, mPaintBlack); canvas.drawPath(mPathWhite, mPaintWhite); } }
From source file:de.tlabs.ssr.g1.client.SourcesView.java
private void recalculateSizeScale() { float sizeInMeters; double upperBoundingPowOfTen; double scaleLengthInMeters; float scaleLengthInPixels; float scaleStartInPixels = 20.0f; // get size of short display edge in meters sizeInMeters = inverseViewportTransformation .mapRadius(Math.min(getWidth(), getHeight()) - scaleStartInPixels * 2.0f); // find upper bounding power of ten (0.01, 0.1, 1, 10, 100, etc.) if (sizeInMeters > 1.0) { upperBoundingPowOfTen = 10.0;// w w w . ja va 2s . c o m while (true) { if (sizeInMeters / upperBoundingPowOfTen > 1.0f) { upperBoundingPowOfTen *= 10.0; } else { break; } } } else { upperBoundingPowOfTen = 1.0; while (true) { if (sizeInMeters / upperBoundingPowOfTen >= 0.1f) { break; } else { upperBoundingPowOfTen *= 0.1; } } } // map to subdivisions scaleLengthInMeters = sizeInMeters / upperBoundingPowOfTen; if (scaleLengthInMeters > 0.75) scaleLengthInMeters = 0.75; else if (scaleLengthInMeters > 0.5) scaleLengthInMeters = 0.5; else if (scaleLengthInMeters > 0.3) scaleLengthInMeters = 0.3; else if (scaleLengthInMeters > 0.2) scaleLengthInMeters = 0.2; else if (scaleLengthInMeters > 0.15) scaleLengthInMeters = 0.15; else scaleLengthInMeters = 0.1; scaleLengthInMeters *= upperBoundingPowOfTen; // get corresponding scale length in pixels scaleLengthInPixels = viewportTransformation.mapRadius((float) scaleLengthInMeters); // generate picture Canvas canvas = sizeScalePicture.beginRecording(0, 0); float y = getHeight() - 5.0f; float xHalf = scaleStartInPixels + scaleLengthInPixels / 2.0f; float scaleEndInPixels = scaleStartInPixels + scaleLengthInPixels; SourcesView.paint.setARGB(255, 255, 255, 255); SourcesView.paint.setAntiAlias(true); canvas.drawLine(scaleStartInPixels - 1.0f, y, scaleEndInPixels + 1.0f, y, SourcesView.paint); canvas.drawLine(scaleStartInPixels, y, scaleStartInPixels, y - 5.0f, SourcesView.paint); canvas.drawLine(xHalf, y, xHalf, y - 5.0f, SourcesView.paint); canvas.drawLine(scaleEndInPixels, y, scaleEndInPixels, y - 5.0f, SourcesView.paint); canvas.drawText("0m", scaleStartInPixels, y - 8.0f, SourcesView.paint); canvas.drawText(String.valueOf((float) scaleLengthInMeters / 2.0f) + "m", xHalf, y - 8.0f, SourcesView.paint); canvas.drawText(String.valueOf((float) scaleLengthInMeters) + "m", scaleEndInPixels, y - 8.0f, SourcesView.paint); }
From source file:com.cssweb.android.view.PriceMini.java
public void drawQihuo(Canvas canvas) { //canvas.restore(); Paint paint = this.mPaint; paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true);//from w ww . ja va2 s. c o m if (quoteData != null) { try { JSONArray jArr = quoteData.getJSONArray("data"); JSONObject jo = jArr.getJSONObject(0); String str = ""; double zrsp = jo.getDouble("zrsp"); double zjcj = jo.getDouble("zjcj"); paint.setTextSize(mTextSize * 2); setColor(paint, zjcj, zrsp); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(DX, DY * 2f); canvas.drawText(Utils.dataFormation(zjcj, stockdigit), x, y, paint); paint.setStyle(Paint.Style.STROKE); paint.setTextSize(mTextSize); double zhangd = jo.getDouble("zd"); if (zhangd < 0) { paint.setColor(GlobalColor.colorPriceDown); } else if (zhangd > 0) { paint.setColor(GlobalColor.colorpriceUp); } else { paint.setColor(GlobalColor.colorPriceEqual); } canvas.translate(-DX / 2, DY * 0.8f); String zhangdie = Utils.dataFormation(zhangd, stockdigit, jo.getInt("tp")); if (zhangdie.equals("-")) canvas.drawText("", x, y, paint); else canvas.drawText(zhangdie, x, y, paint); double zhangf = jo.getDouble("zf"); if (zhangf < 0) { paint.setColor(GlobalColor.colorPriceDown); } else if (zhangf > 0) { paint.setColor(GlobalColor.colorpriceUp); } else { paint.setColor(GlobalColor.colorPriceEqual); } canvas.translate(DX * 2.5f, 0); String zhangfu = Utils.dataFormation(zhangf * 100, 1); if (zhangfu.equals("-") || zhangfu.equals("")) canvas.drawText("", x, y, paint); else canvas.drawText(zhangfu + "%", x, y, paint); paint.setColor(GlobalColor.colorLabelName); canvas.translate(-DX * 3, DY * 1.2f); canvas.drawText("?", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText(" ", x, y, paint); canvas.translate(width / 2, -DY * 2); paint.setTextAlign(Paint.Align.RIGHT); double temp2 = jo.getDouble("sjw1"); setColor(paint, temp2, zrsp); str = Utils.dataFormation(temp2, stockdigit, jo.getInt("tp")); canvas.drawText(str, x - tips, y, paint); canvas.translate(0, DY); temp2 = jo.getDouble("bjw1"); setColor(paint, temp2, zrsp); str = Utils.dataFormation(temp2, stockdigit, jo.getInt("tp")); canvas.drawText(str, x - tips, y, paint); paint.setColor(GlobalColor.colorStockName); canvas.translate(0, DY); canvas.drawText(Utils.dataFormation(jo.getDouble("jrkc"), 0, jo.getInt("tp")), x - tips, y, paint); paint.setColor(GlobalColor.colorLabelName); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(0, -DY * 5); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("?", x, y, paint); canvas.translate(0, DY); canvas.drawText(" ", x, y, paint); paint.setTextAlign(Paint.Align.RIGHT); canvas.translate(width / 2, -DY * 5); double jrkp = jo.getDouble("jrkp"); setColor(paint, jrkp, zrsp); canvas.drawText(Utils.dataFormation(jrkp, stockdigit, jo.getInt("tp")), x - tips, y, paint); canvas.translate(0, DY); double zg = jo.getDouble("zgcj"); setColor(paint, zg, zrsp); canvas.drawText(Utils.dataFormation(zg, stockdigit, jo.getInt("tp")), x - tips, y, paint); canvas.translate(0, DY); double zd = jo.getDouble("zdcj"); setColor(paint, zd, zrsp); canvas.drawText(Utils.dataFormation(zd, stockdigit, jo.getInt("tp")), x - tips, y, paint); canvas.translate(0, DY); paint.setColor(GlobalColor.colorStockName); canvas.drawText(Utils.getAmountFormat(jo.getInt("ssl1"), false), x - tips, y, paint); canvas.translate(0, DY); canvas.drawText(Utils.getAmountFormat(jo.getInt("bsl1"), false), x - tips, y, paint); paint.setColor(GlobalColor.colorStockName); canvas.translate(0, DY); canvas.drawText(Utils.dataFormation(jo.getDouble("jrpc"), 0, jo.getInt("tp")), x - tips, y, paint); } catch (JSONException e) { Log.e(TAG, e.toString()); } } else {//????? paint.setTextAlign(Paint.Align.LEFT); paint.setColor(GlobalColor.colorLabelName); canvas.translate(DX, DY * 2f); canvas.drawText("-", x, y, paint); paint.setStyle(Paint.Style.STROKE); paint.setTextSize(mTextSize); canvas.translate(-DX / 2, DY * 0.8f); canvas.drawText("-", x, y, paint); canvas.translate(DX * 2.5f, 0); canvas.drawText("-", x, y, paint); canvas.translate(-DX * 3, DY * 1.2f); canvas.drawText("?", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText(" ", x, y, paint); paint.setTextAlign(Paint.Align.LEFT); canvas.translate(width / 2, -DY * 5); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("", x, y, paint); canvas.translate(0, DY); canvas.drawText("??", x, y, paint); canvas.translate(0, DY); canvas.drawText("?", x, y, paint); canvas.translate(0, DY); canvas.drawText(" ", x, y, paint); } }
From source file:im.neon.util.NotificationUtils.java
/** * Build a message notification./* www . j a v a 2 s. com*/ * @param context the context * @param from the sender * @param matrixId the user account id; * @param displayMatrixId true to display the matrix id * @param largeIcon the notification icon * @param unseenNotifiedRoomsCount the number of notified rooms * @param body the message body * @param roomId the room id * @param roomName the room name * @param shouldPlaySound true when the notification as sound. * @param isInvitationEvent true if it is an invitation notification * @return the notification */ public static Notification buildMessageNotification(Context context, String from, String matrixId, boolean displayMatrixId, Bitmap largeIcon, int unseenNotifiedRoomsCount, String body, String roomId, String roomName, boolean shouldPlaySound, boolean isInvitationEvent) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setWhen(System.currentTimeMillis()); if (!TextUtils.isEmpty(from)) { // don't display the room name for 1:1 room notifications. if (!TextUtils.isEmpty(roomName) && !roomName.equals(from)) { builder.setContentTitle(from + " (" + roomName + ")"); } else { builder.setContentTitle(from); } } else { builder.setContentTitle(roomName); } builder.setContentText(body); builder.setAutoCancel(true); builder.setSmallIcon(R.drawable.message_notification_transparent); if (null != largeIcon) { largeIcon = createSquareBitmap(largeIcon); // add a bubble in the top right if (0 != unseenNotifiedRoomsCount) { try { android.graphics.Bitmap.Config bitmapConfig = largeIcon.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } // setLargeIcon must used a 64 * 64 pixels bitmap // rescale to have the same text UI. float densityScale = context.getResources().getDisplayMetrics().density; int side = (int) (64 * densityScale); Bitmap bitmapCopy = Bitmap.createBitmap(side, side, bitmapConfig); Canvas canvas = new Canvas(bitmapCopy); // resize the bitmap to fill in size int bitmapWidth = largeIcon.getWidth(); int bitmapHeight = largeIcon.getHeight(); float scale = Math.min((float) canvas.getWidth() / (float) bitmapWidth, (float) canvas.getHeight() / (float) bitmapHeight); int scaledWidth = (int) (bitmapWidth * scale); int scaledHeight = (int) (bitmapHeight * scale); Bitmap rescaledBitmap = Bitmap.createScaledBitmap(largeIcon, scaledWidth, scaledHeight, true); canvas.drawBitmap(rescaledBitmap, (side - scaledWidth) / 2, (side - scaledHeight) / 2, null); String text = "" + unseenNotifiedRoomsCount; // prepare the text drawing Paint textPaint = new Paint(); textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); textPaint.setColor(Color.WHITE); textPaint.setTextSize(10 * densityScale); // get its size Rect textBounds = new Rect(); if (-1 == mUnreadBubbleWidth) { textPaint.getTextBounds("99", 0, 2, textBounds); mUnreadBubbleWidth = textBounds.width(); } textPaint.getTextBounds(text, 0, text.length(), textBounds); // draw a red circle int radius = mUnreadBubbleWidth; Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); canvas.drawCircle(canvas.getWidth() - radius, radius, radius, paint); // draw the text canvas.drawText(text, canvas.getWidth() - textBounds.width() - (radius - (textBounds.width() / 2)), -textBounds.top + (radius - (-textBounds.top / 2)), textPaint); // get the new bitmap largeIcon = bitmapCopy; } catch (Exception e) { Log.e(LOG_TAG, "## buildMessageNotification(): Exception Msg=" + e.getMessage()); } } builder.setLargeIcon(largeIcon); } String name = ": "; if (!TextUtils.isEmpty(roomName)) { name = " (" + roomName + "): "; } if (displayMatrixId) { from = "[" + matrixId + "]\n" + from; } builder.setTicker(from + name + body); TaskStackBuilder stackBuilder; Intent intent; intent = new Intent(context, VectorRoomActivity.class); intent.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomId); if (null != matrixId) { intent.putExtra(VectorRoomActivity.EXTRA_MATRIX_ID, matrixId); } stackBuilder = TaskStackBuilder.create(context).addParentStack(VectorRoomActivity.class) .addNextIntent(intent); // android 4.3 issue // use a generator for the private requestCode. // When using 0, the intent is not created/launched when the user taps on the notification. // PendingIntent pendingIntent = stackBuilder.getPendingIntent((new Random()).nextInt(1000), PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); // display the message with more than 1 lines when the device supports it NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle(); textStyle.bigText(from + ":" + body); builder.setStyle(textStyle); // do not offer to quick respond if the user did not dismiss the previous one if (!LockScreenActivity.isDisplayingALockScreenActivity()) { if (!isInvitationEvent) { // offer to type a quick answer (i.e. without launching the application) Intent quickReplyIntent = new Intent(context, LockScreenActivity.class); quickReplyIntent.putExtra(LockScreenActivity.EXTRA_ROOM_ID, roomId); quickReplyIntent.putExtra(LockScreenActivity.EXTRA_SENDER_NAME, from); quickReplyIntent.putExtra(LockScreenActivity.EXTRA_MESSAGE_BODY, body); if (null != matrixId) { quickReplyIntent.putExtra(LockScreenActivity.EXTRA_MATRIX_ID, matrixId); } // the action must be unique else the parameters are ignored quickReplyIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis()))); PendingIntent pIntent = PendingIntent.getActivity(context, 0, quickReplyIntent, 0); builder.addAction(R.drawable.vector_notification_quick_reply, context.getString(R.string.action_quick_reply), pIntent); } else { { // offer to type a quick reject button Intent leaveIntent = new Intent(context, JoinScreenActivity.class); leaveIntent.putExtra(JoinScreenActivity.EXTRA_ROOM_ID, roomId); leaveIntent.putExtra(JoinScreenActivity.EXTRA_MATRIX_ID, matrixId); leaveIntent.putExtra(JoinScreenActivity.EXTRA_REJECT, true); // the action must be unique else the parameters are ignored leaveIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis()))); PendingIntent pIntent = PendingIntent.getActivity(context, 0, leaveIntent, 0); builder.addAction(R.drawable.vector_notification_reject_invitation, context.getString(R.string.reject), pIntent); } { // offer to type a quick accept button Intent acceptIntent = new Intent(context, JoinScreenActivity.class); acceptIntent.putExtra(JoinScreenActivity.EXTRA_ROOM_ID, roomId); acceptIntent.putExtra(JoinScreenActivity.EXTRA_MATRIX_ID, matrixId); acceptIntent.putExtra(JoinScreenActivity.EXTRA_JOIN, true); // the action must be unique else the parameters are ignored acceptIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis()))); PendingIntent pIntent = PendingIntent.getActivity(context, 0, acceptIntent, 0); builder.addAction(R.drawable.vector_notification_accept_invitation, context.getString(R.string.join), pIntent); } } // Build the pending intent for when the notification is clicked Intent roomIntentTap; if (isInvitationEvent) { // for invitation the room preview must be displayed roomIntentTap = CommonActivityUtils.buildIntentPreviewRoom(matrixId, roomId, context, VectorFakeRoomPreviewActivity.class); } else { roomIntentTap = new Intent(context, VectorRoomActivity.class); roomIntentTap.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomId); } // the action must be unique else the parameters are ignored roomIntentTap.setAction(TAP_TO_VIEW_ACTION + ((int) (System.currentTimeMillis()))); // Recreate the back stack TaskStackBuilder stackBuilderTap = TaskStackBuilder.create(context) .addParentStack(VectorRoomActivity.class).addNextIntent(roomIntentTap); builder.addAction(R.drawable.vector_notification_open, context.getString(R.string.action_open), stackBuilderTap.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)); } //extendForCar(context, builder, roomId, roomName, from, body); Notification n = builder.build(); n.flags |= Notification.FLAG_SHOW_LIGHTS; n.defaults |= Notification.DEFAULT_LIGHTS; if (shouldPlaySound) { n.defaults |= Notification.DEFAULT_SOUND; } if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // some devices crash if this field is not set // even if it is deprecated // setLatestEventInfo() is deprecated on Android M, so we try to use // reflection at runtime, to avoid compiler error: "Cannot resolve method.." try { Method deprecatedMethod = n.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class); deprecatedMethod.invoke(n, context, from, body, pendingIntent); } catch (Exception ex) { Log.e(LOG_TAG, "## buildMessageNotification(): Exception - setLatestEventInfo() Msg=" + ex.getMessage()); } } return n; }