0

我有一个由几个饼片组成的 CustomPieView。我必须在每个馅饼部分的中间画一些东西。

//在Activity的onCreate方法里面

public void onCreate(Bundle savedInstanceState){

    int size = 400;
    int bgColor = 0xffa11b1;
    ViewPieChart piechart = (ViewPieChart) findViewById(R.id.pieChartView);
    piechart.setCallBack(this);
    piechart.setLayoutParams(new LayoutParams(size, size));
    piechart.setGeometry(size, size, 2, 2, 2, 2, 2130837504);
    piechart.setSkinparams(bgColor);
    piechart.setData(piedata, maxCount);
    piechart.invalidate();

}

//CustomPieView 扩展视图

public void setGeometry(int width, int height, int gapleft, int gapright, int gaptop, int gapbottom, int overlayid) {

    mWidth = width;
    mHeight = height;
    mGapleft = gapleft;
    mGapright = gapright;
    mGapBottm = gapbottom;
    mGapTop = gaptop;
}

受保护的无效onDraw(帆布画布){

    canvas.drawColor(Color.DKGRAY);
    mBagpaints.setAntiAlias(true);
    mBagpaints.setStyle(Paint.Style.FILL);
    mBagpaints.setColor(0x88FF0000);
    mBagpaints.setStrokeWidth(0.0f);
    mLinePaints.setAntiAlias(true);
    mLinePaints.setColor(Color.WHITE);
    mLinePaints.setStrokeWidth(3.0f);
    mLinePaints.setStyle(Paint.Style.STROKE);       
    sLinePaint.setAntiAlias(true);
    sLinePaint.setColor(Color.BLACK);
    sLinePaint.setStrokeWidth(3.0f);
    sLinePaint.setStyle(Paint.Style.STROKE);

    RectF mOvals = new RectF(mGapleft, mGapTop, mWidth - mGapright, mHeight
            - mGapBottm);
    mStart = START_INC;
    PieDetailsItem item;
    for (int i = 0; i < mdataArray.size(); i++) {
        item = (PieDetailsItem) mdataArray.get(i);
        mBagpaints.setColor(item.color);
        mSweep = (float) 360* ((float) item.count / (float) mMaxConnection);
        canvas.drawArc(mOvals, mStart, mSweep, true, mBagpaints);
        canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);

// 下面的函数将全局变量 cSegX、cSegY 设置为段的中心。不工作!

        calculateMidPointOfPieSegment(mSweep);
        canvas.drawPoint(cSegX, cSegY, sLinePaint);
        mStart = mStart + mSweep;
    }
    mState = IS_DRAW;
    callBack.onDrawFinished(null);
}

private float calculateRadius(){
float width = mWidth/2;
float height = mHeight/2;
    if(width < height){
        return width;
    }else{
        return height;
    }
}

private void calculateMidPointOfPieSegment(float sweepAngle){
    cSegX = (float)((calculateRadius()/2)*Math.cos(Math.toRadians(sweepAngle/2))+(mWidth/2));
    cSegY = (float)((calculateRadius()/2)*Math.sin(Math.toRadians(sweepAngle/2))+(mHeight/2));
}

只需看下图中的点,它必须位于每个弧的中心。 截屏

参考。

完整来源

4

1 回答 1

1

让我知道这是否可以解决问题。这是您要实现的基本逻辑。

    //rimPoint = (rX, rY)
    //centerPoint = (cX,cY)
    //theta is the angle of the the midpoint measured anticlockwise from the x axis, 
    //or the average of the two angles making up the slice(measured anticlockwise from 
    //the x axis)
    private void calculateMidPointOfPieSegment(cX,cY, theta)
    {
       float rX = cX + calculateRadius()*(cos(theta))
       float rY = cY + calculateRadius()*(sin(theta))

       cSegX = (rX+cX)/2;
       cCegY = (rY+cY)/2;
    }

如果 theta 不是弧度,则将其替换为 theta*Math.PI/180

于 2013-08-18T20:34:26.867 回答