How to put text in a drawable?

后端 未结 5 1948
一生所求
一生所求 2020-11-30 01:47

I\'m trying to create a drawable on the fly to use as a background for a custom linearlayout. It needs to have hash marks and such (no big deal), but also have numbers label

相关标签:
5条回答
  • 2020-11-30 02:12

    This allows you to put any View in a Drawable, including a TextView. You can even use styles in your XML layout.

    public class ViewDrawable extends Drawable {
        final View mView;
    
        public ViewDrawable(final Context context, final @LayoutRes int layoutId) {
            this(LayoutInflater.from(context).inflate(layoutId, null));
        }
    
        public ViewDrawable(final @NonNull View view) {
            mView = view;
        }
    
        public View getView() {
            return mView;
        }
    
        @Override
        public void setBounds(int left, int top, int right, int bottom) {
            super.setBounds(left, top, right, bottom);
            final int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY);
            final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY);
            mView.measure(widthMeasureSpec, heightMeasureSpec);
            mView.layout(left, top, right, bottom);
        }
    
        @Override
        public void draw(@NonNull Canvas canvas) {
            mView.draw(canvas);
        }
    
        @Override
        public void setAlpha(int alpha) {
            mView.setAlpha(alpha/255f);
        }
    
        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {
    
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.UNKNOWN;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:15

    Looking at Plowman's answer and trying to adjust it to my needs I stumpled upon a Class that is used for Camera in this link

    Here is the code from the TextDrawable Class. Looks pretty simillar with Plowmans but for me works better:

    import android.content.res.Resources;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.ColorFilter;
    import android.graphics.Paint;
    import android.graphics.Paint.Align;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.util.TypedValue;
    
    public class TextDrawable extends Drawable {
        private static final int DEFAULT_COLOR = Color.WHITE;
        private static final int DEFAULT_TEXTSIZE = 15;
        private Paint mPaint;
        private CharSequence mText;
        private int mIntrinsicWidth;
        private int mIntrinsicHeight;
    
        public TextDrawable(Resources res, CharSequence text) {
            mText = text;
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(DEFAULT_COLOR);
            mPaint.setTextAlign(Align.CENTER);
            float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                    DEFAULT_TEXTSIZE, res.getDisplayMetrics());
            mPaint.setTextSize(textSize);
            mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
            mIntrinsicHeight = mPaint.getFontMetricsInt(null);
        }
        @Override
        public void draw(Canvas canvas) {
            Rect bounds = getBounds();
            canvas.drawText(mText, 0, mText.length(),
                    bounds.centerX(), bounds.centerY(), mPaint);
        }
        @Override
        public int getOpacity() {
            return mPaint.getAlpha();
        }
        @Override
        public int getIntrinsicWidth() {
            return mIntrinsicWidth;
        }
        @Override
        public int getIntrinsicHeight() {
            return mIntrinsicHeight;
        }
        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }
        @Override
        public void setColorFilter(ColorFilter filter) {
            mPaint.setColorFilter(filter);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:18

    To answer the comments above regarding how to center the text:

    mPaint.textAlign = Align.CENTER
    ...
    // Centering for mixed case letters
    canvas.drawText(mText, 0, mText.length,
            bounds.centerX().toFloat(), bounds.centerY().toFloat() - ((mPaint.descent() + mPaint.ascent()) / 2), mPaint)
    
    // Centering for all uppercase letters
    canvas.drawText(mText, 0, mText.length,
                bounds.centerX().toFloat(), bounds.centerY().toFloat() - mPaint.ascent() / 2, mPaint)
    
    0 讨论(0)
  • 2020-11-30 02:38

    I've read the book "Professional Android 2 Application Development" (by Reto Meier). Amongst others, it contains an example project where you create a simple compass application where you "draw" text, markers etc.

    The brief explanation is that you create a class that extends the android.view.View class and overrides the onDraw(Canvas) method.

    All the source code form the book is available for download here: http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html. If you download the code and look inside the project named "Chapter 4 Compass", I believe you would find what you're looking for :)

    0 讨论(0)
  • 2020-11-30 02:38

    Here is a brief example of a TextDrawable which works like a normal drawable but lets you specify text as the only constructor variable:

    public class TextDrawable extends Drawable {
    
        private final String text;
        private final Paint paint;
    
        public TextDrawable(String text) {
    
            this.text = text;
    
            this.paint = new Paint();
            paint.setColor(Color.WHITE);
            paint.setTextSize(22f);
            paint.setAntiAlias(true);
            paint.setFakeBoldText(true);
            paint.setShadowLayer(6f, 0, 0, Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextAlign(Paint.Align.LEFT);
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawText(text, 0, 0, paint);
        }
    
        @Override
        public void setAlpha(int alpha) {
            paint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            paint.setColorFilter(cf);
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }
    
    0 讨论(0)
提交回复
热议问题