gpt4 book ai didi

android绘制文本重叠

转载 作者:行者123 更新时间:2023-12-03 17:51:07 29 4
gpt4 key购买 nike

尝试使用开源代码在 android 中使用 Paint 和 ondraw() 绘制车辆仪表。仪表工作得很好,但在 android 4 及更高版本上存在问题。之后,通过设置 setLinearText(true) 对 Paint 对象进行了更改,从而使事情正常进行。但是现在仪表中的文本已经开始重叠,并且如图所示,事情看起来很模糊。 enter image description here

下面是用于在屏幕上绘制仪表、文本和线条的源代码。需要帮助来绘制不重叠的文本。

    private void initDrawingTools() {
rimRect = new RectF(0.1f, 0.1f, 0.9f, 0.9f);

// the linear gradient is a bit skewed for realism
rimPaint = new Paint();
rimPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
rimPaint.setShader(new LinearGradient(0.40f, 0.0f, 0.60f, 1.0f,
Color.rgb(0xf0, 0xf5, 0xf0),
Color.rgb(0x30, 0x31, 0x30),
Shader.TileMode.CLAMP));

rimCirclePaint = new Paint();
rimCirclePaint.setAntiAlias(true);
rimCirclePaint.setStyle(Paint.Style.STROKE);
rimCirclePaint.setColor(Color.argb(0x4f, 0x33, 0x36, 0x33));
rimCirclePaint.setStrokeWidth(0.005f);

float rimSize = 0.02f;
faceRect = new RectF();
faceRect.set(rimRect.left + rimSize, rimRect.top + rimSize,
rimRect.right - rimSize, rimRect.bottom - rimSize);

faceTexture = BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.plastic);
BitmapShader paperShader = new BitmapShader(faceTexture,
Shader.TileMode.MIRROR,
Shader.TileMode.MIRROR);
Matrix paperMatrix = new Matrix();
facePaint = new Paint();
facePaint.setFilterBitmap(true);
paperMatrix.setScale(1.0f / faceTexture.getWidth(),
1.0f / faceTexture.getHeight());
paperShader.setLocalMatrix(paperMatrix);
facePaint.setStyle(Paint.Style.FILL);
facePaint.setShader(paperShader);

rimShadowPaint = new Paint();
rimShadowPaint.setShader(new RadialGradient(0.5f, 0.5f, faceRect.width() / 2.0f,
new int[] { 0x00000000, 0x00000500, 0x50000500 },
new float[] { 0.96f, 0.96f, 0.99f },
Shader.TileMode.MIRROR));
rimShadowPaint.setStyle(Paint.Style.FILL);

scalePaint = new Paint();
scalePaint.setStyle(Paint.Style.STROKE);
scalePaint.setColor(0x9f004d0f);
scalePaint.setStrokeWidth(0.005f);
scalePaint.setAntiAlias(true);


float pixel= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
1, getResources().getDisplayMetrics());
scalePaint.setTextSize(0.045f);
//scalePaint.setTextSize(pixel);
scalePaint.setTypeface(Typeface.SANS_SERIF);
scalePaint.setTextScaleX(1.0f);
scalePaint.setTextAlign(Paint.Align.CENTER);
scalePaint.setLinearText(true);
scalePaint.setFlags(Paint.LINEAR_TEXT_FLAG);


float scalePosition = 0.10f;
scaleRect = new RectF();
scaleRect.set(faceRect.left + scalePosition, faceRect.top + scalePosition,
faceRect.right - scalePosition, faceRect.bottom - scalePosition);

titlePaint = new Paint();
titlePaint.setColor(0xaf946109);
titlePaint.setAntiAlias(true);
titlePaint.setTypeface(Typeface.DEFAULT_BOLD);
titlePaint.setTextAlign(Paint.Align.CENTER);
titlePaint.setTextSize(0.05f);
titlePaint.setTextScaleX(0.8f);
titlePaint.setLinearText(true);
titlePaint.setFlags(Paint.LINEAR_TEXT_FLAG);

titlePath = new Path();
titlePath.addArc(new RectF(0.24f, 0.24f, 0.76f, 0.76f), -180.0f, -180.0f);

paramPaint = new Paint();
paramPaint.setColor(0xaf946109);
paramPaint.setAntiAlias(true);
paramPaint.setTypeface(Typeface.DEFAULT_BOLD);
paramPaint.setTextAlign(Paint.Align.CENTER);
paramPaint.setTextSize(0.05f);
paramPaint.setTextScaleX(0.8f);
paramPaint.setLinearText(true);
paramPaint.setFlags(Paint.LINEAR_TEXT_FLAG);

logoPaint = new Paint();
logoPaint.setFilterBitmap(true);
logo = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.logo);
logoMatrix = new Matrix();
logoScale = (1.0f / logo.getWidth()) * 0.3f;
logoMatrix.setScale(logoScale, logoScale);

handPaint = new Paint();
handPaint.setAntiAlias(true);
handPaint.setColor(0xff392f2c);
handPaint.setShadowLayer(0.01f, -0.005f, -0.005f, 0x7f000000);
handPaint.setStyle(Paint.Style.FILL);

handPath = new Path();
handPath.moveTo(0.5f, 0.5f + 0.2f);
handPath.lineTo(0.5f - 0.010f, 0.5f + 0.2f - 0.007f);
handPath.lineTo(0.5f - 0.002f, 0.5f - 0.32f);
handPath.lineTo(0.5f + 0.002f, 0.5f - 0.32f);
handPath.lineTo(0.5f + 0.010f, 0.5f + 0.2f - 0.007f);
handPath.lineTo(0.5f, 0.5f + 0.2f);
handPath.addCircle(0.5f, 0.5f, 0.025f, Path.Direction.CW);

handScrewPaint = new Paint();
handScrewPaint.setAntiAlias(true);
handScrewPaint.setColor(0xff493f3c);
handScrewPaint.setStyle(Paint.Style.FILL);

backgroundPaint = new Paint();
backgroundPaint.setFilterBitmap(true);
}

最佳答案

这是很久以前被问到的。我正在写对我有用的东西来解决这个问题。

paint.setTextScaleX(1.50f);

关于android绘制文本重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21181371/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com