[英]Android: write on an image resource and keep the same size on different devices
I have been looking for days on how to fix my issue and haven't really found an answer. 我一直在寻找如何解决我的问题的日子,但还没有真正找到答案。 So I apologize in advance if there already is a similar question. 因此,如果已经有类似的问题,我先致歉。
My problem is simple. 我的问题很简单。 I want the user to input a name, and this name will be written on an image, in some kind of little box. 我希望用户输入一个名称,这个名称将写在图像上的某种小盒子中。 (The image is then shared in an intent and everything, anyway...) (然后按意向共享图像,并且一切都将共享...)
The image is a png in my drawable folder. 该图像是我的可绘制文件夹中的png。 So it should always have the same size in pixels on any kind of device. 因此,在任何类型的设备上,像素尺寸始终应相同。 I load the png on a bitmap, use canvas on that bitmap, create a paint and myPaint.setTextSize(100)
(setTextSize takes pixels so in this case it is 100 pixels) and then drawText on the canvas using that paint to have the same text size (in pixel) from whatever device is used. 我将png加载到位图上,在该位图上使用画布,创建一个paint和myPaint.setTextSize(100)
(setTextSize占用像素,因此在这种情况下为100像素),然后在画布上使用该paint绘制文本使用任何设备的文字大小(以像素为单位)。
The result is that writing "MyName" on one device fits perfectly, but on a smaller device would go out of the box, and on a larger device also. 结果是,在一个设备上写“ MyName”非常合适,但是在较小的设备上就可以使用,而在较大的设备上也可以使用。
I tried to use dp or sp, even if I knew it wouldn't change anything because we aren't talking about a layout, but a drawable png that has a fixed size.' 我尝试使用dp或sp,即使我知道它不会改变任何东西,因为我们不是在谈论布局,而是具有固定大小的可绘制png。
I have no issue with the position of the text or anything; 我对文字或其他内容的位置没有任何疑问; only the size of it. 只有它的大小。
So anyway, I don't know where this change of textsize from one device to another comes from. 因此,无论如何,我不知道这种文本大小从一台设备到另一台设备的变化来自何处。 Maybe I am not taking care of some kind of detail. 也许我不在乎某些细节。 I hope you guys can help me out. 我希望你们能帮助我。
Context context = getBaseContext();
Resources resources = context.getResources();
Bitmap card = BitmapFactory.decodeResource(resources, R.drawable.card);
android.graphics.Bitmap.Config bitmapConfig = card.getConfig();
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// Converting to mutable
card = card.copy(bitmapConfig, true);
Canvas canvas = new Canvas(card);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color
paint.setColor(Color.WHITE);
paint.setFakeBoldText(true);
paint.setTextAlign(Paint.Align.CENTER);
// text size in pixels
paint.setTextSize(100);
// Name Position
float xName = (float) (0.744 * card.getWidth() );
float yName = (float) (0.680424 * (card.getHeight() - (paint.ascent()/2)));
String name = editName.getText().toString();
canvas.drawText(name, xName, yName, paint);
Try this. 尝试这个。
/**
* Sets the text size for a Paint object so a given string of text will be a
* given width.
*
* @param paint
* the Paint to set the text size for
* @param desiredWidth
* the desired width
* @param text
* the text that should be that width
*/
private static void setTextSizeForWidth(Paint paint, float desiredWidth,
String text) {
// Pick a reasonably large value for the test. Larger values produce
// more accurate results, but may cause problems with hardware
// acceleration. But there are workarounds for that, too; refer to
// http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
final float testTextSize = 48f;
// Get the bounds of the text, using our testTextSize.
paint.setTextSize(testTextSize);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
// Calculate the desired size as a proportion of our testTextSize.
float desiredTextSize = testTextSize * desiredWidth / bounds.width();
// Set the paint for that size.
paint.setTextSize(desiredTextSize);
}
I was able to fix this issue by using density. 我能够通过使用密度来解决此问题。 Not really sure how it works, but it gets the job done so... 不确定如何运作,但可以完成工作...
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
paint.setTextSize(30 * scale);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.