如何在Android中将图像用作数字/字母?

时间:2011-05-29 08:08:47

标签: android fonts android-image

我正在开发一个我需要显示计数的应用程序。该计数将显示例如“4”,然后是“4 + 1”,然后最终显示为“5”。我有.png图像,我想用作数字的背景,以便我可以轻松地显示任何高达“99”的内容,如果我需要的话。

所以基本上,我想为一个数字指定一个可绘制的背景。自定义字体是要走的路吗?提到了具有指定字符串的H ere字体和视图组。有人试过吗?

2 个答案:

答案 0 :(得分:2)

HashMap<char, Drawable> charMap = new HashMap(Character, Drawable>();
Resources res = getResources();
charMap.put('0', new Drawable(res, R.drawable.img_0));
charMap.put('1', new Drawable(res, R.drawable.img_1));
// the rest of the characters you want to use

如果要为角色检索正确的图像:

char c = '0';
Drawable charImage = charMap.get(c);

答案 1 :(得分:2)

实际上你只需要一个大小为10的数组:

Resources res = getResources();
Drawable numbers = new Drawable[10]{
   new Drawable(res, R.drawable.zero),
   new Drawable(res, R.drawable.one),
   new Drawable(res, R.drawable.two),
   ...
   new Drawable(res, R.drawable.nine)
};

并让他们使用:

int i = 5;
Drawable image = numbers[i];

如果数字有2位数,那么你可以输入2张图片等。

你也可以制作一个非常大的阵列,但这实际上是浪费线条。 此外,仅使用10张照片时所需的空间要小得多