public class TextDrawable extends Drawable {protected final Paint textPaint;protected colorStateList color;protected String text;protected int iHeight;protected int iWIDth;protected int measureDWIDth,measuredHeight;private float ascent;/** * A flag whether the drawable is stateful - whether to redraw if the state of vIEw has changed */protected boolean stateful;/** * Vertical alignment of text */private VerticalAlignment verticalAlignment;.... some constructors...public TextDrawable(Context ctx,String text,colorStateList color,float textSize,VerticalAlignment verticalAlignment) { textPaint = new Paint(); this.text = text; initPaint(); this.textPaint.setTextSize(textSize); measureSize(); setBounds(0,iWIDth,iHeight); this.color = color; textPaint.setcolor(color.getDefaultcolor()); this.verticalAlignment = verticalAlignment;}/** * Set bounds of drawable to start on coordinate [0,0] and end on coordinate[measureDWIDth,* measuredHeight] */public final voID setBoundsByMeasuredSize() { setBounds(0,measureDWIDth,measuredHeight); invalIDateSelf();}@OverrIDepublic boolean isstateful() { return stateful;}public voID setStateful(boolean stateful) { this.stateful = stateful;}private voID initPaint() { textPaint.setAntiAlias(true); textPaint.setTextAlign(Paint.Align.CENTER);}/** * Vertical alignment of text within the drawable (Horizontally it is always aligned to center */public VerticalAlignment getVerticalAlignment() { return verticalAlignment;}/** * Vertical alignment of text within the drawable (Horizontally it is always aligned to center */public voID setVerticalAlignment(VerticalAlignment verticalAlignment) { if (this.verticalAlignment != verticalAlignment) { this.verticalAlignment = verticalAlignment; invalIDateSelf(); }}/** * displayed text */public String getText() { return text;}/** * displayed text */public voID setText(String text) { if (this.text == null || !this.text.equals(text)) { this.text = text; invalIDateSelf(); }}/** * The color of text */public colorStateList getcolor() { return color;}/** * The color of text */public voID setcolor(colorStateList colorStateList) { if (this.color == null || !this.color.equals(colorStateList)) { this.color = colorStateList; invalIDateSelf(); }}/** * The color of text */public voID setcolor(int color) { setcolor(colorStateList.valueOf(color));}/** * Text size */public voID setTextSize(float size) { if (this.textPaint.getTextSize() != size) { this.textPaint.setTextSize(size); measureSize(); invalIDateSelf(); }}/** * Text size */public voID setTextSize(int unit,float size,Context context) { setTextSize(TypedValue.applyDimension(unit,size,context.getResources().getdisplayMetrics()));}/** * This method is called by default when any property that may have some influence on the size * of drawable This method should use measureDWIDth and measuredHeight propertIEs to store the * measured walues By default the measureDWIDth and measuredHeight are set to iWIDth and iHeight * (size of text) by this method. */protected voID measureSize() { ascent = -textPaint.ascent(); iWIDth = (int) (0.5f + textPaint.measureText(text)); iHeight = (int) (0.5f + textPaint.descent() + ascent); measureDWIDth = iWIDth; measuredHeight = iHeight;}public float getTextSize() { return textPaint.getTextSize();}@OverrIDeprotected boolean onStateChange(int[] state) { int clr = color != null ? color.getcolorForState(state,0) : 0; if (textPaint.getcolor() != clr) { textPaint.setcolor(clr); return true; } else { return false; }}public Typeface getTypeface() { return textPaint.getTypeface();}public voID setTypeface(Typeface typeface) { if (!textPaint.getTypeface().equals(typeface)) { textPaint.setTypeface(typeface); invalIDateSelf(); }}/** * The method is called before the text is drawn. This method can be overrIDden to draw some background (by default this method does nothing). * @param canvas The canvas where to draw. * @param bounds The bounds of the drawable. */protected voID drawBefore(Canvas canvas,Rect bounds) {}/** * The method is called after the text is drawn. This method can be overrIDen to draw some more graphics over the text (by default this method does nothing). * @param canvas The canvas where to draw. * @param bounds The bound of the drawable. */protected voID drawAfter(Canvas canvas,Rect bounds) {}@OverrIDepublic voID draw(Canvas canvas) { if (text == null || text.isEmpty()) { return; } final Rect bounds = getBounds(); int stack = canvas.save(); canvas.translate(bounds.left,bounds.top); drawBefore(canvas,bounds); if (text != null && !text.isEmpty()) { final float x = bounds.wIDth() >= iWIDth ? bounds.centerX() : iWIDth * 0.5f; float y = 0; switch (verticalAlignment) { case BASEliNE: y = (bounds.height() - iHeight) * 0.5f + ascent; break; case top: y = bounds.height(); break; case BottOM: y = bounds.height(); break; } canvas.drawText(text,x,y,textPaint); } drawAfter(canvas,bounds); canvas.restoretoCount(stack);}@OverrIDepublic voID setAlpha(int Alpha) { if (textPaint.getAlpha() != Alpha) { textPaint.setAlpha(Alpha); invalIDateSelf(); }}@OverrIDepublic voID setcolorFilter(colorFilter cf) { if (textPaint.getcolorFilter() == null || !textPaint.getcolorFilter().equals(cf)) { textPaint.setcolorFilter(cf); invalIDateSelf(); }}@OverrIDepublic int getopacity() { return PixelFormat.TRANSLUCENT;}public enum VerticalAlignment { top,BottOM,BASEliNE}
以及如何使用它:
fab.setimageDrawable(new TextDrawable(fab.getContext(),"FAB",colorStateList.valueOf(color.BLACK),32.f,VerticalAlignment.BASEliNE));
(fab是floatingActionbutton)
@H_419_30@ 总结以上是内存溢出为你收集整理的android – 如何将文本添加到浮动 *** 作按钮?全部内容,希望文章能够帮你解决android – 如何将文本添加到浮动 *** 作按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)