简体   繁体   中英

Update size of drawable in Android

I have TextDrawable class. When i call setText , the text is change but size of drawable not change follow text size. I want size of drawable auto change same as TextView . Thanks

This is my code

public class TextDrawable extends Drawable {
    private Paint paint, strokePaint;
    private String text;
    private int instrinsicWidth, instrinsicHeight;
    private float strokeWidth;
    private Rect recBounds;


    public TextDrawable(String text){
        this.text = text;
        strokeWidth = 4f;

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setTextSize(32f);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);

        instrinsicWidth = (int)(paint.measureText(text, 0, text.length()) + 4);
        instrinsicHeight = paint.getFontMetricsInt(null) + 4;

        strokePaint = new Paint();
        strokePaint.setAntiAlias(true);
        strokePaint.setColor(Color.GRAY);
        strokePaint.setTextSize(32f);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(4f);
        strokePaint.setTextAlign(Paint.Align.CENTER);

        recBounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), recBounds);
    }

    public TextDrawable(String text, float textSize, int textColor, float strokeWidth, int strokeColor){
        this.text = text;
        this.strokeWidth = strokeWidth;

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);

        instrinsicWidth = (int)(paint.measureText(text, 0, text.length()) + strokeWidth);
        instrinsicHeight = (int)(paint.getFontMetricsInt(null) + strokeWidth);

        strokePaint = new Paint();
        strokePaint.setAntiAlias(true);
        strokePaint.setColor(strokeColor);
        strokePaint.setTextSize(textSize);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(strokeWidth);
        strokePaint.setTextAlign(Paint.Align.CENTER);

        recBounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), recBounds);

        //Log.d("htdu87", "rW-rH; w-h: " +recBounds.width()+"-"+recBounds.height()+";"+instrinsicWidth+"-"+instrinsicHeight);
    }

    public void setText(String text) {
        this.text = text;
        instrinsicWidth = (int)(paint.measureText(text, 0, text.length()) + strokeWidth);
        instrinsicHeight = (int)(paint.getFontMetricsInt(null) + strokeWidth);
        setBounds(0, 0, instrinsicWidth, instrinsicHeight);
        invalidateSelf();
    }

    @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        Log.d("htdu87", "bouns: "+getBounds());
        canvas.drawText(text, instrinsicWidth/2, instrinsicHeight-(instrinsicHeight-recBounds.height())-strokeWidth/2, strokePaint);
        canvas.drawText(text, instrinsicWidth/2, instrinsicHeight-(instrinsicHeight-recBounds.height())-strokeWidth/2, paint);
    }

    @Override
    public int getOpacity() {
        // TODO Auto-generated method stub
        return paint.getAlpha();
    }

    @Override
    public void setAlpha(int alpha) {
        // TODO Auto-generated method stub
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter filter) {
        // TODO Auto-generated method stub
        paint.setColorFilter(filter);
    }

    @Override
    public int getIntrinsicHeight() {
        // TODO Auto-generated method stub
        return instrinsicHeight;
    }

    @Override
    public int getIntrinsicWidth() {
        // TODO Auto-generated method stub
        return instrinsicWidth;
    }

}

Problem is that default View s are not designed to catch changes in Drawable 's size. So, you should assume that your Drawable is immutable and remove setText method at all - instead supply this parameter directly in constructor.

Basically, it leads to either recreating TextDrawable each time you want to change a text or reassigning it to respective View .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM