在行图android

时间:2018-02-06 07:40:11

标签: canvas graphics line android-canvas draw

我正在尝试打印'关闭'在折线图中没有数据的范围内的矩形中。我在图表中数据的结束点和重新呈现数据的第一个点之间绘制一个矩形。

但我不能写'关闭'在每部手机的整个矩形的中间。在某些手机上关闭'在矩形的下方或上方。

也许我可以通过这张照片更好地说出我的意思: Maybe I can tell what I mean better with this picture

我将图表和矩形绘制到代码中,如下所示:

int i;
    for( i=0; i<totalYvalues.size() ;i++)
    {

        if(totalYvalues.get(i)>=110)
        {
            mPaint.setColor(getResources().getColor(R.color.viitaButtonGreen));
        }
        else if( totalYvalues.get(i)>=80 &&  totalYvalues.get(i)<110)
        {
            mPaint.setColor(GRAY);
        }
        else if( totalYvalues.get(i)<80)
        {
            mPaint.setColor(getResources().getColor(R.color.colorAccent));
        }

        if(i == first) //no data
        {
            canvas.drawRect(xVals.get(i), 70, xVals.get(last),totalYvalues.get(last)+30, rectPaint); // ilk parametre genişliği artırıyor, ikinci yüksekliği artırıyor
            canvas.save();
            drawText(canvas, xVals, totalYvalues, i);
            i= last;
        }
        else
        {  // here i am drawing graph with points. (for colorful line chart)
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle( xVals.get(i), totalYvalues.get(i), 3f, mPaint);
        }
    }

并且drawText(..)方法如下:

public  void drawText(Canvas canvas, ArrayList<Float> xVals, ArrayList<Integer> totalYvalues, int i)
{
    // Text coordinates as follows
    Float x = xVals.get(first)  ;
    Integer y = ( totalYvalues.get(last)+30 +  totalYvalues.get(first) ) /2;

    mPaint.setColor(GRAY);
        mPaint.setTextSize(25);

    canvas.translate(canvas.getWidth(), canvas.getHeight());
    canvas.rotate(-180f);
    int cx = canvas.getWidth()/ 2;
    int cy =canvas.getHeight() / 2;
    canvas.scale(-1f, 1f, cx, cy);

    canvas.translate(x, y);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawText("OFF", 0, y, mPaint);
    mPaint.setStyle(Paint.Style.STROKE);

    canvas.restore();
    canvas.save();
    canvas.translate(0, canvas.getHeight());
    canvas.scale(1,-1);
    canvas.restore();
    canvas.save();

}

如何确保矩形中的文字正好位于每部手机的中间?如果你有帮助,我会非常感激。

0 个答案:

没有答案