MoonUtil.java 15.5 KB
Newer Older
1
package com.ydl.ydlcommon.utils;
konghaorui committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

19 20 21
import com.ydl.ydlcommon.utils.emoji.EmojiManager;
import com.ydl.ydlcommon.utils.emoji.ImageSpanAlignCenter;
import com.ydl.ydlcommon.R;
konghaorui committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MoonUtil {
//	private static final float DEF_SCALE = 0.6f;
	private static final float DEF_SCALE = 0.55f;
	private static final float SMALL_SCALE = 0.45F;
	public static void identifyFaceExpression(Context context,
			View textView, String value,int align ) {
		identifyFaceExpression(context, textView, value, align, DEF_SCALE);
	}

	/**
	 * 动态正文SetText
	 * @param context
	 * @param textView
	 * @param value 正文
	 * @param topic 话题
	 * @param allContent 是否有全文的标示
	 * @param align
	 */
	public static void SetTrendContent(Context context,TextView textView, String value,String topic,String allContent,int align){
//		viewSetText(textView, mSpannableString);
		SpannableString spantopic = new SpannableString(topic);
konghaorui committed
48
		spantopic.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_main_theme)), 0, topic.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
49 50 51 52 53
		textView.setText(spantopic);
		SpannableString mSpannableString = makeSpannableStringTags(context, value, DEF_SCALE, align);
		textView.append(mSpannableString);
		if(!"".equals(allContent)&&allContent!=null){
			SpannableString spanallContent = new SpannableString(allContent);
konghaorui committed
54
			spanallContent.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_main_theme)), 0, allContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
			textView.append(spanallContent);
		}
//		textView.setMovementMethod(LinkMovementMethod.getInstance());
	}

	/**
	 * 动态列表评论 SetText
	 * @param context
	 * @param textView
	 * @param value 评论内容
	 * @param username 发布评论的人
	 * @param align
	 */
	public static void SetDicuss(Context context,TextView textView,String value,String username,String reply_username,int align){
//		LogUtil.d("username: "+username+ " reply username: "+reply_username);
		if(!"".equals(reply_username)&&reply_username!=null){
			SpannableString spantuser = new SpannableString(username);
konghaorui committed
72
			spantuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_default_text_color)), 0, username.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
73 74 75 76
			textView.setText(spantuser);
			textView.append("回复");
			String reply_user_name=reply_username+":";
			SpannableString spantreplyuser = new SpannableString(reply_user_name);
konghaorui committed
77
			spantreplyuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_default_text_color)), 0, reply_user_name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
78 79 80 81 82 83
			textView.append(spantreplyuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}else {
			String usernamed=username+":";
			SpannableString spantuser = new SpannableString(usernamed);
konghaorui committed
84
			spantuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_default_text_color)), 0, usernamed.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
85 86 87 88 89 90 91 92 93 94 95 96 97
			textView.setText(spantuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}

	}


	public static void SetItemDicussContent(Context context,TextView textView,String value,String username,String reply_username,int align){
		if(!"".equals(reply_username)&&reply_username!=null){
			textView.setText("回复");
			String reply_user_name=reply_username+":";
			SpannableString spantreplyuser = new SpannableString(reply_user_name);
konghaorui committed
98
			spantreplyuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_main_theme)), 0, reply_user_name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
99 100 101 102 103 104
			textView.append(spantreplyuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}else {
			String usernamed=username+":";
			SpannableString spantuser = new SpannableString(usernamed);
konghaorui committed
105
			spantuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.platform_main_theme)), 0, usernamed.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
