我在我的項目中使用canvas.drawText(...)
繪製了文本,並使用不同的設備對其進行了測試。Java Android Canvas.drawText(...)在所有屏幕上同樣大
但是,從屏幕到屏幕,文本的大小各不相同。
我試圖乘以getResources().getDisplayMetrics().density
,但它們仍然略有不一樣的大小。
有沒有辦法讓文本在不同的設備上具有相同的大小?
我在我的項目中使用canvas.drawText(...)
繪製了文本,並使用不同的設備對其進行了測試。Java Android Canvas.drawText(...)在所有屏幕上同樣大
但是,從屏幕到屏幕,文本的大小各不相同。
我試圖乘以getResources().getDisplayMetrics().density
,但它們仍然略有不一樣的大小。
有沒有辦法讓文本在不同的設備上具有相同的大小?
嗨,你可以看看這篇文章,http://catchthecows.com/?p=72,我認爲它對你有幫助。 @Override protected void onSizeChanged(int w,int h,int oldw,int oldh){0,0,0,0}。
// save view size
mViewWidth = w;
mViewHeight = h;
// first determine font point size
adjustTextSize();
// then determine width scaling
// this is done in two steps in case the
// point size change affects the width boundary
adjustTextScale();
}
void adjustTextSize() {
mTextPaint.setTextSize(100);
mTextPaint.setTextScaleX(1.0f);
Rect bounds = new Rect();
// ask the paint for the bounding rect if it were to draw this
// text
mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
// get the height that would have been produced
int h = bounds.bottom - bounds.top;
// make the text text up 70% of the height
float target = (float)mViewHeight*.7f;
// figure out what textSize setting would create that height
// of text
float size = ((target/h)*100f);
// and set it into the paint
mTextPaint.setTextSize(size);
}
void adjustTextScale() {
// do calculation with scale of 1.0 (no scale)
mTextPaint.setTextScaleX(1.0f);
Rect bounds = new Rect();
// ask the paint for the bounding rect if it were to draw this
// text.
mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
// determine the width
int w = bounds.right - bounds.left;
// calculate the baseline to use so that the
// entire text is visible including the descenders
int text_h = bounds.bottom-bounds.top;
mTextBaseline=bounds.bottom+((mViewHeight-text_h)/2);
// determine how much to scale the width to fit the view
float xscale = ((float) (mViewWidth-getPaddingLeft()-getPaddingRight()))/w;
// set the scale for the text paint
mTextPaint.setTextScaleX(xscale);
}
@Override
protected void onDraw(Canvas canvas) {
// let the ImageButton paint background as normal
super.onDraw(canvas);
// draw the text
// position is centered on width
// and the baseline is calculated to be positioned from the
// view bottom
canvas.drawText(mText, mViewWidth/2, mViewHeight-mTextBaseline, mTextPaint);
}
這是什麼
getResources()。getDisplayMetrics()。密度
做的是,它使得它看起來在同一屏幕尺寸的不同密度的相似。欣賞你的舉動。
您的問題有兩種解決方案。 1.嘗試使用Large,xlarge和普通佈局。在相應的佈局上相應地調整它們以使其看起來均勻。
Paint類將以像素爲單位的文本大小。如果要直接通過它繪製文本,則需要在調用paint.setTextSize(float size)之前處理單位轉換。始終使用sp或dp單位,並且您可能希望至少包含用於small,normal,large和xlarge屏幕的單獨的dimens.xml資源。
當調用Canvas.drawText()
時,文本大小首先由Paint
對象中的傳入值確定,可通過Paint.setTextSize()
進行設置。文字大小將根據畫布密度自動縮放Canvas
,可以使用Canvas.getDensity()
找到。
當在畫布上繪製的繪畫對象上設置文本大小時,使用單位值dp
或sp
,並讓Canvas處理縮放比例。
當然,你可能需要指定根據屏幕尺寸不同的值:Supporting Multiple Screens
你如何定義你的文字大小? Android內置了可根據屏幕尺寸縮放文字的功能,除非您指定不顯示。 – NominSim
我使用mPaint.setTextSize(mTextSize) – Rasmus
使用'in'表示大小單位 – njzk2