android绘制文本重叠

标签 android paint ondraw

尝试使用开源代码在 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/

相关文章:

java - 如何将油漆(图形g)添加到另一个类中?

android - 如何在修改后更新 Android View ?

java - 如何更改 ViewPager android 中单个选项卡的宽度?

android - Cordova Android 本地视频播放曾经可以工作;现在没有

android - 无法使用 Volley 显示两个自定义 ListView

android - 如何使 UI 阴影到任何 View

java - 以编程方式在Android上绘制虚线

java - 使用线条创建填充桶工具

java - Android自定义 View : getting null references on every class members in the onDraw method

android - 我是否需要使用无效调用,如果需要,在哪里?