0

我有一个关于我的运动的问题。我的问题是我的存储卡中有一个 .txt 文件,里面有一个字符串。我想使用该字符串制作图像。我想创建的图像我不在乎我是否只是一个没有可理解内容的图像,我只想将该字符串作为图像视图上的图像。

我尝试了很多想法,但我什么也做不了

我尝试将 tha 字符串读取为字节 [] 数组,然后使用 Bitmapfactory(),但在图像视图上向我发送了一个空图像。

有没有办法做到这一点?

4

2 回答 2

1

这是因为BitmapFactory期望压缩图像数据。它不知道如何解释你给它的值。作为替代方案,您可以做的是检查byte[]自己,将其转换为int[]并使用Bitmap.setPixels

于 2012-06-25T11:40:34.247 回答
0

试试这样:

private Bitmap getViewBitmap ( String textString ) {
    View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.your_layout, null);
    TextView txt = (TextView)view.findViewById(R.id.txtId);
    txt.setText(textString);

    int spec = MeasureSpec.makeMeasureSpec(187, 187);
    view.measure(spec,spec);
    view.layout(0, 0, 187, 187);

    Bitmap cache = Bitmap.createBitmap(
            view.getWidth(), view.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(cache);
    view.draw(canvas);

    return cache;
}
于 2012-06-25T11:37:47.363 回答