android油漆文字重叠

时间:2014-01-17 08:43:13

标签: android paint ondraw

尝试使用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);
}

1 个答案:

答案 0 :(得分:0)

这是很久以前问过的。我正在写对我有用的解决此问题的方法。

paint.setTextScaleX(1.50f);