我正在开发一个需要显示计数的应用程序。例如,此计数将显示“4”,然后是“4 + 1”,最后是“5”。我有 .png 图像,我想将其用作数字的背景,以便在需要时可以轻松地显示最大为“99”的任何内容。
所以基本上,我想为一个数字分配一个可绘制的背景。自定义字体是要走的路吗?这里提到了带有分配字符串的字体和视图组。有人试过这个吗?
我正在开发一个需要显示计数的应用程序。例如,此计数将显示“4”,然后是“4 + 1”,最后是“5”。我有 .png 图像,我想将其用作数字的背景,以便在需要时可以轻松地显示最大为“99”的任何内容。
所以基本上,我想为一个数字分配一个可绘制的背景。自定义字体是要走的路吗?这里提到了带有分配字符串的字体和视图组。有人试过这个吗?
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);
实际上你只需要一个大小为 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 张图片所需的空间也明显更小