Outline TextView



  • Is it possible to make such a leash? TextView? If so, where do we start?

    Example:

    введите сюда описание изображения



  • Seeing the article https://habrahabr.ru/post/150871/ ♪ I redo it for ♪ TextView

    OutlineTextView.java:

    package com.example.outlinetextview;
    

    import android.content.Context;
    import android.content.res.ColorStateList;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint.Join;
    import android.graphics.Paint.Style;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.widget.TextView;

    import com.example.outlinetextview.R;

    public class OutlineTextView extends TextView {
    private int strokeColor= Color.TRANSPARENT;
    private int strokeWidth=2;

    public OutlineTextView(Context context) {
        super(context);
    }
    
    public OutlineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
        strokeColor = a.getColor(R.styleable.OutlineTextView_textStrokeColor, strokeColor);
        strokeWidth = a.getDimensionPixelSize(R.styleable.OutlineTextView_textStrokeWidth, strokeWidth);
        a.recycle();
    }
    
    public void onDraw(Canvas canvas) {
        final ColorStateList textColor = getTextColors();
        TextPaint paint = this.getPaint();
        paint.setStyle(Style.STROKE);
        paint.setStrokeJoin(Join.ROUND);
        paint.setStrokeMiter(10);
        this.setTextColor(strokeColor);
        paint.setStrokeWidth(strokeWidth);
        super.onDraw(canvas);
        paint.setStyle(Style.FILL);
        setTextColor(textColor);
        super.onDraw(canvas);
    }
    

    }

    attrs:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    ....
    <declare-styleable name="OutlineTextView">
    <attr name="textStrokeColor" format="color"/>
    <attr name="textStrokeWidth" format="dimension"/>
    </declare-styleable>
    ....
    </resources>

    example.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@null"
    android:gravity="center"
    android:orientation="vertical">

    &lt;com.example.outlinetextview.OutlineTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:gravity="center"
        android:textSize="15sp"
        android:textColor="#2196F3"
        app:textStrokeWidth="5sp"
        app:textStrokeColor="#FFFFFF"/&gt;
    

    </LinearLayout>

    Result:

    введите сюда описание изображения

    P.S. I hope someone needs to:




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2