android - 如何在 Android 中使用 Canvas 设计图表?

标签 android graphics charts android-canvas

我想在 android 中使用 Canvas 或其他方法设计以下图表请帮助我...

如图所示,我想根据输入的百分比 Ex 绘制圆的各个部分。圆圈的 50% 部分为红色,50% 部分为奶油色,并带有一些文字,如图所示。

As Show in image i want to draw sections of circle as per input in percentage Ex. 50% part of circle is red color and 50% part in cream color with some text as show in image.

最佳答案

可能会有帮助,

public class View_PieChart extends View {
    private static final int WAIT = 0;
    private static final int IS_READY_TO_DRAW = 1;
    private static final int IS_DRAW = 2;
    private static final float START_INC = 30;
    private Paint mBgPaints   = new Paint();
    private Paint mLinePaints = new Paint();
    private int   mOverlayId;
    private int   mWidth;
    private int   mHeight;
    private int   mGapLeft;
    private int   mGapRight;
    private int   mGapTop;
    private int   mGapBottom;
    private int   mBgColor;
    private int   mState = WAIT;
    private float mStart;
    private float mSweep;
    private int   mMaxConnection;
    private List<PieDetailsItem> mDataArray;
    //--------------------------------------------------------------------------------------
    public View_PieChart (Context context){
        super(context);
    }
    //--------------------------------------------------------------------------------------
    public View_PieChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //--------------------------------------------------------------------------------------
    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //------------------------------------------------------
        if (mState != IS_READY_TO_DRAW) return;
        canvas.drawColor(mBgColor);
        //------------------------------------------------------
        mBgPaints.setAntiAlias(true);
        mBgPaints.setStyle(Paint.Style.FILL);
        mBgPaints.setColor(0x88FF0000);
        mBgPaints.setStrokeWidth(0.5f);
        //------------------------------------------------------
        mLinePaints.setAntiAlias(true);
        mLinePaints.setStyle(Paint.Style.STROKE);
        mLinePaints.setColor(0xff000000);
        mLinePaints.setStrokeWidth(0.5f);
        //------------------------------------------------------
        RectF mOvals = new RectF( mGapLeft, mGapTop, mWidth - mGapRight, mHeight - mGapBottom);
        //------------------------------------------------------
        mStart = START_INC;
        PieDetailsItem Item;
        for (int i = 0; i < mDataArray.size(); i++) {
            Item = (PieDetailsItem) mDataArray.get(i);
            mBgPaints.setColor(Item.Color);
            mSweep = (float) 360 * ( (float)Item.Count / (float)mMaxConnection );
            canvas.drawArc(mOvals, mStart, mSweep, true, mBgPaints);
            canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);
            mStart += mSweep;
        }
        //------------------------------------------------------
        Options options = new BitmapFactory.Options();
        options.inScaled = false;
        /*Bitmap OverlayBitmap = BitmapFactory.decodeResource(getResources(), mOverlayId, options);
        canvas.drawBitmap(OverlayBitmap, 0.0f, 0.0f, null);*/
        //------------------------------------------------------
        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;
        mGapTop    = GapTop;
        mGapBottom = GapBottom;
        mOverlayId = OverlayId;
    }
    //--------------------------------------------------------------------------------------
    public void setSkinParams(int bgColor) {
        mBgColor   = bgColor;
    }
    //--------------------------------------------------------------------------------------
    public void setData(List<PieDetailsItem> data, int MaxConnection) {
        mDataArray = data;
        mMaxConnection = MaxConnection;
        mState = IS_READY_TO_DRAW;
    }
    //--------------------------------------------------------------------------------------
    public void setState(int State) {
        mState = State;
    }
    //--------------------------------------------------------------------------------------
    public int getColorValue( int Index ) {
        if (mDataArray == null) return 0;
        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;
        }
    }
}

关于android - 如何在 Android 中使用 Canvas 设计图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803188/

相关文章:

javascript - 是否可以在图表 js 中使用 mouseenter 和 mouseleave 事件?

MATLAB 玫瑰图 : increasing radial increments?

iphone - iPhone开发的矢量绘图工具

android - afreechart Android eclipse esample

java - Firestore 查询数据顺序

无法将 BMP 图像放入矩形 OpenGL

java - 在Java中绘制高斯曲线

java - 尝试绘制动画位图,但不起作用

java - 如何实现简单的edittext语法高亮方法?

android - 带有 completeSmart() 函数的 IntelliJ 实时模板错误