konghaorui committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
			textView.setText(spantuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}

	}

	/**
	 * 动态列表评论 SetText
	 * @param context
	 * @param textView
	 * @param value 评论内容
	 * @param username 发布评论的人
	 * @param textColor 替换文字颜色
	 * @param align
	 */
	public static void SetDicuss(Context context,TextView textView,String value,String username,int textColor,String reply_username,int align){
		if(!"".equals(reply_username)&&reply_username!=null){
			SpannableString spantuser = new SpannableString(username);
			spantuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(textColor)), 0, username.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(spantuser);
			textView.append("回复");
			String reply_user_name=reply_username+":";
			SpannableString spantreplyuser = new SpannableString(reply_user_name);
			spantreplyuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(textColor)), 0, reply_user_name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.append(spantreplyuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}else {
			String usernamed=username+":";
			SpannableString spantuser = new SpannableString(usernamed);
			spantuser.setSpan(new ForegroundColorSpan(context.getResources().getColor(textColor)), 0, usernamed.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(spantuser);
			SpannableString mSpannableString = makeSpannableStringTags2(context, value, 0.45f, align);
			textView.append(mSpannableString);
		}

	}

	public static void identifyFaceExpressionAndATags(Context context,
			View textView, String value,int align){
		SpannableString mSpannableString = makeSpannableStringTags(context, value, DEF_SCALE, align);
		viewSetText(textView, mSpannableString);
	}
	
	/** 具体类型的view设置内容
	 * @param textView
	 * @param mSpannableString
	 */
	private static void viewSetText(View textView, SpannableString mSpannableString) {
		if(textView instanceof TextView) {
			TextView tv=(TextView) textView;
			tv.setText(mSpannableString);
		} else if(textView instanceof EditText) {
			EditText et=(EditText) textView;
			et.setText(mSpannableString);
		}
	}
	
	public static void identifyFaceExpression(Context context,
			View textView, String value,int align, float scale) {

		SpannableString mSpannableString = replaceEmoticons(context, value, scale, align);
		viewSetText(textView, mSpannableString);
	}
	/**
	 lstmsgviewholder类使用,只需显示a标签对应的文本
	 */
	public static void identifyFaceExpressionAndTags(Context context,
			View textView, String value,int align, float scale) {
		SpannableString mSpannableString = makeSpannableStringTags(context, value, scale, align,false);
		viewSetText(textView, mSpannableString);
	}

