List of usage examples for android.graphics.drawable Drawable getIntrinsicWidth
public int getIntrinsicWidth()
From source file:com.gelakinetic.mtgfam.helpers.ImageGetterHelper.java
/** * This function returns a custom ImageGetter which replaces glyphs with text. * This could have been a new subclass of ImageGetter with a constructor that takes a resource, but I guess * I didn't feel like writing it that way on that day. * * @param context the context to get resources to get drawables from * @return a custom ImageGetter/*ww w. j av a2 s . c o m*/ */ public static ImageGetter GlyphGetter(final Context context) { return new ImageGetter() { public Drawable getDrawable(String source) { Drawable d = null; source = source.replace("/", ""); if (source.equalsIgnoreCase("w")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_w);//ContextCompat.getDrawable(context, R.drawable.glyph_w, null); } else if (source.equalsIgnoreCase("u")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_u); } else if (source.equalsIgnoreCase("b")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_b); } else if (source.equalsIgnoreCase("r")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_r); } else if (source.equalsIgnoreCase("g")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_g); } else if (source.equalsIgnoreCase("t")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_tap); } else if (source.equalsIgnoreCase("q")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_untap); } else if (source.equalsIgnoreCase("wu") || source.equalsIgnoreCase("uw")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_wu); } else if (source.equalsIgnoreCase("ub") || source.equalsIgnoreCase("bu")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_ub); } else if (source.equalsIgnoreCase("br") || source.equalsIgnoreCase("rb")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_br); } else if (source.equalsIgnoreCase("rg") || source.equalsIgnoreCase("gr")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_rg); } else if (source.equalsIgnoreCase("gw") || source.equalsIgnoreCase("wg")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_gw); } else if (source.equalsIgnoreCase("wb") || source.equalsIgnoreCase("bw")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_wb); } else if (source.equalsIgnoreCase("bg") || source.equalsIgnoreCase("gb")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_bg); } else if (source.equalsIgnoreCase("gu") || source.equalsIgnoreCase("ug")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_gu); } else if (source.equalsIgnoreCase("ur") || source.equalsIgnoreCase("ru")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_ur); } else if (source.equalsIgnoreCase("rw") || source.equalsIgnoreCase("wr")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_rw); } else if (source.equalsIgnoreCase("2w") || source.equalsIgnoreCase("w2")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_w2); } else if (source.equalsIgnoreCase("2u") || source.equalsIgnoreCase("u2")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_u2); } else if (source.equalsIgnoreCase("2b") || source.equalsIgnoreCase("b2")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_b2); } else if (source.equalsIgnoreCase("2r") || source.equalsIgnoreCase("r2")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_r2); } else if (source.equalsIgnoreCase("2g") || source.equalsIgnoreCase("g2")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_g2); } else if (source.equalsIgnoreCase("s")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_s); } else if (source.equalsIgnoreCase("pw") || source.equalsIgnoreCase("wp")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_pw); } else if (source.equalsIgnoreCase("pu") || source.equalsIgnoreCase("up")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_pu); } else if (source.equalsIgnoreCase("pb") || source.equalsIgnoreCase("bp")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_pb); } else if (source.equalsIgnoreCase("pr") || source.equalsIgnoreCase("rp")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_pr); } else if (source.equalsIgnoreCase("pg") || source.equalsIgnoreCase("gp")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_pg); } else if (source.equalsIgnoreCase("p")) { d = ContextCompat.getDrawable(context, getResourceIdFromAttr(context.getTheme(), R.attr.glyph_p)); } else if (source.equalsIgnoreCase("+oo")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_inf); } else if (source.equalsIgnoreCase("100")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_100); } else if (source.equalsIgnoreCase("1000000")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_1000000); } else if (source.equalsIgnoreCase("hr") || source.equalsIgnoreCase("rh")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_hr); } else if (source.equalsIgnoreCase("hw") || source.equalsIgnoreCase("wh")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_hw); } else if (source.equalsIgnoreCase("c")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_c); } else if (source.equalsIgnoreCase("chaos")) { d = ContextCompat.getDrawable(context, getResourceIdFromAttr(context.getTheme(), R.attr.glyph_chaos)); } else if (source.equalsIgnoreCase("z")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_z); } else if (source.equalsIgnoreCase("y")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_y); } else if (source.equalsIgnoreCase("x")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_x); } else if (source.equalsIgnoreCase("h")) { d = ContextCompat.getDrawable(context, R.drawable.glyph_half); } else if (source.equalsIgnoreCase("pwk")) { d = ContextCompat.getDrawable(context, getResourceIdFromAttr(context.getTheme(), R.attr.glyph_pwk)); } else if (source.equalsIgnoreCase("e")) { d = ContextCompat.getDrawable(context, getResourceIdFromAttr(context.getTheme(), R.attr.glyph_e)); } else { for (int i = 0; i < drawableNumbers.length; i++) { if (source.equals(Integer.valueOf(i).toString())) { d = ContextCompat.getDrawable(context, drawableNumbers[i]); } } } if (d == null) { return null; } d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; }
From source file:radialdemo.RadialMenuWidget.java
@Override protected void onDraw(Canvas c) { Paint paint = new Paint(); paint.setAntiAlias(true);/*from w ww. j ava 2 s . c om*/ paint.setStrokeWidth(3); // draws a dot at the source of the press if (showSource == true) { paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawCircle(xSource, ySource, cRadius / 10, paint); paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xSource, ySource, cRadius / 10, paint); } //inner for (int i = 0; i < Wedges.length; i++) { RadialMenuWedge f = Wedges[i]; paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawPath(f, paint); if (f == enabled && Wedge2Shown == true) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f != enabled && Wedge2Shown == true) { paint.setColor(disabledColor); paint.setAlpha(disabledAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f == enabled && Wedge2Shown == false) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f == selected) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else { paint.setColor(defaultColor); paint.setAlpha(defaultAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } //button content Rect rf = iconRect[i]; if ((menuEntries.get(i).getIcon() != 0) && (menuEntries.get(i).getLabel() != null)) { // This will look for a "new line" and split into multiple lines String menuItemName = menuEntries.get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); if (f != enabled && Wedge2Shown == true) { paint.setAlpha(disabledAlpha); } else { paint.setAlpha(textAlpha); } paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } Rect rf2 = new Rect(); rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2)); float textBottom = rf2.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } // Puts in the Icon Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon()); drawable.setBounds(rf2); if (f != enabled && Wedge2Shown == true) { drawable.setAlpha(disabledAlpha); } else { drawable.setAlpha(pictureAlpha); } drawable.draw(c); // Icon Only } else if (menuEntries.get(i).getIcon() != 0) { // Puts in the Icon Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon()); drawable.setBounds(rf); if (f != enabled && Wedge2Shown == true) { drawable.setAlpha(disabledAlpha); } else { drawable.setAlpha(pictureAlpha); } drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); if (f != enabled && Wedge2Shown == true) { paint.setAlpha(disabledAlpha); } else { paint.setAlpha(textAlpha); } paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize + 10); // This will look for a "new line" and split into multiple lines String menuItemName = menuEntries.get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = rf.centerY() - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } // Animate the outer ring in/out if (animateOuterIn == true) { animateOuterWedges(ANIMATE_IN); } else if (animateOuterOut == true) { animateOuterWedges(ANIMATE_OUT); } //outer if (Wedge2Shown == true) { for (int i = 0; i < Wedges2.length; i++) { RadialMenuWedge f = Wedges2[i]; paint.setColor(outlineColor); paint.setAlpha(20); paint.setStyle(Paint.Style.STROKE); c.drawPath(f, paint); if (f == selected2) { paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } Rect rf = iconRect2[i]; if ((wedge2Data.getChildren().get(i).getIcon() != 0) && (wedge2Data.getChildren().get(i).getLabel() != null)) { // This will look for a "new line" and split into multiple // lines String menuItemName = wedge2Data.getChildren().get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(animateTextSize); Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } Rect rf2 = new Rect(); rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2)); float textBottom = rf2.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } // Puts in the Icon Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon()); drawable.setBounds(rf2); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Icon Only } else if (wedge2Data.getChildren().get(i).getIcon() != 0) { // Puts in the Icon Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon()); drawable.setBounds(rf); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(animateTextSize); // This will look for a "new line" and split into multiple // lines String menuItemName = wedge2Data.getChildren().get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = rf.centerY() - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } } //Check if the user has given input for centre circle if (centerCircle != null) { // Draws the Middle Circle paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawCircle(xPosition, yPosition, cRadius, paint); if (inCircle == true) { paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xPosition, yPosition, cRadius, paint); helper.onCloseAnimation(this, xPosition, yPosition, xSource, ySource); } else { paint.setColor(defaultColor); paint.setAlpha(defaultAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xPosition, yPosition, cRadius, paint); } // Draw the circle picture if ((centerCircle.getIcon() != 0) && (centerCircle.getLabel() != null)) { // This will look for a "new line" and split into multiple lines String menuItemName = centerCircle.getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); Rect rectText = new Rect(); Rect rectIcon = new Rect(); Drawable drawable = getResources().getDrawable(centerCircle.getIcon()); int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize); int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize); rectIcon.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText); textHeight = textHeight + (rectText.height() + 3); } rectIcon.set(rectIcon.left, rectIcon.top - ((int) textHeight / 2), rectIcon.right, rectIcon.bottom - ((int) textHeight / 2)); float textBottom = rectIcon.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText); float textLeft = xPosition - rectText.width() / 2; textBottom = textBottom + (rectText.height() + 3); c.drawText(stringArray[j], textLeft - rectText.left, textBottom - rectText.bottom, paint); } // Puts in the Icon drawable.setBounds(rectIcon); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Icon Only } else if (centerCircle.getIcon() != 0) { Rect rect = new Rect(); Drawable drawable = getResources().getDrawable(centerCircle.getIcon()); int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize); int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize); rect.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2); drawable.setBounds(rect); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); // This will look for a "new line" and split into multiple lines String menuItemName = centerCircle.getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = yPosition - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = xPosition - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } // Draws Text in TextBox if (headerString != null) { paint.setTextSize(headerTextSize); paint.getTextBounds(headerString, 0, headerString.length(), this.textRect); if (HeaderBoxBounded == false) { determineHeaderBox(); HeaderBoxBounded = true; } paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint); paint.setColor(headerBackgroundColor); paint.setAlpha(headerBackgroundAlpha); paint.setStyle(Paint.Style.FILL); c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint); paint.setColor(headerTextColor); paint.setAlpha(headerTextAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(headerTextSize); c.drawText(headerString, headerTextLeft, headerTextBottom, paint); } }