diff --git a/app/src/main/java/zhou/demo/MainActivity.java b/app/src/main/java/zhou/demo/MainActivity.java index 57eece3..53c0a9e 100644 --- a/app/src/main/java/zhou/demo/MainActivity.java +++ b/app/src/main/java/zhou/demo/MainActivity.java @@ -11,6 +11,8 @@ import com.zzhoujay.richtext.RichText; import com.zzhoujay.richtext.callback.OnUrlClickListener; +import static zhou.demo.TestActivity.TEST_TYPE; + //import com.zzhoujay.okhttpimagedownloader.OkHttpImageDownloader; @@ -181,6 +183,7 @@ public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, "ListView"); menu.add(0, 2, 2, "Gif"); menu.add(0, 3, 3, "Test"); + menu.add(0, 4, 4, "TextWithIcons"); return super.onCreateOptionsMenu(menu); } @@ -192,8 +195,14 @@ public boolean onOptionsItemSelected(MenuItem item) { startActivity(new Intent(this, ListViewActivity.class)); } else if (item.getItemId() == 2) { startActivity(new Intent(this, GifActivity.class)); - } else if (item.getItemId() == 3) { - startActivity(new Intent(this, TestActivity.class)); + } else { + Intent testIntent = new Intent(this, TestActivity.class); + if (item.getItemId() == 3) { + startActivity(testIntent); + } else if (item.getItemId() == 4) { + testIntent.putExtra(TEST_TYPE, 1); + startActivity(testIntent); + } } return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/zhou/demo/TestActivity.java b/app/src/main/java/zhou/demo/TestActivity.java index f71548b..70f835b 100644 --- a/app/src/main/java/zhou/demo/TestActivity.java +++ b/app/src/main/java/zhou/demo/TestActivity.java @@ -18,7 +18,7 @@ public class TestActivity extends AppCompatActivity { - static final String test = "

Test

" + + static final String test = "

Test

" + "\"\"" + "\"\"" + "\"\"" + @@ -27,11 +27,21 @@ public class TestActivity extends AppCompatActivity { "\"\"" + "\"\"" + "
" + - "
"; + ""; + static final String test2 = "

Test2

" + + "" + + "Android——Test 打赏 " + + "" + + "" + + "我是Test 一个水晶花" + + "" + + ""; private RichText richText; private TextView textView; + public static final String TEST_TYPE = "test_type"; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -40,17 +50,31 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { textView = findViewById(R.id.text); assert textView != null; + if (getIntent().getIntExtra(TEST_TYPE, 0) == 0) { + richText = RichText.from(test) + .imageClick(new OnImageClickListener() { + @Override + public void imageClicked(List imageUrls, int position) { + Calendar calendar = Calendar.getInstance(); + int m = calendar.get(Calendar.MINUTE); + int s = calendar.get(Calendar.SECOND); + Toast.makeText(TestActivity.this, "M:" + m + ",S:" + s, Toast.LENGTH_SHORT).show(); + } + }).into(textView); + } else { + richText = RichText.from(test2) + .autoFix(false) + .imageClick(new OnImageClickListener() { + @Override + public void imageClicked(List imageUrls, int position) { + Calendar calendar = Calendar.getInstance(); + int m = calendar.get(Calendar.MINUTE); + int s = calendar.get(Calendar.SECOND); + Toast.makeText(TestActivity.this, "M:" + m + ",S:" + s, Toast.LENGTH_SHORT).show(); + } + }).into(textView); + } - richText = RichText.from(test) - .imageClick(new OnImageClickListener() { - @Override - public void imageClicked(List imageUrls, int position) { - Calendar calendar = Calendar.getInstance(); - int m = calendar.get(Calendar.MINUTE); - int s = calendar.get(Calendar.SECOND); - Toast.makeText(TestActivity.this, "M:" + m + ",S:" + s, Toast.LENGTH_SHORT).show(); - } - }).into(textView); } diff --git a/richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java b/richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java index fc9131f..a060201 100644 --- a/richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java +++ b/richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java @@ -43,10 +43,46 @@ public ClickableImageSpan(Drawable drawable, List imageUrls, int positio @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 drawable = getDrawable(); + Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt(); + int transY = (y + fontMetricsInt.descent + y + fontMetricsInt.ascent) / 2 - drawable.getBounds().bottom / 2; + canvas.save(); + canvas.translate(x, transY); + drawable.draw(canvas); + canvas.restore(); this.x = x; } + // Extra variables used to redefine the Font Metrics when an ImageSpan is added + private int initialDescent = 0; + private int extraSpace = 0; + + // Method used to redefined the Font Metrics when an ImageSpan is added + @Override + public int getSize(Paint paint, CharSequence text, + int start, int end, + Paint.FontMetricsInt fm) { + Drawable d = getDrawable(); + Rect rect = d.getBounds(); + + if (fm != null) { + // Centers the text with the ImageSpan + if (rect.bottom - (fm.descent - fm.ascent) >= 0) { + // Stores the initial descent and computes the margin available + initialDescent = fm.descent; + extraSpace = rect.bottom - (fm.descent - fm.ascent); + } + + fm.descent = extraSpace / 2 + initialDescent; + fm.bottom = fm.descent; + + fm.ascent = -rect.bottom + fm.descent; + fm.top = fm.ascent; + } + + return rect.right; + } + public boolean clicked(int position) { Drawable drawable = getDrawable(); if (drawable != null) {