/** * Helper function to draw secondary text */ protected void drawMainText(Canvas canvas) { getPaint().setColor(getCurrentTextColor()); canvas.drawText(getPrimaryText(), 0, getPrimaryText().length(), mTextX, mTextY, getPaint()); }
/** * Helper method to size text */ protected void layoutText() { Paint paint = getPaint(); if (mTextSize != 0f) paint.setTextSize(mTextSize); float textWidth = paint.measureText(getText().toString()); float boxWidth = getWidth() - getPaddingLeft() - getPaddingRight(); float textSize = getTextSize(); if (textWidth > boxWidth){ paint.setTextSize(textSize * boxWidth / textWidth); mTextX = getPaddingLeft(); mTextSize = textSize; } else { mTextX = (getWidth() - textWidth) / 2; } mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2; if (mSecondaryPaint != null) mSecondaryPaint.setTextSize(mSecondaryTextSize); }
@Override protected void onDraw(Canvas canvas) { if (mSecondaryText != null){ //draw the text in the upper corner mSecondaryPaint.setColor(mSecondaryTextColor); mButtonHeight = getHeight(); // - getPaddingTop() - getPaddingBottom(); mButtonWidth = getWidth(); // - getPaddingLeft() - getPaddingRight(); mSecTextWidth = mSecondaryPaint.measureText(mSecondaryText); mSecAdditionalXOffset = getContext().getResources() .getDimensionPixelSize(R.dimen.button_ellipses_additional_offset_x); mSecTextHeight = mSecondaryPaint.getTextSize(); mSecAdditionalYOffset = getContext().getResources() .getDimensionPixelSize(R.dimen.button_ellipses_additional_offset_y); findSecondaryTextCoordinates(); canvas.drawText(mSecondaryText, 0, mSecondaryText.length(), mSecXCord, mSecYCord, mSecondaryPaint); } drawMainText(canvas); }
protected String getPrimaryText() { return getText().toString(); }
public SecondaryTextButton(Context context, AttributeSet attrs) { super(context, attrs); int secTextPct = SECONDARY_FONT_PERCENTAGE; //grab custom resource variable TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SecondaryTextButton, 0, 0); try { mSecondaryText = ta.getString(R.styleable.SecondaryTextButton_secondary_text); secTextPct = ta.getInteger(R.styleable.SecondaryTextButton_secondary_text_font_size_percentage, SECONDARY_FONT_PERCENTAGE); mSecondaryTextColor = ta.getColor(R.styleable.SecondaryTextButton_secondary_text_color, ContextCompat.getColor(context, R.color.button_secondary_text)); } finally { ta.recycle(); } mSecondaryTextSize = getPaint().getTextSize() * secTextPct / 100f; mSecondaryPaint = new Paint(getPaint()); }
/** * Overloaded method, will be called by onDraw in SecondaryTextButton */ @Override protected void drawMainText(Canvas canvas) { String text = getText().toString(); if (text.contains("/")){ getPaint().setColor(getCurrentTextColor()); if (mUnderline) getPaint().setUnderlineText(true); canvas.drawText(mTopText, 0, mTopText.length(), mTopTextX, mTopTextY, getPaint()); getPaint().setUnderlineText(false); canvas.drawText(mBotText, 0, mBotText.length(), mBotTextX, mBotTextY, getPaint()); if (text.contains(mArrowText)){ canvas.drawText(mArrowText, 0, mArrowText.length(), mArrowTextX, mArrowTextY, getPaint()); } } else super.drawMainText(canvas); }