"Textviewをビットマップにスクリーンショット"というトピックで多くの投稿が見つかりました。TextViewをビットマップに描画する方法(ディスプレイに描画することなく)
私の問題との違いは、まずビューがディスプレイに描画され(すべてのレイアウトと測定作業が完了している)、Bitmapに接続されたCanvasに描画されます。
ビットマップにレンダリングされるディスプレイに表示されることなく、最初からTextViewを作成したいだけです。
これは既に機能している基本設定です。 TextViewをクリックすると、それ自体がビットマップに描画され、ImageViewに設定されます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#fff">
<TextView android:id="@+id/tv" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="The Quick Brown Fox Jumps Over The Lazy Dog."
android:textSize="20dip" android:background="#abcdef"
android:textColor="#000" android:padding="10dip"
android:layout_margin="10dip" />
<ImageView android:id="@+id/iv" android:layout_width="449px"
android:layout_height="47px" android:background="#56789a"
android:layout_margin="10dip" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.tv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bmp = Bitmap.createBitmap(449, 47, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
v.draw(canvas);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
}
});
}
今問題の部分があります。私はJavaでTextViewを作成し、これをビットマップに直接描画します。その後、これをImageViewに設定します。私は、このランニング:(
Bitmap bmp = Bitmap.createBitmap(449, 47, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
TextView tv = new TextView(this);
tv.setText("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
tv.setTextSize(55f);
tv.setTextColor(this.getResources().getColor(android.R.color.black));
tv.draw(canvas);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
これはどちらか動作しませんでしたsetDrawingCacheEnabled()、測定()とrequestLayout()を試す。のonCreateでもOnClickListenerのいずれにおいても動作しません。
私はたくさん助けました。 –