如何在画布中心显示TextView?

时间:2016-09-21 12:39:07

标签: android android-studio canvas

我使用TextViewcanvas置于layout内。 我的第一个问题是如何集中它? 因为每次我width/2height /2都会给它一个偏移量。

其次,为什么angleTextView.setGravity(Gravity.CENTER);无效?

这是我正在做什么来绘制我的TextView任何建议?

的onDraw:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        if (pathsArray.size() == 0) { // initlizes the init
            init();
        }

        // draws the first circle
        canvas.drawCircle(width / 2f, height / 2f, OUTER_RING_DIAMETER, firstCirclePaint);

        //go through the array and paints the corisponding cells
        for (int i = 0; i < pathsArray.size(); i++) {

            if (pathsArray.get(i).state == 1) { // scanned
                canvas.drawPath(pathsArray.get(i), fillColorForSlicesNoHbs);
                canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);

            } else if (pathsArray.get(i).state == 2) { // found hbs
                canvas.drawPath(pathsArray.get(i), fillColorForSlicesWithHbs);
                canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);

            } else {
                canvas.drawPath(pathsArray.get(i), mainPaintForSlices);
                canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);
            }
        }


        //paint the outer circles
        canvas.drawCircle(width / 2f, height / 2f, OUTER_RING_DIAMETER, outSideCirclePaint);

        canvas.drawCircle(width / 2f, height / 2f, (OUTER_RING_DIAMETER - (RINGS_STEP)), circleOutlinesPaint);

        canvas.drawCircle(width / 2f, height / 2f, (OUTER_RING_DIAMETER - (RINGS_STEP * 2)), circleOutlinesPaint);

        canvas.drawCircle(width / 2f, height / 2f, (OUTER_RING_DIAMETER - (RINGS_STEP * 3)), firstCirclePaint);


        canvas.drawPath(highlightedPath, highlitedCirclePaint);

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

            if (pathsArray.get(i).elvation == highlightedPath.elvation) {

                if (pathsArray.get(i).state == 1) { // scanned
                    canvas.drawPath(pathsArray.get(i), fillColorForSlicesNoHbs);
                    canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);

                } else if (pathsArray.get(i).state == 2) { // found hbs
                    canvas.drawPath(pathsArray.get(i), fillColorForSlicesWithHbs);
                    canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);

                } else {
                    canvas.drawPath(pathsArray.get(i), circleOutlinesPaint);
                }

            }
        }


        //draws the dark opacity on top of everything else
        canvas.drawPath(lowlightedPathOuter, lowlitedCirclePaint);
        canvas.drawPath(lowlightedPathInner, lowlitedCirclePaint);


        // draw the needle
        // draw the circle of the current elevantion.
        alignmentSlice staticNeedle = getSlicesPaths(width / 2f, height / 2f, (OUTER_RING_DIAMETER - (RINGS_STEP * 3)), OUTER_RING_DIAMETER + 100, 0, 0.6f, 0);
        alignmentSlice mainNeedle = getSlicesPaths(width / 2f, height / 2f, (OUTER_RING_DIAMETER - (RINGS_STEP * 3)), OUTER_RING_DIAMETER + 100, needleAngle, 0.6f, 0);

        canvas.drawPath(staticNeedle, paintSecondaryNeedle);
        canvas.drawPath(mainNeedle, paintMainNeedle);


        //puts a text view
        RelativeLayout layout = new RelativeLayout(getContext());

        angleTextView = new TextView(getContext());
        angleTextView.setVisibility(View.VISIBLE);
        angleTextView.setTextSize(width/35);
        angleTextView.setGravity(Gravity.CENTER);
        angleTextView.setTextColor(Color.WHITE);
        angleTextView.setText(angleText);

        layout.addView(angleTextView);

        layout.measure(canvas.getWidth(), canvas.getHeight());
        layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());

        // To place the text view somewhere specific:
        canvas.translate((width/2)-(width/15), (height/2)-(height/27));


        layout.draw(canvas);

    }

2 个答案:

答案 0 :(得分:3)

textview更改为:

Paint textPaint = new Paint();
        textPaint.setColor(getResources().getColor(R.color.alignementWhite));
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(width/10);

        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;

        canvas.drawText(angleText, xPos, yPos, textPaint);

答案 1 :(得分:2)

如果您要使用layout.setGravity(Gravity.CENTER),则需要在父级上进行设置。在这种情况下textview。如果您想在layout params上进行设置,则必须使用LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); params.gravity = Gravity.CENTER; angleTextView.setLayoutParams(params);

{{1}}