Skip to content

Commit

Permalink
修改为ImageTextSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
黄世杰 authored and 黄世杰 committed Sep 7, 2017
1 parent b7ef07c commit 72edcd3
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 156 deletions.
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<source.length()){
mSpanBuild.append(source.substring(maxEnd,source.length()));
}
return mSpanBuild;
}


public static SimplifySpanBuild getContent(String source, HashMap<String,SpecialUrlBean> urlHashMap, final Context context, TextView textView) {
SimplifySpanBuild mSpanBuild = new SimplifySpanBuild();
int linkNorTextColor = 0xFF483D8B;
Expand All @@ -131,10 +57,12 @@ public static SimplifySpanBuild getContent(String source, HashMap<String,Special
preStart=end+1;
}
mSpanBuild.append(new SpecialTextUnit(at, linkNorTextColor)
.setClickableUnit(new SpecialClickableUnit(textView, new OnClickableSpanListener() {
.setClickableUnit(new SpecialClickableUnit(textView,
new OnClickableSpanListener() {
@Override
public void onClick(TextView tv, String clickText) {
Toast.makeText(context, "Click Text: " + clickText, Toast.LENGTH_SHORT).show();}})
Toast.makeText(context, "Click Text: " + clickText, Toast.LENGTH_SHORT).show();}
}).setPressTextColor(linkNorTextColor).setNormalTextColor(linkNorTextColor)
.setPressBgColor(Color.TRANSPARENT)));
}
//处理##话题
Expand All @@ -152,8 +80,10 @@ public void onClick(TextView tv, String clickText) {
@Override
public void onClick(TextView tv, String clickText) {
Toast.makeText(context, "Click Text: " + clickText, Toast.LENGTH_SHORT).show();}
}).
setPressBgColor(Color.TRANSPARENT)));
})
.setPressTextColor(linkNorTextColor)
.setNormalTextColor(linkNorTextColor)
.setPressBgColor(Color.TRANSPARENT)));

}
// 处理url地址
Expand All @@ -172,13 +102,27 @@ public void onClick(TextView tv, String clickText) {
public void onClick(TextView tv, String clickText) {
Toast.makeText(context, "Click Text: " + url, Toast.LENGTH_SHORT).show();
}
}).setNormalTextColor(bean.getTextNormalColor()).setPressBgColor(Color.TRANSPARENT).setNormalBgColor(Color.TRANSPARENT),
new SpecialImageUnit(context,bean.getURLName(),
bean.getIcon(), 33, 33,
}).setNormalTextColor(bean.getTextNormalColor()).setPressBgColor(Color.TRANSPARENT).setNormalBgColor(Color.TRANSPARENT)
, new SpecialImageUnit(context,bean.getURLName(),bean.getIcon(), 33, 33,
context.getResources().getDrawable(R.drawable.bg_rectangle,context.getTheme())
).setGravity(SpecialGravity.CENTER),
new SpecialTextUnit(bean.getURLName()).setTextSize(13)
);
)
.setSpnWidth(-1).setPadding(8)
//.setGravity(SpecialGravity.CENTER)

);
}else {
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(bean.getTextNormalColor()).setPressBgColor(Color.TRANSPARENT).setNormalBgColor(Color.TRANSPARENT)
, new SpecialImageUnit(context,bean.getURLName(), BitmapFactory.decodeResource(context.getResources(), R.drawable.timeline_card_small_web), 33, 33,
context.getResources().getDrawable(R.drawable.bg_rectangle,context.getTheme()))
.setSpnWidth(-1)
//.setGravity(SpecialGravity.CENTER)

);
}

}
Expand All @@ -193,6 +137,7 @@ public void onClick(TextView tv, String clickText) {

}
}).setPressBgColor(linkPressBgColor)));

return mSpanBuild;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class MainActivity extends AppCompatActivity implements OnClickableSpanLi
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
TextView tvText1 = (TextView) findViewById(R.id.tv_text1);
TextView tvText11 = (TextView) findViewById(R.id.tv_text11);
TextView tvText2 = (TextView) findViewById(R.id.tv_text2);
Expand Down Expand Up @@ -159,7 +158,7 @@ public void onClick(View view) {
.append("有默认背景")
.append(new SpecialTextUnit("点我点我3").setClickableUnit(new SpecialClickableUnit(tvText41, this).setPressBgColor(Color.BLUE).setPressTextColor(Color.WHITE)).setTextColor(0xFFFF5000).setTextBackgroundColor(0xFF87CEEB))
.append("我只是个结尾");
tvText41.setText(simplifySpanBuild41.build());*/
tvText41.setText(simplifySpanBuild41.build());

TextView richTextView = (TextView) findViewById(R.id.rtv_conent);
RichTextBean richTextBean = new RichTextBean();
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/res/drawable/bg_rectangle_normal.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- android:shape指定形状类型,默认为rectangle -->
<!-- 矩形边框圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
android:shape="rectangle"
android:useLevel="true">

<size android:height="100dp"
android:width="100dp"/>

<!-- solid指定形状的填充色,只有android:color一个属性 -->
<solid android:color="#2F90BD" />
<!-- padding设置内容区域离边界的间距 -->
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
<!-- corners设置圆角,只适用于rectangle -->
<corners android:radius="200dp" />
<solid android:color="#e00c19a9" />

<corners android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />

</shape>
25 changes: 14 additions & 11 deletions app/src/main/res/drawable/bg_rectangle_press.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- android:shape指定形状类型,默认为rectangle -->
<!-- 矩形边框圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
android:shape="rectangle"
android:useLevel="true">

<size android:height="100dp"
android:width="100dp"/>

<!-- solid指定形状的填充色,只有android:color一个属性 -->
<solid android:color="#52a149ed" />
<!-- padding设置内容区域离边界的间距 -->
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
<!-- corners设置圆角,只适用于rectangle -->
<corners android:radius="200dp" />
<solid android:color="#afb611a6" />

<corners android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />

</shape>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -62,15 +63,18 @@ 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);
} else {
ds.setColor(mTextColorNor);
}
}
//ds.setColor(isPressed ? mTextColorPre : mTextColorNor);

// set background color And press status color

Expand Down
Loading

0 comments on commit 72edcd3

Please sign in to comment.