For android, if onDraw is rewritten to implement a control similar to TextView that can display expressions and links (1)
First look:
Write an object supported by the hyperconnection:
/** As the superjoin Display object */public class LinkInfo implements Comparable
{Private String content; private String type; private String id; private boolean bIsFace = false; private boolean bSelected = false; public static final String EMAIL = "Email "; public static final String WEBURL = "WebUrl"; public static final String PHONENUMBER = "PhoneNumber"; private int startIndex; private int endIndex ;}
Parse the following strings as follows:
String s = "(# Laugh) % $ % 3434343434343 $ % youjiancau@163.com $ dfsfsfsdffds ^ 15959224872) dfsfdsafsaf@153.cn & fefrewafrewfw.io (fsfsfsd@tre.com.cn ()()() www.baidu.com3242343243www.sohu.com @ afjiofjfjaof ";
Private static Pattern EMAIL_PATTERN = Patterns. EMAIL_ADDRESS; private static Pattern PHONE_PATTERN = Patterns. PHONE; private static Pattern WEBURL_PATTERN = Patterns. WEB_URL; public static ArrayList
ParseStr (String strLink) {if (TextUtils. isEmpty (strLink) {return null;} ArrayList
ResultList = new ArrayList
(); ArrayList
InfoList = null; try {infoList = new ArrayList
();
Matcher matcher = EMAIL_PATTERN.matcher (strLink); // locate the email in the stringInt begin = 0; while (matcher. find () {int start = matcher. start (); int end = matcher. end (); LinkInfo info = new LinkInfo (); info. setStartIndex (start); info. setEndIndex (end); info. setContent (matcher. group (); info. setType (LinkInfo. EMAIL); infoList. add (info );}
Matcher matcher1 = PHONE_PATTERN.matcher (strLink); // locate the number in the stringWhile (matcher1.find () {int start = matcher1.start (); int end = matcher1.end (); LinkInfo info = new LinkInfo (); info. setStartIndex (start); info. setEndIndex (end); info. setContent (matcher1.group (); info. setType (LinkInfo. PHONENUMBER); infoList. add (info) ;}// (# Laugh) Pattern pattern = Pattern. compile ("(\ (# \ S {1} \)"); Matcher matcher2 = pattern. matcher (strLink); while (matcher2.find () {int start = matcher2.start (); int end = matcher2.end (); System. out. println ("= start =" + start + "end =" + end + "match group =" + matcher2.group (); LinkInfo info = new LinkInfo (); info. setStartIndex (start); info. setEndIndex (end); info. setContent (matcher2.group (); info. setFace (true); infoList. add (info);} Collections. sort (infoList); int last = 0; for (int I = 0; I
Layout of activity:
IntroView has the following method to load a string:
Public class IntroView extends TextView {private ArrayList
TitleList; private int displayWidth = 0; private float displayHeight = 0; private float curLen = 0; private Bitmap starBmp; private Bitmap selectedBmp; private float posX = 0; private float posY = 0; private LinkInfo curInfo; // The currently clicked Link object private OnClickLinkListener Listener; private String mFaceType = MSG_FACE_TYPE; public static final String MSG_FACE_TYPE = "msgtype "; public static final String STATUS_FACE_TYPE = "statustype"; public IntroView (Context context) {super (context) ;}public IntroView (Context context, AttributeSet attrs) {super (context, attrs);} public IntroView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public void setTitleList (ArrayList
TitleList) {this. titleList = titleList; displayHeight = 0; requestLayout ();}}
Activity to call:
IntroView news_item_text = (IntroView) findViewById (R. id. news_item_text );
// \ N is not supported in strings
String s = "(# Laugh) % $ % 3434343434343 $ % youjiancau@163.com $ dfsfsfsdffds ^ 15959224872) dfsfdsafsaf@153.cn & fefrewafrewfw.io (fsfsfsd@tre.com.cn ()()() www.baidu.com3242343243www.sohu.com @ afjiofjfjaof ";
News_item_text.setTitleList (ParseNewsInfoUtil. parseStr (s ));
News_item_text.setOnClickLinkListener (this );The topic idea of IntroView is to obtain the ArrayList when measureWidth and measureHeight in onMeasure.
TitleList the location information of each LinkInfo and obtains the height and width of the IntroView,
In onDraw mode, each LinkInfo of titleList is drawn through loops.
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){super.onMeasure(widthMeasureSpec, heightMeasureSpec);try{int width = measureWidth(widthMeasureSpec);int height = measureHeight(heightMeasureSpec);setMeasuredDimension(width, height);}catch(Exception ex){ex.printStackTrace();}}
Private int measureWidth (int measureSpec) {int result = 0; int specMode = MeasureSpec. getMode (measureSpec); int specSize = MeasureSpec. getSize (measureSpec); int initialWidth = getPaddingLeft () + getPaddingRight (); int width = initialWidth; int maxWidth = 0; TextPaint tempPaint = null; if (specMode = MeasureSpec. EXACTLY) {result = specSize;} else {if (tempPaint = null) {tempPaint = new TextPa Int (); tempPaint. setStyle (Style. FILL); tempPaint. setAntiAlias (true); tempPaint. setTextSize (getTextSize ();} if (titleList! = Null & titleList. size ()> 0) {maxWidth = specSize; int size = titleList. size (); forLable: for (int I = 0; I <size; I ++) {LinkInfo info = titleList. get (I); if (info. isFace () {Bitmap faceBmp = null; if (mFaceType = MSG_FACE_TYPE) {faceBmp = MessageFaceModel. getInstance (). getFaceIcon (info. getContent ();} if (faceBmp! = Null) {int wSize = faceBmp. getWidth () + 4;If (width + wSize> = maxWidth){// Indicates that the calculated width is greater than the control width, and maxWidth = maxWidth; break forLable;} width + = wSize;} continue;} is returned ;} string text = info. getContent (); text = text. replaceAll ("\ n", ""); // replace this space if (! TextUtils. isEmpty (text) {float wSize = tempPaint. measureText (text); if (width + wSize> = maxWidth) {width = maxWidth; break forLable;} width + = wSize ;}}result = width;} displayWidth = result; return result ;}