我正在创建一个我想在图像上绘制文字的应用。此文本应完全根据图像上的数字定位。我确切地知道我想在原始img中绘制哪个像素。但是,在执行此操作时,文本的位置和大小会因屏幕而异。
有没有办法在位图上绘制,与屏幕尺寸无关? 提前谢谢!
代码:
...
Bitmap imgFront = BitmapFactory.decodeResource(getResources(),
R.drawable.card_front,options);
List<StringDrawer> sds = new LinkedList<StringDrawer>();
ImageGenerator ig = new ImageGenerator(imgFront);
ig.process(sds);
BitmapDrawable bgd = new BitmapDrawable(getResources(),imgFront);
v.setBackgroundDrawable(bgd);
...
public class ImageGenerator {
Bitmap image;
public ImageGenerator(Bitmap image){
this.image = image;
}
public void process(List<StringDrawer> strings) {
Canvas c = new Canvas(image);
for(StringDrawer sd:strings){
sd.draw(c);
}
}
}
public class StringDrawer {
private String text;
private int x, y, size, style;
private int color;
public StringDrawer(int x, int y, int size, int style, int color,
String text) {
this.x = x;
this.y = y;
this.size = size;
this.text = text;
this.style = style;
this.color = color;
}
public void draw(Canvas c) {
c.save();
Paint p = new Paint();
p.setAlpha(0);
p.setTypeface(Typeface.create("Arial", style));
p.setTextSize(size);
p.setColor(color);
c.rotate(90, x, y);
c.drawText(text, x, y, p);
c.restore();
}
}
经过一些阅读并了解dp是什么后,我得出结论:这就是所需要的(放入StringDrawer来转换x,y和大小):
private int pxToDp(int px){
return (int) (px * Resources.getSystem().getDisplayMetrics().density);