Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更改图片对齐方式为对齐文本中央 #322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/src/main/java/zhou/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
48 changes: 36 additions & 12 deletions app/src/main/java/zhou/demo/TestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class TestActivity extends AppCompatActivity {

static final String test = "<h1>Test</h1><div class=\"topic_content\" itemprop=\"articleBody\">" +
static final String test = "<h1>Test</h1><body>" +
"<img alt=\"\" src=\"http://img10.360buyimg.com/imgzone/jfs/t2719/53/693438809/405912/957c1efa/5721e109N8ad86029.jpg\">" +
"<img alt=\"\" src=\"http://img10.360buyimg.com/imgzone/jfs/t2836/30/707249522/270588/840d428a/5721e108Ne667230f.jpg\">" +
"<img alt=\"\" src=\"http://img10.360buyimg.com/imgzone/jfs/t2305/211/1222246162/89571/4ce4f9a1/56496ac7N982aa001.jpg\">" +
Expand All @@ -27,11 +27,21 @@ public class TestActivity extends AppCompatActivity {
"<img alt=\"\" src=\"http://img10.360buyimg.com/imgzone/jfs/t2776/164/715581717/852142/2fa4714f/5721e10bN04e38f08.jpg\">" +
"<img alt=\"\" src=\"http://img10.360buyimg.com/imgzone/jfs/t2104/197/2936780208/316761/f3051b63/5721e10cN1b74089c.jpg\">" +
"<br>" +
"</div>";
"</body>";
static final String test2 = "<h1>Test2</h1><body>" +
"<body><img src=\"https://static.911tech.cn/images/FmoYOaecbs1fnCmRrhEtjFCEXaJh.png?imageView2/2/h/100/interlace/1\"/>" +
"<img src=\"https://static.911tech.cn/images/FmoYOaecbs1fnCmRrhEtjFCEXaJh.png?imageView2/2/h/100/interlace/1\"/><a href=bbdzt://uid?uid=5009>Android——Test</a><font color=#6DFFC6> 打赏 </font>" +
"<img src=\"https://static.911tech.cn/images/FmoYOaecbs1fnCmRrhEtjFCEXaJh.png?imageView2/2/h/100/interlace/1\"/>" +
"<img src=\"https://static.911tech.cn/images/FmoYOaecbs1fnCmRrhEtjFCEXaJh.png?imageView2/2/h/100/interlace/1\"/>" +
"<a href=bbdzt://uid?uid=5009>我是Test</a><font color=#6DFFC6> 一个水晶花</font>" +
"<img src=\"https://static.911tech.cn/images/FmoYOaecbs1fnCmRrhEtjFCEXaJh.png?imageView2/2/h/200/interlace/1\"/></body>" +
"</body>";

private RichText richText;
private TextView textView;

public static final String TEST_TYPE = "test_type";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -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<String> 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<String> 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<String> 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);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,46 @@ public ClickableImageSpan(Drawable drawable, List<String> 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) {
Expand Down