The realization of the Android text stroke function _android

Source: Internet
Author: User

Here also want to simply say, these small modules are not my original, but also looked at the data found, because time is longer, the original link has been forgotten, so here is not listed reference links. However, the code I have modified, improved, but also added a number of comments, I hope to help.

Text strokes This function is very practical, if it is a single background display text, text strokes can also play a decorative role. If it is a complex background, especially in different picture background display text, because the text color is very easy and the picture background similar, so that the text can not see clearly, the user experience is poor. If the text after different color strokes, the text contour part of a color, the text inside another color, because in general, the picture is either similar to the text contour color, or similar to the color inside the text, so no matter how complex the picture background, text will be the overall display.

The method I use here is to rewrite the TextView way.

The following is the relevant code, the overall relatively simple, very easy to understand.

The inherited TextView text stroke classes are as follows:

public class Stroketextview extends TextView {private TextView outlinetextview = null;
    
    Public Stroketextview {Super (context);
    Outlinetextview = new TextView (context);
  Init ();
    
    Public Stroketextview (context, AttributeSet attrs) {Super (context, attrs);
    Outlinetextview = new TextView (context, attrs);
  Init ();
    
    Public Stroketextview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
    Outlinetextview = new TextView (context, attrs, Defstyle);
  Init ();
    public void init () {Textpaint paint = Outlinetextview.getpaint ();
    Paint.setstrokewidth (3);//Stroke width paint.setstyle (style.stroke);
  Outlinetextview.settextcolor (Color.parsecolor ("#45c01a"));//Stroke Color outlinetextview.setgravity (getgravity ());
    @Override public void Setlayoutparams (Viewgroup.layoutparams params) {super.setlayoutparams (params); Outlinetextview.setlaYoutparams (params); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasu
   
    Respec, Heightmeasurespec);
    Set outline text Charsequence Outlinetext = Outlinetextview.gettext ();
      if (Outlinetext = null | |!outlinetext.equals (THIS.GETTEXT ())) {Outlinetextview.settext (GetText ());
    Postinvalidate ();
  } outlinetextview.measure (Widthmeasurespec, Heightmeasurespec); @Override protected void OnLayout (Boolean changed, int left, int. top, int right, int bottom) {Super.onlayo
    UT (changed, left, top, right, bottom);
  Outlinetextview.layout (left, top, right, bottom);
    } @Override protected void OnDraw (Canvas Canvas) {Outlinetextview.draw (Canvas);
  Super.ondraw (canvas); }
}

The layout files are as follows:

<com.my.teststroketextview.stroketextview
    android:id= "@+id/test_stroketextview"
    android:layout_ Width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:layout_marginleft= "10DP"
    Android : layout_marginright= "10DP"
    android:textsize= "25sp"
    android:textcolor= "@color/dark_gray"
    android: text= "@string/hello_world"/>

The calling code is as follows:

Private Stroketextview Test_stroketextview = null;
 
@Override
protected void onCreate (Bundle savedinstancestate)
{
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 
 Test_stroketextview = (Stroketextview) Findviewbyid (R.id.test_stroketextview);
 Test_stroketextview.settext ("Hello world!");
}

If you want to change the text stroke width, or stroke color, you need to modify the above StrokeTextView class, of course, you can also design the class more flexible, so you can dynamically modify the stroke width or stroke color.

The above is an example of the implementation of the text stroke in Android, I hope this article will help you learn about Android development. Please support the cloud-dwelling community a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.