Android的imagespan图像切割误差在单线的TextView [英] android imagespan image is cut error in singleline textview

查看:624
本文介绍了Android的imagespan图像切割误差在单线的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ImageSpan在TextView中显示的图像。
TextView是被sinlgline与最终ellipsize。
当图像是在TextView中的端部并没有为它足够多的空间。
我觉得它应该显示...。
但它显示图像的一部分。图像被错误截断。

在xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            的xmlns:工具=htt​​p://schemas.android.com/tool​​s
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
            机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
            机器人:paddingTop =@扪/ activity_vertical_margin
            机器人:paddingBottom会=@扪/ activity_vertical_margin
            工具:上下文=。MainActivity
机器人:方向=垂直>
    <的EditText
    机器人:ID =@ + ID / EditText上
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT/>    <按钮
    机器人:ID =@ + ID / BTN。
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=插入图片/>    <的TextView
    机器人:ID =@ + ID /文
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:单线=真
    机器人:ellipsize =结束/>
< / LinearLayout中>

在code:

 公共类MainActivity延伸活动{私人的EditText EDITTEXT;
私人Button按钮;
私人TextView的TextView的;私有静态最终字符串模式=\\\\ [?\\\\ W + \\\\];
私人模式格局;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    EDITTEXT =(EditText上)findViewById(R.id.edittext);
    按钮=(按钮)findViewById(R.id.btn);
    TextView的=(的TextView)findViewById(R.id.text);    button.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            insertImageSpan();
        }
    });    模式= Pattern.compile(模式);    editText.addTextChangedListener(新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){        }        @覆盖
        公共无效afterTextChanged(编辑S){
            textView.setText(多个);
        }
    });
}私人无效insertImageSpan(){
    insertTextInCurrentPosition(EDITTEXT[脸上]);
    诠释选择= editText.getSelectionStart();
    editText.setText(spanText(editText.getText()));
    editText.setSelection(选择);
}公众的CharSequence spanText(CharSequence的文字){
    SpannableStringBuilder T = NULL;
    如果(文本的instanceof SpannableStringBuilder){
        T =(SpannableStringBuilder)文本;
    }其他{
        T =新SpannableStringBuilder(文本);
    }
    匹配M = pattern.matcher(文本);
    而(m.find()){
        串mResult = m.group();
        字符串键= mResult.substring(1 mResult.length() - 1);        ImageSpan []跨距= t.getSpans(m.start(),m.end(),ImageSpan.class);
        如果(跨度== NULL || spans.length == 0){
            尝试{
                ImageSpan跨度=新ImageSpan(这一点,R.drawable.ic_launcher);
                t.se​​tSpan(跨度,m.start(),m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }赶上(例外五){
                继续;
            }
        }    }
    返回吨;
}公共静态无效insertTextInCurrentPosition(EditText上电视,为CharSequence STR){
    如果(电视== NULL || TextUtils.isEmpty(STR))回报;
    tv.getText()取代(tv.getSelectionStart(),tv.getSelectionEnd(),STR,0,str.length());
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.menu_main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}
}


解决方案

我使用 TextUtils.ellipsize 设置入的TextView之前的文本。

 的CharSequence CS = TextUtils.ellipsize(S,textView.getPaint(),textView.getWidth()-textView.getPaddingRight() -  textView.getPaddingLeft(),TextUtils.TruncateAt.END );
textView.setText(CS);

I use ImageSpan to display image in textview. The TextView is sinlgline with end ellipsize. When image is in the end of textview and there is not enought space for it. I think it should display "...". But it display part of Image . The image is clipped in error.

the 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>

the code:

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);
}


}

解决方案

I use TextUtils.ellipsize for the text before setting it into textView.

CharSequence cs = TextUtils.ellipsize(s , textView.getPaint() ,textView.getWidth() -textView.getPaddingRight() - textView.getPaddingLeft()  , TextUtils.TruncateAt.END);
textView.setText(cs);

这篇关于Android的imagespan图像切割误差在单线的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