线性布局视图到位图图像视图

时间:2017-11-02 12:29:36

标签: java android android-layout listview

如何获取位图图像与线性布局中的文本视图相同 img

我的XML代码是

<LinearLayout
    android:id="@+id/screen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textString" />
</LinearLayout>
<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

2 个答案:

答案 0 :(得分:1)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/screen"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="textString" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

答案 1 :(得分:0)

  

将textview中的文本转换为位图

TextView textview = (TextView) findViewById(R.id.textviewid);
ImageView imageview = (ImageView) findViewById(R.id.imageviewid)
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(textview.getDrawingCache());
imageview.setImageBitmap(bitmap);
  

将linearlayout转换为位图

LinearLayout linearlayout = (LinearLayout)findViewById(R.id.linearlayoutid);
ImageView imageview = (ImageView) findViewById(R.id.imageviewid)
linearlayout.setDrawingCacheEnabled(true);
linearlayout.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(linearlayout.getDrawingCache());
imageview.setImageBitmap(bitmap);

预先测量视图,使高度和宽度不会保持为空。

linearlayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

为视图及其所有后代指定大小和位置

  linearlayout.layout(0, 0, linearlayout.getMeasuredWidth(), linearlayout.getMeasuredHeight());