2011-09-04 5 views
9

Nhiều bài viết được tìm thấy theo chủ đề "ảnh chụp màn hình một TextView thành một ảnh bitmap".Cách vẽ TextView vào Bitmap (không bao giờ được vẽ trên màn hình)

Vâng, sự khác biệt đối với vấn đề của tôi là, lần đầu tiên khung nhìn được vẽ trên màn hình (với tất cả bố cục và đo lường đã được thực hiện) và sau đó được vẽ vào Canvas kết nối với Bitmap.

Tôi chỉ muốn tạo TextView từ đầu mà không bao giờ được hiển thị trên màn hình được hiển thị thành bitmap.

Đây là cấu hình cơ sở đã hoạt động. Một cú nhấp chuột vào TextView sẽ tự vẽ vào một Bitmap và đặt nó thành một 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); 
     } 
    }); 
} 

Bây giờ đến phần có vấn đề. Tôi sẽ tạo một TextView trong Java và tôi muốn cái này được vẽ thẳng vào một Bitmap. Sau này tôi sẽ thiết lập điều này cho một ImageView. Tôi không bao giờ có chạy này :(

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); 

này không làm việc không phải trong onCreate cũng không phải trong một OnClickListener. Thử nghiệm với setDrawingCacheEnabled(), đo lường() và requestLayout() không làm việc một trong hai.

Trả lời

19

Dưới đây là hai phương pháp làm thế nào để vẽ một TextView vào vải mà thuộc về một cái nhìn hoặc nó có nguồn gốc từ một bitmap:

//method 1 
TextPaint tp = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); 
tp.setColor(Color.WHITE); 
tp.setTextSize(30); 
tp.setShadowLayer(5, 2, 2, Color.CYAN); 
StaticLayout sl=new StaticLayout("This is the first sample text 
     which will be wrapped within the text box.",tp,300, 
     Layout.Alignment.ALIGN_NORMAL, 1f,0f,false); 

canvas.save(); 
canvas.translate(50, 20); //position text on the canvas 
sl.draw(canvas); 
canvas.restore(); 

//method 2 
TextView textView = new TextView(StartActivity.this); 
textView.layout(0, 0, 300, 500); //text box size 300px x 500px 
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30); 
textView.setTextColor(Color.WHITE); 
textView.setShadowLayer(5, 2, 2, Color.CYAN); //text shadow 
textView.setText("This is the second sample 
     text which will be wrapped within the text box."); 
textView.setDrawingCacheEnabled(true); 
canvas.drawBitmap(textView.getDrawingCache(), 50, 200, null); 
    //text box top left position 50,50 
+0

Giúp tôi rất nhiều. –