我想在饼图项目上显示标签。请指导我。

Main.java:-

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        PieDetailsItem item;
       int maxCount=0;
        int itemCount=0;
        int items[]={2,6,0};
        int colors[]={-6777216,-16776961,-16711681};
       String itemslabel[]={" vauesr ur 100"," vauesr ur 200"," vauesr ur 300"};
        for(int i=0;i<items.length;i++)
        {
        itemCount=items[i];
        item=new PieDetailsItem();
        item.count=itemCount;
        item.label=itemslabel[i];
        item.color=colors[i];
        piedata.add(item);
       maxCount=maxCount+itemCount;
        }
        int size=155;
        int BgColor=0xffa11b1;
       Bitmap mBaggroundImage=Bitmap.createBitmap(size,size,Bitmap.Config.ARGB_8888);
        View_PieChart piechart=new View_PieChart(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();
        piechart.draw(new Canvas(mBaggroundImage));
        piechart=null;
        ImageView mImageView=new ImageView(this);
        mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mImageView.setBackgroundColor(BgColor);
        mImageView.setImageBitmap(mBaggroundImage);
        LinearLayout finalLayout=(LinearLayout)findViewById(R.id.pie_container);
        finalLayout.addView(mImageView);
    }


View_piechart.java:-

公共View_PieChart(上下文上下文){
        超级(上下文);
        Log.w(“ single cons”,“ single cons”);
    }

public View_PieChart(Context context, AttributeSet attr) {
    super(context, attr);
    Log.w(" double cons ", " double cons");
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mState != IS_READY_TO_DRAW) {
        return;
    }
    canvas.drawColor(mBgcolor);
    mBagpaints.setAntiAlias(true);
    mBagpaints.setStyle(Paint.Style.FILL);
    mBagpaints.setColor(0x88FF0000);
    mBagpaints.setStrokeWidth(0.0f);
    mLinePaints.setAntiAlias(true);
    mLinePaints.setColor(0xff000000);
    mLinePaints.setStrokeWidth(3.0f);
    mLinePaints.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);
        mStart = mStart + mSweep;
    }

    mState = IS_DRAW;
}

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;

}

public void setSkinparams(int bgcolor) {
    Log.w(" Set bg color  : ", bgcolor + "");
    mBgcolor = bgcolor;
}

public void setData(List<PieDetailsItem> data, int maxconnection) {
    mdataArray = data;
    mMaxConnection = maxconnection;
    Log.w(" Max Connection  ", maxconnection + " " + "  Adataarray :"
            + data.toString());
    mState = IS_READY_TO_DRAW;
}

public void setState(int state) {
    mState = state;
}

public int getColorValues(int index) {
    if (mdataArray == null) {
        return 0;
    }

    else if (index < 0)
        return ((PieDetailsItem) mdataArray.get(0)).color;
    else if (index > mdataArray.size())
        return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
    else
        return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;

}


main.xml




>

<LinearLayout android:id="@+id/pie_container"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"

                    android:orientation="vertical" >
</LinearLayout>

最佳答案

您将不使用TextView,而只是将文本绘制在饼图的正上方。

参考此:
Android Canvas.drawText

09-30 11:20