//	private static SpannableString replaceEmoticons(final Context context, String value, float scale, int align) {
//		if(TextUtils.isEmpty(value)) {
//			value="";
//		}
//		SpannableString mSpannableString=null;
//		String[] sts=null;
//		if(value.contains("4444")){
//			sts=value.split("4444");
//		}
//		if(sts!=null){
//			mSpannableString= new SpannableString(sts[0]+sts[1]);
//			int start = mSpannableString.length() - sts[1].length() ;  //超链接起始位置
//			int end = mSpannableString.length();   //超链接结束位置
//			final String url=sts[2];
//			//可以为多部分设置超链接
//			mSpannableString.setSpan(new Clickable(new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					DeliveryListener.getL().showHelp(url);
//				}
//			}), start, end, Spanned.SPAN_MARK_MARK);
//		}
//		if(mSpannableString==null){
//			mSpannableString=new SpannableString(value);
//		}
//
//		Matcher matcher = EmojiManager.getPattern().matcher(value);
//		while (matcher.find()) {
//			int start = matcher.start();
//			int end = matcher.end();
//			String emot = value.substring(start, end);
//			Drawable d = getEmotDrawable(context, emot, scale);
//			if (d != null) {
//				ImageSpan span = new ImageSpan(d, align);
//				mSpannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//			}
//		}
//		return mSpannableString;
//	}

     //2017/5/4 云信DEMOv3.6.0
	private static SpannableString replaceEmoticons(Context context, String value, float scale, int align) {
		if(TextUtils.isEmpty(value)) {
			value="";
		}

		SpannableString mSpannableString = new SpannableString(value);
		Matcher matcher = EmojiManager.getPattern().matcher(value);
		while (matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			String emot = value.substring(start, end);
			Drawable d = getEmotDrawable(context, emot, scale);
			if (d != null) {
				ImageSpan span = new ImageSpan(d, align);
				mSpannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}
		return mSpannableString;
	}


	private static Pattern mATagPattern =  Pattern.compile("<a.*?>.*?</a>");
	public static SpannableString makeSpannableStringTags(Context context, String value,float scale,int align) {
		return makeSpannableStringTags(context, value, DEF_SCALE, align, true);
	}
	public static SpannableString makeSpannableStringTags2(Context context, String value,float scale,int align) {
		return makeSpannableStringTags(context, value, scale, align, true);
	}

//	public static SpannableString makeSpannableStringTags(Context context, String value,float scale,int align,boolean bTagClickable) {
//		ArrayList<ATagSpan> tagSpans = new ArrayList<ATagSpan>();
//		if(TextUtils.isEmpty(value)){
//			value="";
//		}
//		//a标签需要替换原始文本,放在moonutil类中
//		Matcher aTagMatcher = mATagPattern.matcher(value);
//
//		int start =0;
//		int end = 0;
//		while (aTagMatcher.find()) {
//			 start = aTagMatcher.start();
//			 end = aTagMatcher.end();
//			String atagString = value.substring(start, end);
//			ATagSpan tagSpan = getTagSpan(atagString);
//			value = value.substring(0,start)+tagSpan.getTag()+value.substring(end);
//			tagSpan.setRange(start, start+tagSpan.getTag().length());
//			tagSpans.add(tagSpan);
//			aTagMatcher= mATagPattern.matcher(value);
//		}
//
//
//		SpannableString mSpannableString = new SpannableString(value);
//		Matcher matcher = EmojiManager.getPattern().matcher(value);
//		while (matcher.find()) {
//			start = matcher.start();
//			end = matcher.end();
//			String emot = value.substring(start, end);
//			Drawable d = getEmotDrawable(context, emot, scale);
//			if (d != null) {
//				ImageSpan span = new ImageSpan(d, align);
//				mSpannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//			}
//		}
//
//		if(bTagClickable){
//			for(ATagSpan tagSpan:tagSpans){
//				mSpannableString.setSpan(tagSpan, tagSpan.start, tagSpan.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//			}
//		}
//
//		return mSpannableString;
//	}


	//: 2017/5/4 云信DEMOv3.6.0
	public static SpannableString makeSpannableStringTags(Context context, String value,float scale,int align,boolean bTagClickable) {
		ArrayList<ATagSpan> tagSpans = new ArrayList<ATagSpan>();
		if(TextUtils.isEmpty(value)){
			value="";
		}
		//a标签需要替换原始文本,放在moonutil类中
		Matcher aTagMatcher = mATagPattern.matcher(value);

		int start =0;
		int end = 0;
		while (aTagMatcher.find()) {
			start = aTagMatcher.start();
			end = aTagMatcher.end();
			String atagString = value.substring(start, end);
			ATagSpan tagSpan = getTagSpan(atagString);
			value = value.substring(0,start)+tagSpan.getTag()+value.substring(end);
			tagSpan.setRange(start, start+tagSpan.getTag().length());
			tagSpans.add(tagSpan);
			aTagMatcher= mATagPattern.matcher(value);
		}


		SpannableString mSpannableString = new SpannableString(value);
		Matcher matcher = EmojiManager.getPattern().matcher(value);
		while (matcher.find()) {
			start = matcher.start();
			end = matcher.end();
			String emot = value.substring(start, end);
			Drawable d = getEmotDrawable(context, emot, scale);
			if (d != null) {
				ImageSpan span = align == -1 ? new ImageSpanAlignCenter(d) : new ImageSpan(d, align);
				mSpannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}

		if(bTagClickable){
			for(ATagSpan tagSpan:tagSpans){
				mSpannableString.setSpan(tagSpan, tagSpan.start, tagSpan.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}

		return mSpannableString;
	}
	
	public static void replaceEmoticons(Context context, Editable editable, int start, int count) {
		if (count <= 0 || editable.length() < start + count)
			return;

		CharSequence s = editable.subSequence(start, start + count);
		Matcher matcher = EmojiManager.getPattern().matcher(s);
		while (matcher.find()) {
			int from = start + matcher.start();
			int to = start + matcher.end();
			String emot = editable.subSequence(from, to).toString();
			Drawable d = getEmotDrawable(context, emot, SMALL_SCALE);
			if (d != null) {
				ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
				editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}
	}

	private static Drawable getEmotDrawable(Context context, String text, float scale) {
		Drawable drawable = EmojiManager.getDrawable(context, text);

		// scale
		if (drawable != null) {
			int width = (int) (drawable.getIntrinsicWidth() * scale);
			int height = (int) (drawable.getIntrinsicHeight() * scale);
			drawable.setBounds(0, 0, width, height);
		}
		
		return drawable;
	}
	
	private static ATagSpan getTagSpan(String text){
		String href = null;
		String tag = null;
		if(text.toLowerCase().contains("href")){
			int start = text.indexOf("\"");
			int end = text.indexOf("\"",start+1);
			if(end>start)
				href =text.substring(start+1, end);
		}
		int start = text.indexOf(">");
		int end = text.indexOf("<", start);
		if(end>start)
			tag = text.substring(start+1,end);
		return new ATagSpan(tag,href);
		
	}

	private static class ATagSpan extends ClickableSpan{
		private int start;
		private int end;
		private String mUrl;
		private String tag;
		ATagSpan(String tag, String url) {
			this.tag = tag;
			this.mUrl = url;
		}
		@Override
		public void updateDrawState(TextPaint ds) {
			super.updateDrawState(ds);
			ds.setUnderlineText(true);
		}
		public String getTag(){
			return tag;
		}
		
		public void setRange(int start,int end){
			this.start = start;
			this.end = end;
		}
		
		@Override
		public void onClick(View widget) {
			try {
                if (TextUtils.isEmpty(mUrl))
                    return;
                Uri uri = Uri.parse(mUrl);
                String scheme = uri.getScheme();
                if (TextUtils.isEmpty(scheme)) {
                    mUrl = "http://" + mUrl;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
		}
	}

}