截断甜甜圈图表中的文字android

时间:2018-02-22 13:00:21

标签: java android path paint

我根据自定义要求创建了一个圆环图。但问题是底部的文本和侧面文本被截断,这是不合适的。让我知道如何解决它?

这是代码。

    public class Chart_daily extends View {


private static final String TAG = Chart_daily.class.getName();
    private static final String TEXT_SELLIN = "SELL IN";
    private static final String TEXT_SELLOUT = "SELL OUT";
    private static final String TEXT_GROWTH = "GROWTH";
    Paint paint;
    Paint shadowPaint;
    Path myPath;
    Path shadowPath;
    RectF outterCircle;
    RectF innerCircle;
    RectF shadowRectF;
    RectF textOuterCircle;
    RectF textInnerCircle;
    String numerator;
    String denominator;
    PerformanceData performanceData;
    private float radius;



       public Chart_daily(Context context, ResponseBody responseBody) {
            super(context);
            this.radius = 
 //the value of the donut_height is 132.5dp
(getContext().getResources().getDimensionPixelSize(R.dimen.donut_height)) / 2;
            this.numerator = "";
            this.performanceData = responseBody.getPerformanceData();
            if (null != performanceData.getTotRetailerCnt()) {
                if (performanceData.getTotRetailerCnt().toString().equalsIgnoreCase("null")) {
                    this.denominator = " /";
                } else {
                    this.denominator = " /" + performanceData.getTotRetailerCnt();
                }

            } else {
                this.denominator = " /";
            }

            paint = new Paint();
            paint.setDither(true);
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(radius / 14.0f);

            shadowPaint = new Paint();
            shadowPaint.setColor(0xf0000000);
            shadowPaint.setStyle(Paint.Style.STROKE);
            shadowPaint.setAntiAlias(true);
            shadowPaint.setStrokeWidth(6.0f);
            shadowPaint.setMaskFilter(new BlurMaskFilter(4, BlurMaskFilter.Blur.SOLID));
            myPath = new Path();
            shadowPath = new Path();
            outterCircle = new RectF();
            innerCircle = new RectF();
            shadowRectF = new RectF();
            textInnerCircle = new RectF();
            textOuterCircle = new RectF();
    //        radius=300;
            float adjust = (.019f * radius);
            //********************************************************************************************

            adjust = .175f * radius;//0.038
            outterCircle.set(adjust, adjust, radius * 2 - adjust, radius * 2 - adjust);


            adjust = .375f * radius;//0.276
            innerCircle.set(adjust, adjust, radius * 2 - adjust, radius * 2 - adjust);
            //************************************************************************************************


            adjust = .32f * radius;//0.038
            textInnerCircle.set(adjust, adjust, radius * 2 - adjust, radius * 2 - adjust);

            adjust = .125f * radius;//0.038
            textOuterCircle.set(adjust, adjust, radius * 2 - adjust, radius * 2 - adjust);


        }


        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            float adjust = (.0095f * radius);

            try {
                String SellinColor = "";
                String SellOutColor = "";
                String GrowthColor = "";
                String SellInCount = "";
                String SellOutCount = "";
                String tdyGrowth = "";
                if (performanceData.getTdySellInColor() != null)
                    SellinColor = "" + performanceData.getTdySellInColor().toString();
                if (performanceData.getTdySellOutColor() != null)
                    SellOutColor = "" + performanceData.getTdySellOutColor().toString();
                if (performanceData.getTdyGrowthColor() != null)
                    GrowthColor = "" + performanceData.getTdyGrowthColor().toString();
                if (performanceData.getTdySellInCount() != null)
                    SellInCount = "" + performanceData.getTdySellInCount();
                if (performanceData.getTdySellOutCount() != null)
                    SellOutCount = "" + performanceData.getTdySellOutCount();
                if (performanceData.getTdyGrowth() != null)
                    tdyGrowth = "" + performanceData.getTdyGrowth();

                if (SellinColor.equalsIgnoreCase("G"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorGreen));
                else if (SellinColor.equalsIgnoreCase("R"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorRed));
                else if (SellinColor.equalsIgnoreCase("Y"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorYellow));
                else
                    paint.setColor(getContext().getResources().getColor(R.color.colorDarkGray));
                drawDonut(canvas, paint, 30, 120, TEXT_SELLIN, SellInCount);

                if (SellOutColor.equalsIgnoreCase("G"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorGreen));
                else if (SellOutColor.equalsIgnoreCase("R"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorRed));
                else if (SellOutColor.equalsIgnoreCase("Y"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorYellow));
                else
                    paint.setColor(getContext().getResources().getColor(R.color.colorDarkGray));
                drawDonut(canvas, paint, 150, 120, TEXT_SELLOUT, SellOutCount);

                if (GrowthColor.equalsIgnoreCase("G"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorGreen));
                else if (GrowthColor.equalsIgnoreCase("R"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorRed));
                else if (GrowthColor.equalsIgnoreCase("Y"))
                    paint.setColor(getContext().getResources().getColor(R.color.colorYellow));
                else
                    paint.setColor(getContext().getResources().getColor(R.color.colorDarkGray));
                drawDonut(canvas, paint, 270, 120, TEXT_GROWTH, tdyGrowth);
                if (performanceData.getTdySmileyColor() != null) {
                    setSmiley(canvas, performanceData.getTdySmileyColor());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        public void drawDonut(Canvas canvas, Paint paint, float start, float sweep, String text, String numerator) {

            if (numerator.equalsIgnoreCase("null")) {
                numerator = "";
            }
            Paint mPaintText_inner;
            float textAdjustment_outer = 50;
            float textAdjustment_inner = 40;
            mPaintText_inner = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaintText_inner.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaintText_inner.setColor(Color.WHITE);
            mPaintText_inner.setTextSize(getContext().getResources().getDimensionPixelSize(R.dimen.textsize_inner));

            Paint mPaintText_outer;
            mPaintText_outer = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaintText_outer.setStyle(Paint.Style.FILL_AND_STROKE);

            mPaintText_outer.setTextSize(getContext().getResources().getDimensionPixelSize(R.dimen.textsize_outer));
            mPaintText_outer.setStrokeWidth(1);

            //Draw the ciecle
            myPath.reset();
            myPath.arcTo(outterCircle, start, sweep, false);
            myPath.arcTo(innerCircle, start + sweep, -sweep, false);
            myPath.close();
            canvas.drawPath(myPath, paint);

            if (text == TEXT_GROWTH) {
                Path myTextPath_inner = new Path();
                myTextPath_inner.arcTo(textInnerCircle, start + textAdjustment_inner, sweep, false);
                canvas.drawTextOnPath(text, myTextPath_inner, 0, 0, mPaintText_inner);
            } else if (text == TEXT_SELLOUT) {
                Path myTextPath_inner = new Path();
                myTextPath_inner.arcTo(textInnerCircle, start + textAdjustment_inner, sweep, false);
                canvas.drawTextOnPath(text, myTextPath_inner, 0, 0, mPaintText_inner);
            } else {
                Path myTextPath_inner = new Path();
                myTextPath_inner.arcTo(textInnerCircle, start + textAdjustment_inner + 40, -48, false);
                canvas.drawTextOnPath(text, myTextPath_inner, 16, 14, mPaintText_inner);

            }


            if (text == TEXT_GROWTH) {
                //Draw the numerator of outer text circle
                float padding = textAdjustment_outer;
                //adjust the numerator dynamically as per size
                padding = padding - 6 * (numerator.length() - 1);
                mPaintText_outer.setColor(Color.RED);
                Path myTextPath_outer = new Path();
                myTextPath_outer.arcTo(textOuterCircle, start + padding, 25, false);
                canvas.drawTextOnPath(numerator, myTextPath_outer, -1, 0, mPaintText_outer);

                //Draw the denominator of outer text circle
                mPaintText_outer.setColor(Color.BLACK);
                Path myTextPath_outer2 = new Path();
                myTextPath_outer2.arcTo(textOuterCircle, start + textAdjustment_outer + 5, 60, false);
                canvas.drawTextOnPath(denominator, myTextPath_outer2, 0, 0, mPaintText_outer);

            } else if (text == TEXT_SELLOUT) {
                //Draw the numerator of outer text circle
                float padding = textAdjustment_outer;
                //adjust the numerator dynamically as per size
                padding = padding - 6 * (numerator.length() - 1);
                mPaintText_outer.setColor(Color.RED);
                Path myTextPath_outer = new Path();
                myTextPath_outer.arcTo(textOuterCircle, start + padding, 35, false);
                canvas.drawTextOnPath(numerator, myTextPath_outer, -4, 0, mPaintText_outer);

                //Draw the denominator of outer text circle
                mPaintText_outer.setColor(Color.BLACK);
                Path myTextPath_outer2 = new Path();
                myTextPath_outer2.arcTo(textOuterCircle, start + textAdjustment_outer + 5, 60, false);
                canvas.drawTextOnPath(denominator, myTextPath_outer2, 0, 0, mPaintText_outer);
            } else {
                //Draw the numerator of outer text circle
                float padding = textAdjustment_outer;
                RectF textOuterCircleSellin;
                textOuterCircleSellin = new RectF();
                float adjustSellin = (.019f * radius);
                adjustSellin = .11f * radius;//0.038
                textOuterCircleSellin.set(adjustSellin, adjustSellin, radius * 2 - adjustSellin, radius * 2 - adjustSellin);

                //adjust the numerator dynamically as per size

                padding = padding + 20 + 7 *(numerator.length() - 1);
                mPaintText_outer.setColor(Color.RED);
                Path myTextPath_outer = new Path();
                myTextPath_outer.arcTo(textOuterCircleSellin, start + padding, -50, false);
                canvas.drawTextOnPath(numerator, myTextPath_outer, 10, 15, mPaintText_outer);

                //Draw the denominator of outer text circle
                mPaintText_outer.setColor(Color.BLACK);
                Path myTextPath_outer2 = new Path();
                myTextPath_outer2.arcTo(textOuterCircleSellin, start + textAdjustment_outer + 16, -50, false);
                canvas.drawTextOnPath(denominator, myTextPath_outer2, 10, 15, mPaintText_outer);
            }
        }

        private void setSmiley(Canvas canvas, String smileyType) {
            Resources res = getResources();
            Bitmap bitmap;
            if (smileyType.equalsIgnoreCase("R")) {
                bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_red_profile);
            } else if (smileyType.equalsIgnoreCase("G")) {
                bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_green_profile);
            } else {
                bitmap = BitmapFactory.decodeResource(res, R.drawable.smile);
            }

            float adjust_bitmap = .55f * radius;//0.038
            canvas.drawBitmap(bitmap, null, new RectF(adjust_bitmap, adjust_bitmap, radius * 2 - adjust_bitmap, radius * 2 - adjust_bitmap), paint);
        }

        public void setGradient(int sColor, int eColor) {
            paint.setShader(new RadialGradient(radius, radius, radius - 5,
                    new int[]{sColor, eColor},
                    new float[]{.6f, .95f}, TileMode.CLAMP));
        }
    }

但是当我在xml和java中使用它时,

//responseBody is nothing but values which I am passing 
 Chart_daily donutChart_daily = new Chart_daily(getBaseContext(), responseBody);
            layout_donutChart_daily.addView(donutChart_daily);

但问题是,如果您看到底部和左侧文字,它会隐藏起来。

enter image description here

0 个答案:

没有答案