Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在画布内部的中心显示TextView?_Android_Android Studio_Canvas - Fatal编程技术网

Android 如何在画布内部的中心显示TextView?

Android 如何在画布内部的中心显示TextView?,android,android-studio,canvas,Android,Android Studio,Canvas,我有一个文本视图,我使用布局将它放在画布中。 我的第一个问题是如何将其居中? 因为每次我做width/2和height/2都会给它一个偏移量 其次,为什么我的angleTextView.setGravity(Gravity.CENTER)是不工作 下面是我在做什么来绘制我的TextView有什么建议吗 昂德劳: @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);

我有一个
文本视图
,我使用
布局将它放在
画布
中。 我的第一个问题是如何将其居中? 因为每次我做
width/2
height/2
都会给它一个偏移量

其次,为什么我的
angleTextView.setGravity(Gravity.CENTER)是不工作

下面是我在做什么来绘制我的
TextView
有什么建议吗

昂德劳:

@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);

    }
@覆盖
受保护的void onDraw(画布){
super.onDraw(帆布);
if(pathsArray.size()==0){//初始化初始化
init();
}
//画第一个圆
帆布画圈(宽/2f,高/2f,外圈直径,第一圈漆);
//遍历数组并绘制相应的单元格
对于(int i=0;i
如果要使用
重力
,则需要在父项上设置重力。在这种情况下,
layout.setGravity(Gravity.CENTER)
。如果要在
textview
上进行设置,必须使用
布局参数

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;

angleTextView.setLayoutParams(params);

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);

什么样的补偿?你试过
(width+textview.getMeasuredWidth())/2
(height+textview.getMeasuredHeight())/2
?是的,我试过了,它真的会将它偏移到右下角。你正在创建一个新的
RelativeLayout
并在
onDraw
中调用它的
addView
方法???你知道调用
onDraw
的频率吗?这是我读到的唯一一种在画布中放置文本视图的方法,你有更好的方法吗?愚蠢的问题-当你打印
textview.getX()
textview.getY()
时,它们是否在所需的位置(画布的宽度/2和高度/2?)我应该导入什么layoutparams?是吗,线性布局,布局公园?或者RelativeLayout.layoutparmas?实际上我不确定它在这种情况下是否重要,但我会使用RelativeLayout使其与父级匹配。它仍然没有修复它,仍然是相同的问题