diff --git a/.idea/modules.xml b/.idea/modules.xml index 8b9a797..1d2fdf8 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,8 +3,11 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/meitu/com/richtextviewproject/Library/process/ContentTextUtil.java b/app/src/main/java/meitu/com/richtextviewproject/Library/process/ContentTextUtil.java index 6777f94..530eace 100644 --- a/app/src/main/java/meitu/com/richtextviewproject/Library/process/ContentTextUtil.java +++ b/app/src/main/java/meitu/com/richtextviewproject/Library/process/ContentTextUtil.java @@ -33,80 +33,6 @@ public class ContentTextUtil { private static final String ALL = "(" + AT + ")" + "|" + "(" + TOPIC + ")" + "|" + "(" + URL + ")"; - public static SimplifySpanBuild getContent(String source, final Context context, TextView textView) { - SimplifySpanBuild mSpanBuild = new SimplifySpanBuild(); - int linkNorTextColor = Color.WHITE; - int linkPressBgColor = 0xFF87CEFA; - int linkNormalBgColor = 0xFF83B5ED; - //设置正则 - Pattern pattern = Pattern.compile(ALL); - Matcher matcher = pattern.matcher(source); - int maxEnd=0; - int preStart=0; - while (matcher.find()) { - final String at = matcher.group(1); - final String topic = matcher.group(2); - final String url = matcher.group(3); - - //处理@用户 - if (at != null) { - int start = matcher.start(1); - int end = start + at.length(); - maxEnd=Math.max(maxEnd,end); - if (start!=0&&preStart!=start){ - mSpanBuild.append(source.substring(preStart,start)); - preStart=end+1; - } - mSpanBuild.append(new SpecialTextUnit(at, linkNorTextColor).setClickableUnit(new SpecialClickableUnit(textView, new OnClickableSpanListener() { - @Override - public void onClick(TextView tv, String clickText) { - Toast.makeText(context, "Click Text: " + clickText, Toast.LENGTH_SHORT).show(); - } - }).setPressBgColor(Color.TRANSPARENT))); - } - //处理##话题 - if (topic != null) { - int start = matcher.start(2); - int end = start + topic.length(); - maxEnd=Math.max(maxEnd,end); - if (start!=0&&preStart!=start){ - mSpanBuild.append(source.substring(preStart,start)); - preStart=end+1; - } - mSpanBuild.append(new SpecialTextUnit(topic, linkNorTextColor).setClickableUnit(new SpecialClickableUnit(textView, new OnClickableSpanListener() { - @Override - public void onClick(TextView tv, String clickText) { - Toast.makeText(context, "Click Text: " + clickText, Toast.LENGTH_SHORT).show(); - } - }))); - - } - // 处理url地址 - if (url != null) { - int start = matcher.start(3); - int end = start + url.length(); - maxEnd=Math.max(maxEnd,end); - if (start!=0&&preStart!=start){ - mSpanBuild.append(source.substring(preStart,start)); - preStart=end+1; - } - mSpanBuild.appendMultiClickable(new SpecialClickableUnit(textView, new OnClickableSpanListener() { - @Override - public void onClick(TextView tv, String clickText) { - Toast.makeText(context, "Click Text: " + url, Toast.LENGTH_SHORT).show(); - } - }).setNormalTextColor(linkNorTextColor).setPressBgColor(linkPressBgColor).setNormalBgColor(linkNormalBgColor), - new SpecialImageUnit(context, BitmapFactory.decodeResource(context.getResources(), R.drawable.timeline_card_small_web), 40, 40).setGravity(SpecialGravity.CENTER), - new SpecialTextUnit("网页链接")); - } - } - if (maxEnd urlHashMap, final Context context, TextView textView) { SimplifySpanBuild mSpanBuild = new SimplifySpanBuild(); int linkNorTextColor = 0xFF483D8B; @@ -131,10 +57,12 @@ public static SimplifySpanBuild getContent(String source, HashMap - + + android:shape="rectangle" + android:useLevel="true"> + + + - - - - - + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_rectangle_press.xml b/app/src/main/res/drawable/bg_rectangle_press.xml index fcd41f4..9bc0b8c 100644 --- a/app/src/main/res/drawable/bg_rectangle_press.xml +++ b/app/src/main/res/drawable/bg_rectangle_press.xml @@ -1,15 +1,18 @@ - + + android:shape="rectangle" + android:useLevel="true"> + + + - - - - - + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1ea4bd0..c2eea8e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:2.3.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/SimplifySpanBuild.java b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/SimplifySpanBuild.java index 97be905..5343b0a 100644 --- a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/SimplifySpanBuild.java +++ b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/SimplifySpanBuild.java @@ -406,7 +406,6 @@ public SpannableStringBuilder build() { for (int startPos : startPoss) { CustomImageSpan curCustomImageSpan = new CustomImageSpan(normalSizeText, specialImageUnit); spannableStringBuilder.setSpan(curCustomImageSpan, startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - if (specialImageUnit.isClickable()) { addClickStateChangeListeners(startPos, startPos + specialTextLength, curCustomImageSpan); } diff --git a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomClickableSpan.java b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomClickableSpan.java index 3465a13..2a71d16 100644 --- a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomClickableSpan.java +++ b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomClickableSpan.java @@ -1,16 +1,17 @@ package meitu.com.mylibrary.simplifyspan.customspan; import android.graphics.Color; -import android.graphics.Paint; import android.text.Spanned; import android.text.TextPaint; import android.text.style.ClickableSpan; import android.view.View; import android.widget.TextView; + import java.util.List; -import meitu.com.mylibrary.simplifyspan.other.OnClickStateChangeListener; -import meitu.com.mylibrary.simplifyspan.other.OnClickableSpanListener; -import meitu.com.mylibrary.simplifyspan.unit.SpecialClickableUnit; + +import meitu.com.mylibrary.simplifyspan.other.OnClickStateChangeListener; +import meitu.com.mylibrary.simplifyspan.other.OnClickableSpanListener; +import meitu.com.mylibrary.simplifyspan.unit.SpecialClickableUnit; /** * Custom ClickableSpan @@ -42,8 +43,8 @@ public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) { @Override public void onClick(View widget) { if (null != mOnClickableSpanListener) { - TextView tv = (TextView)widget; - Spanned spanned = (Spanned)tv.getText(); + TextView tv = (TextView) widget; + Spanned spanned = (Spanned) tv.getText(); int start = spanned.getSpanStart(this); int end = spanned.getSpanEnd(this); mOnClickableSpanListener.onClick(tv, spanned.subSequence(start, end).toString()); @@ -62,8 +63,10 @@ public void setPressed(boolean isSelected) { @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); - // ds.setTextSize(); + // ds.setTextSize(); // set text color And press status color + + if (mTextColorNor != 0) { if (mTextColorPre != 0) { ds.setColor(isPressed ? mTextColorPre : mTextColorNor); @@ -71,6 +74,7 @@ public void updateDrawState(TextPaint ds) { ds.setColor(mTextColorNor); } } + //ds.setColor(isPressed ? mTextColorPre : mTextColorNor); // set background color And press status color diff --git a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomImageSpan.java b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomImageSpan.java index cb6423c..f13f43f 100644 --- a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomImageSpan.java +++ b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/customspan/CustomImageSpan.java @@ -1,9 +1,15 @@ package meitu.com.mylibrary.simplifyspan.customspan; +import android.content.res.Resources; +import android.graphics.Bitmap; import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Matrix; import android.graphics.Paint; +import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.RectF; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.text.style.ImageSpan; @@ -26,23 +32,24 @@ public class CustomImageSpan extends ImageSpan implements OnClickStateChangeList private int gravity; private Rect mRect = new Rect(); private String mNormalSizeText; - private String mImagDesContent; private Drawable mBgDrawable; private int mBgColor; private boolean isSelected; private boolean isClickable; private int pressBgColor; + private int mSpanWidth; private int mLineTextHeight; private int mLineTextBaselineOffset; - + private int mPadding; public CustomImageSpan(String normalSizeText, SpecialImageUnit specialImageUnit) { super(specialImageUnit.getContext(), specialImageUnit.getBitmap(), ALIGN_BASELINE); this.gravity = specialImageUnit.getGravity(); this.mNormalSizeText = normalSizeText; - this.mImagDesContent = specialImageUnit.getText(); this.mBgDrawable = specialImageUnit.getDrawable(); this.mBgColor = specialImageUnit.getBgColor(); this.isClickable = specialImageUnit.isClickable(); + this.mSpanWidth = specialImageUnit.getSpnWidth(); + this.mPadding = specialImageUnit.getPadding(); } @@ -58,28 +65,26 @@ public void updateDrawState(TextPaint ds) { } @Override - public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { - //mBgDrawable.getState() - if (mBgDrawable!=null){ - if (isSelected){ - mBgDrawable.setState(new int[] {android.R.attr.state_pressed}); - }else { - mBgDrawable.setState(new int[] {}); - } - - mBgDrawable.draw(canvas); + public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { + super.getSize(paint, text, start, end, fm); + if (mSpanWidth == 0){ + mSpanWidth= super.getSize(paint, text, start, end, fm); + }else if (mSpanWidth ==-1){ + mSpanWidth = super.getSize(paint, text, start, end, fm)*(end-start)+mPadding*2; } + return mSpanWidth; + } + + @Override + public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { + //super.draw(canvas, text, start, end, x, top, y, bottom, paint); Drawable b = getCachedDrawable(); int drawableHeight = b.getIntrinsicHeight(); int drawableWidth = b.getIntrinsicWidth(); - int textLength; - paint.getTextBounds(mImagDesContent, 0, mImagDesContent.length(), mRect); - textLength=mRect.width(); - mRect.setEmpty(); - paint.getTextBounds(mNormalSizeText, 0, mNormalSizeText.length(), mRect); - Paint.FontMetrics fontMetrics = paint.getFontMetrics(); - int lineHeight = (int) (Math.ceil(fontMetrics.descent - fontMetrics.ascent) + 2); + paint.getTextBounds(String.valueOf(text), start, end, mRect); + int textLength = mRect.width(); + int lineHeight = mRect.height(); float finalUnitHeight = bottom - top; float bgTop = bottom - finalUnitHeight; @@ -89,27 +94,30 @@ public void draw(Canvas canvas, CharSequence text, int start, int end, float x, }else { mBgDrawable.setState(new int[] {}); } - mBgDrawable.setBounds((int) x, (int) top, (int) (x + paint.measureText(text, start, end)),(int) top + lineHeight); + mBgDrawable.setBounds((int) x, (int) top, (int) (x+mSpanWidth),(int) y +lineHeight/2); mBgDrawable.draw(canvas); - } - if (isClickable && isSelected) { - // click background - paint.setColor(pressBgColor); - //canvas.drawRect(x, bgTop, x + drawableWidth, bgTop + finalUnitHeight, paint); - - canvas.drawRect(x, bgTop, x + drawableWidth, bgTop + lineHeight, paint); - } else { - // normal background - paint.setColor(mBgColor); - canvas.drawRect(x, bgTop, x + drawableWidth, bgTop + lineHeight, paint); + }else { + if (isClickable && isSelected) { + // click background + paint.setColor(pressBgColor); + //paint.setColor(Color.GREEN); + //canvas.drawRect(x, bgTop, x + drawableWidth, bgTop + finalUnitHeight, paint); + + canvas.drawRect(x, bgTop, x + mSpanWidth, bgTop + lineHeight, paint); + } else { + // normal background + paint.setColor(mBgColor); + //paint.setColor(Color.GREEN); + canvas.drawRect(x, bgTop, x + mSpanWidth, bgTop + lineHeight, paint); + } } int textHeight = mRect.height(); - if (drawableHeight > textHeight) { + /* if (drawableHeight > textHeight) { super.draw(canvas, text, start, end, x, top, y, bottom, paint); return; - } + }*/ canvas.save(); @@ -124,24 +132,29 @@ public void draw(Canvas canvas, CharSequence text, int start, int end, float x, switch (gravity) { case SpecialGravity.TOP: { newStartY -= mLineTextHeight - mLineTextBaselineOffset; - canvas.translate(x, newStartY); + canvas.translate(x+8, newStartY); break; } case SpecialGravity.CENTER: { newStartY -= (mLineTextHeight / 2 + drawableHeight / 2 - mLineTextBaselineOffset); - canvas.translate(x, newStartY); + canvas.translate(x+mPadding, newStartY); break; } case SpecialGravity.BOTTOM: { newStartY -= drawableHeight - mLineTextBaselineOffset; - canvas.translate(x, newStartY); + canvas.translate(x+mPadding, newStartY); break; } } b.draw(canvas); canvas.restore(); + paint.setColor(Color.WHITE); + paint.setTextSize(32); + canvas.drawText(text,start+3,end,x+drawableWidth+mPadding*2,y,paint); + } + private Drawable getCachedDrawable() { Drawable drawable = null; diff --git a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/unit/SpecialImageUnit.java b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/unit/SpecialImageUnit.java index cc0d5b6..c6f465f 100644 --- a/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/unit/SpecialImageUnit.java +++ b/mylibrary/src/main/java/meitu/com/mylibrary/simplifyspan/unit/SpecialImageUnit.java @@ -18,6 +18,28 @@ public class SpecialImageUnit extends BaseSpecialUnit { private Bitmap bitmap; private int bgColor; private boolean isClickable; + + public int getPadding() { + return mPadding; + } + + public SpecialImageUnit setPadding(int padding) { + mPadding = padding; + return this; + } + + private int mPadding; + + public int getSpnWidth() { + return mSpnWidth; + } + + public SpecialImageUnit setSpnWidth(int spnWidth) { + mSpnWidth = spnWidth; + return this; + } + + private int mSpnWidth=0; private int width; // px private int height; // px private Drawable mDrawable; @@ -43,13 +65,28 @@ public SpecialImageUnit(Context context, String specialText, Bitmap bitmap) { /** * @param context Context - * @param specialText Special Text + * @param bitmap Bitmap + * @param width Width + * @param height Height + */ + public SpecialImageUnit(Context context, Bitmap bitmap, int width, int height,Drawable drawable) { + super(DEF_IMG_PLACEHOLDER); + this.context = context; + this.bitmap = bitmap; + this.width = width; + this.height = height; + this.mDrawable =drawable; + } + + + /** + * @param context Context * @param bitmap Bitmap * @param width Width * @param height Height */ public SpecialImageUnit(Context context, String specialText, Bitmap bitmap, int width, int height,Drawable drawable) { - super(specialText); + super(DEF_IMG_PLACEHOLDER+specialText); this.context = context; this.bitmap = bitmap; this.width = width;