如何解决可绘制资源在不同的屏幕密度上缩放
我正在尝试在 android 中创建文本贴纸。我正在我的一部手机上创建一个文本贴纸,然后将文本大小保存在 DP 中。 然后在另一部手机上,我使用相同的 DP 加载相同的文本并将 DP 值转换为像素。然后调用方法
TextPaint.setTextSize(pixelValue)
但是两部手机的输出不同。我也试过用 SP 替换 DP,但似乎不是解决方案。
还有一件事,我正在根据可绘制(res)宽度高度确定视图的宽度和高度。
这是那个drawable的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<size android:width="200dp" android:height="50dp" />
</shape>
但这似乎不是问题,因为值在可绘制文件中的 DP 中。
public DynamicTextSticker(@NonNull Context context,@Nullable Drawable drawable,String text) {
this.context = context;
this.drawable = drawable;
if (drawable == null) {
this.drawable = ContextCompat.getDrawable(context,R.drawable.sticker_transparent_background);
}
textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
minTextSizePixels = DensityUtils.px2dp(context,6);
maxTextSizePixels = DensityUtils.px2dp(context,32);
alignment = Layout.Alignment.ALIGN_CENTER;
textPaint.setTextSize(maxTextSizePixels);
realBounds = new Rect(0,getWidth(),getHeight());
textRect = new Rect(0,getHeight());
}
draw method
@Override public void draw(@NonNull Canvas canvas) {
Matrix matrix = getMatrix();
canvas.save();
canvas.concat(matrix);
if (drawable != null) {
drawable.setBounds(realBounds);
drawable.draw(canvas);
}
canvas.restore();
canvas.save();
canvas.concat(matrix);
if (textRect.width() == getWidth()) {
int dy = getHeight() / 2 - staticLayout.getHeight() / 2;
// center vertical
canvas.translate(0,dy);
} else {
int dx = textRect.left;
int dy = textRect.top + textRect.height() / 2 - staticLayout.getHeight() / 2;
canvas.translate(dx,dy);
}
staticLayout.draw(canvas);
canvas.restore();
}
here is the resize method where the static layout is initialized
final int availableHeightPixels = textRect.height();
final int availableWidthPixels = textRect.width();
final CharSequence text = getText();
// Safety check
// (Do not resize if the view does not have dimensions or if there is no text)
if (text == null
|| text.length() <= 0
|| availableHeightPixels <= 0
|| availableWidthPixels <= 0
|| maxTextSizePixels <= 0) {
return this;
}
float targetTextSizePixels = maxTextSizePixels;
int targetTextHeightPixels =
getTextHeightPixels(text,availableWidthPixels,targetTextSizePixels);
// Until we either fit within our TextView
// or we have reached our minimum text size,// incrementally try smaller sizes
while (targetTextHeightPixels > availableHeightPixels
&& targetTextSizePixels > minTextSizePixels) {
targetTextSizePixels = Math.max(targetTextSizePixels - 2,minTextSizePixels);
targetTextHeightPixels =
getTextHeightPixels(text,targetTextSizePixels);
}
// If we have reached our minimum text size and the text still doesn't fit,// append an ellipsis
// (NOTE: Auto-ellipsize doesn't work hence why we have to do it here)
if (targetTextSizePixels == minTextSizePixels
&& targetTextHeightPixels > availableHeightPixels) {
// Make a copy of the original TextPaint object for measuring
TextPaint textPaintcopy = new TextPaint(textPaint);
textPaintcopy.setTextSize(targetTextSizePixels);
// Measure using a StaticLayout instance
StaticLayout staticLayout =
new StaticLayout(text,textPaintcopy,Layout.Alignment.ALIGN_norMAL,linespacingMultiplier,linespacingExtra,false);
// Check that we have a least one line of rendered text
if (staticLayout.getLineCount() > 0) {
// Since the line at the specific vertical position would be cut off,// we must trim up to the prevIoUs line and add an ellipsis
int lastLine = staticLayout.getLineForVertical(availableHeightPixels) - 1;
if (lastLine >= 0) {
int startOffset = staticLayout.getLinestart(lastLine);
int endOffset = staticLayout.getLineEnd(lastLine);
float linewidthPixels = staticLayout.getlinewidth(lastLine);
float ellipseWidth = textPaintcopy.measureText(mEllipsis);
// Trim characters off until we have enough room to draw the ellipsis
while (availableWidthPixels < linewidthPixels + ellipseWidth) {
endOffset--;
linewidthPixels =
textPaintcopy.measureText(text.subSequence(startOffset,endOffset + 1).toString());
}
setText(text.subSequence(0,endOffset) + mEllipsis);
}
}
}
textPaint.setTextSize(targetTextSizePixels);
this.targetTextSizePixel = targetTextSizePixels;
staticLayout =
new StaticLayout(this.text,textPaint,textRect.width(),alignment,true);
return this;
here is the getWidth and getHeight method
@Override public int getWidth() {
return drawable.getIntrinsicWidth();
}
@Override public int getHeight() {
return drawable.getIntrinsicHeight();
}
I think the problem is not with text size. but the problem might be with drawable. But I don't kNow what possibly Could go wrong with that,as I have used DP unit everywhere.
**If i forget to add any code please tell me I will attach it as well**
I am new to android so I am not sure the reason behind this problem.
Thanks for your help.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。