我想將“顯示”的以下部分作為圖像提取到 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(); } }當我執行此代碼時,我獲得了一個 PGN 圖像,其中 Raghul 的單詞被部分截斷,如下圖所示對于名稱 2 和名稱 3:怎么了 ?為什么“Raghul s”和“name 2”被截斷?如何解決這個問題?
1 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
我認為您的問題與您的 TextView 寬度太短而無法完全顯示您的姓名有關。
有 3 種可能性來檢查我看到的原因是否正確。
A.您刪除小部件并在 TextView 中ImageView
替換為wrap_content
match_parent
B. 你在 TextView 小部件中添加以下屬性
android:text="xxxxxxxxxx"
view.invalidate()
C.在使用以下代碼分配 TextView 后,您使用以下代碼刷新您的 LinearLayout :
txt.setText(list.get(i)); view.invalidate();
我認為(未經測試),最后一個命令將解決您的問題。
我已經添加了這個可能性
D. 你可以為 LinearLayout 中的所有小部件著色,看看會發生什么。這樣做,我們可以看到所有小部件的寬度,也許我們可以更好地了解您的問題。
添加回答
舉報
0/150
提交
取消