【问题标题】:custom view not align centre自定义视图不对齐中心
【发布时间】:2016-02-20 13:38:41
【问题描述】:

我正在画canavasarc,但它总是从左边开始,我应该从中间开始

  float x = 0.25f;
    final float radius = x * (new Float(dpi));
    mRadius = Math.round(radius) + 20;

 mRect = new RectF(
            getWidth() + mStrokeWidth, getWidth() + mStrokeWidth, getWidth() + (mRadius / 2) - mStrokeWidth, getWidth() + (mRadius / 2) - mStrokeWidth
    );
        canvas.drawArc(mRect, lastDegree, mSectionDegree, false, mPaint);

为什么这个视图总是从左边开始,即使我仍然给出了重心

float Degree = 270 + (mGap / 2);
    for (int i = 0; i < mTotalSections; i++) {

        fillColor(i);
        canvas.drawArc(mRect, Degree, mDegree, false, mPaint);
        Degree += mDegree + mGap;
        Paint mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1.setStrokeWidth(1);
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint1.setAntiAlias(true);
        mPaint1.setTextSize(15 * getResources().getDisplayMetrics().density);

        mPaint1.setColor(getResources().getColor(black));

        mPaint1.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, mRect.centerX(), mRect.centerY(), mPaint1);
    }

【问题讨论】:

    标签: android android-canvas android-custom-view


    【解决方案1】:

    我不确定你想做什么,但你的 rect 看起来很奇怪。更像这样的矩形应该会更好:

     mRect = new RectF(
            0 + mStrokeWidth, 0 + mStrokeWidth, getWidth() - mStrokeWidth, getHeight - mStrokeWidth
    );
    

    但是稍微画出你想要的东西和你的角度值会是完美的;)。如果您提供,我将编辑我的答案。

    编辑: 所以如果你想在你的自定义视图中间有一个圆圈,你可以这样做:

     RectF mRect = new RectF(
                50 + mStrokeWidth, 50 + mStrokeWidth, getWidth() -50 - mStrokeWidth, getHeight() -50 - mStrokeWidth
        );
    
        canvas.drawArc(mRect, 0,360,false,paint);
    

    结果(蓝线是我的观点的极限):

    您必须修改 50 以适合您的需求;)。 如果你想要一个以 180 度角粘在边缘的圆弧:

    RectF mRect = new RectF(
                 mStrokeWidth,  mStrokeWidth, getWidth()  - mStrokeWidth, getHeight()  - mStrokeWidth
        );
    
        canvas.drawArc(mRect, 0,180,false,paint);
    

    结果:

    如果您需要其他东西,请告诉我。

    【讨论】:

    • 所以问题是我的圈子总是在左边,我希望它应该居中
    • 此视图不可见..检查我已编辑的 qus
    • 如果你能给我看一张你想要的画就完美了。
    猜你喜欢
    • 2011-06-26
    • 2020-10-14
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多