2

我想将字符串中的文本转换为图像并将其显示在 aImageView中,此外我还想获取在运行时创建的图像的尺寸以供进一步使用。这是我在搜索中所做和使用的。

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

问题:参数没有按照代码中的设置工作。图像转换后不显示完整的文本。

4

3 回答 3

1

我相信它占据了整个屏幕,因为您没有像 Linear Layout 这样的容器,该容器然后包含具有布局约束的 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>
于 2013-03-21T11:15:45.060 回答
0

这对我有用,对于那些想要实现同样目标的人

 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();
    }
于 2013-03-21T12:03:27.463 回答
0

我尝试过Text2Jpg,它看起来很棒,可以将任何文本转换为带有或不带有背景自定义图像的 jpg 图像。

使用静态布局:

    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);
于 2015-07-24T13:44:05.053 回答