android TextView Two Methods of Setting and Cancelling Strike Line

  • 2021-08-28 21:02:32
  • OfStack

1. TextView There are two ways to set a strikethrough:

(Recommended) Mode 1:

The original Flags property of TextView and the strikethrough 1 block are set by the bitwise OR operator. TextView is redrawn in setPaintFlags.


tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Mode 2:

After getting the brush, set the properties and redraw TextView. There is a problem with this method, which will replace the original Flags attribute of TextView, such as anti-aliasing. If you look closely, you will find that in this way, the text has jagged.


tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
tv.invalidate();

2. TextView has two ways to cancel strikethrough:

(Recommended) Mode 1:

First reverse the Paint.STRIKE_THRU_TEXT_FLAG property, then use the bitwise AND operator & Removing the strikethrough attribute and preserving the original Flags attribute of TextView. TextView is redrawn in setPaintFlags.


tv.setPaintFlags(tv.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

Mode 2:

After getting the brush, clear the Flags property and redraw TextView. There is a problem with this method, which will empty all the original Flags attributes of TextView, such as anti-aliasing. If you look closely, you will find that in this way, the text has jagged;


tv.getPaint().setFlags(0);
tv.invalidate();

Related articles: