Blame |
Last modification |
View Log
| RSS feed
package com.gebauz.pingK;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
public class TextRender
{
public static int CHARACTER_SIZE =
8;
Bitmap mFontMap
;
float mRotation = 0.0f
;
int mColor =
Color.
WHITE;
char characters
[] =
{
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'!',
'=',
'-'
};
public TextRender
(Resources resources
) {
mFontMap = BitmapFactory.
decodeResource(resources, R.
drawable.
font);
}
public void drawText
(Canvas canvas,
String text,
float x,
float y
) {
drawText
(canvas, text, x, y, 1.0f
);
}
public void drawText
(Canvas canvas,
String text,
float x,
float y,
float scale
) {
String actualText = text.
toUpperCase();
Paint paint =
new Paint();
paint.
setColor(mColor
);
paint.
setColorFilter(new PorterDuffColorFilter
(mColor, PorterDuff.
Mode.
SRC_ATOP));
float offsetX = 0.0f
;
for (int i =
0; i
< actualText.
length(); i++
) {
char c = actualText.
charAt(i
);
int index = getCharacterIndex
(c
);
offsetX += CHARACTER_SIZE
;
if (index == -
1) {
// white space or unknown
continue;
}
canvas.
save();
canvas.
rotate(mRotation, x, y
);
canvas.
drawBitmap(mFontMap,
new Rect
(index
* CHARACTER_SIZE,
0,
(index+
1) * CHARACTER_SIZE, CHARACTER_SIZE
),
new Rect
((int)(x + offsetX
* scale
),
(int)y,
(int)(x +
(offsetX + CHARACTER_SIZE
) * scale
),
(int)(y + CHARACTER_SIZE
* scale
)), paint
);
canvas.
restore();
}
}
private int getCharacterIndex
(char character
) {
for (int i =
0; i
< characters.
length; i++
) {
if (character == characters
[i
])
return i
;
}
return -
1;
}
public void setRotation
(float degrees
) {
mRotation = degrees
;
}
public void setColor
(int color
) {
mColor = color
;
}
}