2013-03-21 95 views
2

我想將字符串中的文本轉換爲圖像,並將其顯示在ImageView中,此外我想獲取在運行時創建的圖像的尺寸以供進一步使用。這是我從搜索中完成和使用的內容。Android text to image

tv = new TextView(this); 
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60); 

    tv.setLayoutParams(layoutParams); 

    tv.setText("Some Text, can be long as much as required"); 
    tv.setTextColor(Color.BLACK); 
    tv.setBackgroundColor(Color.WHITE); 

    Bitmap testB; 
    timer = new Timer(); 
    timer.schedule(new TickerTask(), 1000,25); 
    testB = Bitmap.createBitmap(600, 20, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(testB); 

    tv.layout(0, 0, 380, 100); 
    tv.draw(c); 

    iv = (ImageView) findViewById(R.id.menuIcon); 
    iv.setLayoutParams(layoutParams); 
    iv.setBackgroundColor(Color.GREEN); 
    iv.setImageBitmap(testB); 

問題: 參數不工作在代碼中設置。 轉換圖像後不顯示完整文字。

+0

不是真的勸..如何使用背景圖片和不斷變化的文字..類似的ImageButton ..? – ngesh 2013-03-21 10:48:22

+0

試試這個http://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android – sinek 2013-03-21 10:48:26

+0

我必須達到的任務需要將文本轉換爲圖像。我正在水平上對文本執行動畫,但無法檢測到文本已完全通過左側文本視圖的限制。完成後,圖像寬度可以幫助我在動畫上應用重新啓動 – 2013-03-21 10:51:03

回答

0

這爲我工作,對於那些誰希望同樣的事情到acheive

BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable(); 
    Bitmap bitmap = drawable.getBitmap(); 
    //Bitmap bitmap = testB; 
    File sdCardDirectory = Environment.getExternalStorageDirectory(); 
    File image = new File(sdCardDirectory, "test.png"); 

    boolean success = false; 

    // Encode the file as a PNG image. 
    FileOutputStream outStream; 
    try { 

     outStream = new FileOutputStream(image); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     /* 100 to keep full quality of the image */ 

     outStream.flush(); 
     outStream.close(); 
     success = true; 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (success) { 
     Toast.makeText(getApplicationContext(), "Image saved with success", 
       Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(getApplicationContext(), 
       "Error during image saving", Toast.LENGTH_LONG).show(); 
    } 
1

我相信它佔據了整個屏幕,因爲您沒有容器,如線性佈局,然後包含帶有佈局約束的ImageView,因此ImageView將擴展以填充可用屏幕。試試這個:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TextView tv = new TextView(this); 
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); 
    tv.setLayoutParams(layoutParams); 
    tv.setText("testing 1 2 3"); 
    tv.setTextColor(Color.BLACK); 
    tv.setBackgroundColor(Color.TRANSPARENT); 

    Bitmap testB; 

    testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(testB); 
    tv.layout(0, 0, 80, 100); 
    tv.draw(c); 

    ImageView iv = (ImageView)findViewById(R.id.menuIcon); 
    iv.setLayoutParams(layoutParams); 
    iv.setBackgroundColor(Color.GRAY); 
    iv.setImageBitmap(testB); 
    iv.setMaxHeight(80); 
    iv.setMaxWidth(80); 
} 

而在你的main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <ImageView android:id="@+id/menuIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 


</LinearLayout> 
0

我已經嘗試Text2Jpg它看起來很棒,可以將任何文本轉換爲帶有或不帶背景自定義圖像的jpg圖像。

使用StaticLayout:

TextPaint mTextPaint = new TextPaint(); 
    mTextPaint.setColor(bordercolor); 
    mTextPaint.setTextSize(textSize); 
    StaticLayout mTextLayout = new StaticLayout(text, mTextPaint, (int) (screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); 
    // DynamicLayout mTextLayout = new DynamicLayout(text, mTextPaint,(int)(screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); 
    //mTextPaint.setStrokeWidth(screen_width * .70f); 
    canvas.translate(screen_width * .12f, screen_height * .06f); //position the text 
    mTextLayout.draw(canvas);