android imagespan 图像在单行文本视图中被剪切错误
android imagespan image is cut error in singleline textview
我使用 ImageSpan 在文本视图中显示图像。
TextView 是带有结束椭圆大小的单线。
当图像位于 textview 的末尾并且没有足够的 space 时。
我认为它应该显示“...”。
但它显示了 Image 的一部分。图像被错误地剪裁。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert image"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"/>
</LinearLayout>
代码:
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String PATTERN = "\[\w+?\]";
private Pattern pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertImageSpan();
}
});
pattern = Pattern.compile(PATTERN);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
textView.setText(s);
}
});
}
private void insertImageSpan(){
insertTextInCurrentPosition(editText,"[face]");
int select = editText.getSelectionStart();
editText.setText(spanText(editText.getText()));
editText.setSelection(select);
}
public CharSequence spanText(CharSequence text) {
SpannableStringBuilder t = null;
if(text instanceof SpannableStringBuilder){
t = (SpannableStringBuilder) text;
}else{
t = new SpannableStringBuilder(text);
}
Matcher m = pattern.matcher(text);
while (m.find()) {
String mResult = m.group();
String key = mResult.substring(1, mResult.length() - 1);
ImageSpan[] spans = t.getSpans(m.start(),m.end(),ImageSpan.class);
if(spans== null || spans.length==0 ){
try{
ImageSpan span = new ImageSpan(this , R.drawable.ic_launcher);
t.setSpan(span , m.start() , m.end() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}catch (Exception e){
continue;
}
}
}
return t;
}
public static void insertTextInCurrentPosition(EditText tv, CharSequence str) {
if (tv == null || TextUtils.isEmpty(str)) return;
tv.getText().replace(tv.getSelectionStart() , tv.getSelectionEnd() , str , 0 , str.length());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在将文本设置到 TextView 之前,我使用 TextUtils.ellipsize 作为文本。
CharSequence cs = TextUtils.ellipsize(s, textView.getPaint(), textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft(), TextUtils.TruncateAt.END);
textView.setText(cs);
如果textview设置wrap_content或match_parent,最好在textview.post()中调用TextUtils.ellipsize;
tv.post(new Runnable() {
@Override
public void run() {
tv.setText(TextUtils.ellipsize(tv.getText(), (TextPaint) tv.getPaint(), tv.getWidth() - tv.getPaddingRight() - tv.getPaddingLeft(), TextUtils.TruncateAt.END));
}
});
我使用 ImageSpan 在文本视图中显示图像。 TextView 是带有结束椭圆大小的单线。 当图像位于 textview 的末尾并且没有足够的 space 时。 我认为它应该显示“...”。 但它显示了 Image 的一部分。图像被错误地剪裁。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert image"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"/>
</LinearLayout>
代码:
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String PATTERN = "\[\w+?\]";
private Pattern pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertImageSpan();
}
});
pattern = Pattern.compile(PATTERN);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
textView.setText(s);
}
});
}
private void insertImageSpan(){
insertTextInCurrentPosition(editText,"[face]");
int select = editText.getSelectionStart();
editText.setText(spanText(editText.getText()));
editText.setSelection(select);
}
public CharSequence spanText(CharSequence text) {
SpannableStringBuilder t = null;
if(text instanceof SpannableStringBuilder){
t = (SpannableStringBuilder) text;
}else{
t = new SpannableStringBuilder(text);
}
Matcher m = pattern.matcher(text);
while (m.find()) {
String mResult = m.group();
String key = mResult.substring(1, mResult.length() - 1);
ImageSpan[] spans = t.getSpans(m.start(),m.end(),ImageSpan.class);
if(spans== null || spans.length==0 ){
try{
ImageSpan span = new ImageSpan(this , R.drawable.ic_launcher);
t.setSpan(span , m.start() , m.end() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}catch (Exception e){
continue;
}
}
}
return t;
}
public static void insertTextInCurrentPosition(EditText tv, CharSequence str) {
if (tv == null || TextUtils.isEmpty(str)) return;
tv.getText().replace(tv.getSelectionStart() , tv.getSelectionEnd() , str , 0 , str.length());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在将文本设置到 TextView 之前,我使用 TextUtils.ellipsize 作为文本。
CharSequence cs = TextUtils.ellipsize(s, textView.getPaint(), textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft(), TextUtils.TruncateAt.END);
textView.setText(cs);
如果textview设置wrap_content或match_parent,最好在textview.post()中调用TextUtils.ellipsize;
tv.post(new Runnable() {
@Override
public void run() {
tv.setText(TextUtils.ellipsize(tv.getText(), (TextPaint) tv.getPaint(), tv.getWidth() - tv.getPaddingRight() - tv.getPaddingLeft(), TextUtils.TruncateAt.END));
}
});