我想将'display'的以下部分作为图像提取到PNG文件中。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Layout">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/Image_view"
/>
<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
</LinearLayout>
为此,我使用以下代码
list.add("Raghul s");
list.add("Name 2");
list.add("Name 3");
for(int i=0;i<list.size();i++)
{
LinearLayout view = (LinearLayout) findViewById(R.id.Layout);
txt = (TextView) findViewById(R.id.Text_View1);
txt.setText(list.get(i));
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
bs = new ByteArrayOutputStream();
bs.compress(Bitmap.CompressFormat.PNG, 40, bitmap);
try {
file = new File("Image.png");
file.createNewFile();
ws = new FileOutputStream(file);
ws.write(bs.toByteArray());
ws.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
当我执行此代码时,我获得了一个PNG图像,其中Raghul的单词被部分截断,如下图所示
怎么了 ?
为什么“Raghul s”和“name 2”会被截断?
如何解决这个问题?
我认为您的问题与TextView宽度太短而无法完全显示您的名字有关。
有3种可能性可以检查我看到的原因是否正确。
A.你删除ImageView
小部件,你用wrap_content
替换TextView中的match_parent
B.在TextView小部件中添加以下属性
android:text="xxxxxxxxxx"
C.使用以下代码分配TextView后,使用view.invalidate()
刷新LinearLayout:
txt.setText(list.get(i));
view.invalidate();
我认为(没有测试),最后一个命令将解决您的问题。
我已经添加了这种可能性
D.你可以为LinearLayout中的所有小部件着色,看看会发生什么。这样做,我们可以看到所有小部件的宽度,也许我们可以更好地了解您的问题。