Android - 位图高度= Textview文本高度?

时间:2013-08-06 16:06:31

标签: android bitmap textview height scale

在文本视图中添加图像有效。将图像高度和宽度更改为文本视图文本高度不起作用。 (原始图像“红色圆圈”是:32px x 32px)

输出如下:

test img http://img850.imageshack.us/img850/9500/m1f.png

XML:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp" />

代码:

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.textView);   
}

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  // TODO Auto-generated method stub
  super.onWindowFocusChanged(hasFocus);  

    String getText = getResources().getString(R.string.getText);

    textView.setText(getText);

    SpannableStringBuilder ssb = new SpannableStringBuilder(getText);

    Bitmap redcircle= BitmapFactory.decodeResource( getResources(), R.drawable.redcircle );

    Bitmap resizedbitmap = Bitmap.createScaledBitmap(redcircle, (int)textView.getMeasuredHeight(), (int)textView.getMeasuredHeight(), true);
    ssb.setSpan( new ImageSpan( resizedbitmap ), 3, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE );    

    textView.setText( ssb, BufferType.SPANNABLE );      
}

编辑:

我想,图像的高度和宽度等于文本视图文本高度。例如:

test img 2 http://img849.imageshack.us/img849/3163/r9x.png

1 个答案:

答案 0 :(得分:1)

尝试使用

ImageSpan(resizedbitmap, ImageSpan.ALIGN_BASELINE)

而不是

ImageSpan(resizedbitmap)

ImageSpan的默认对齐方式为ALIGN_BOTTOM