protected void initCompassView() { setFocusable(true); Resources r = this.getResources(); circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(R.color.transparent_color); circlePaint.setStrokeWidth(1); circlePaint.setStyle(Paint.Style.STROKE); textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(r.getColor(R.color.text_color)); textPaint.setFakeBoldText(true); textPaint.setSubpixelText(true); textPaint.setTextAlign(Align.LEFT); textHeight = (int) textPaint.measureText("yY"); markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); markerPaint.setColor(r.getColor(R.color.transparent_color)); markerPaint.setAlpha(200); markerPaint.setStrokeWidth(1); markerPaint.setStyle(Paint.Style.STROKE); markerPaint.setShadowLayer(2, 1, 1, r.getColor(R.color.transparent_color)); borderGradientColors = new int[4]; borderGradientPositions = new float[4]; borderGradientColors[3] = r.getColor(R.color.transparent_color); borderGradientColors[2] = r.getColor(R.color.transparent_color); borderGradientColors[1] = r.getColor(R.color.transparent_color); borderGradientColors[0] = r.getColor(R.color.transparent_color); borderGradientPositions[3] = 0.0f; borderGradientPositions[2] = 1 - 0.03f; borderGradientPositions[1] = 1 - 0.06f; borderGradientPositions[0] = 1.0f; glassGradientColors = new int[5]; glassGradientPositions = new float[5]; int glassColor = 245; glassGradientColors[4] = Color.argb(65, glassColor, glassColor, glassColor); glassGradientColors[3] = Color.argb(100, glassColor, glassColor, glassColor); glassGradientColors[2] = Color.argb(50, glassColor, glassColor, glassColor); glassGradientColors[1] = Color.argb(0, glassColor, glassColor, glassColor); glassGradientColors[0] = Color.argb(0, glassColor, glassColor, glassColor); glassGradientPositions[4] = 1 - 0.0f; glassGradientPositions[3] = 1 - 0.06f; glassGradientPositions[2] = 1 - 0.10f; glassGradientPositions[1] = 1 - 0.20f; glassGradientPositions[0] = 1 - 1.0f; skyHorizonColorFrom = r.getColor(R.color.transparent_color); skyHorizonColorTo = r.getColor(R.color.transparent_color); groundHorizonColorFrom = r.getColor(R.color.transparent_color); groundHorizonColorTo = r.getColor(R.color.transparent_color); }
/** * Y周的刻度值 * * @param canvas */ private void drawYValueText(Canvas canvas) { Paint paint = new Paint(); paint.setColor(axesLineColor); paint.setAntiAlias(true); paint.setSubpixelText(true); paint.setTypeface(Typeface.MONOSPACE); paint.setTextSize(leftTextSize); paint.setTextAlign(Align.RIGHT); /*int yHeight = height - topPadding - bottomPadding - topTextHeight; int spaceLength = yHeight / yNum; int spaceValue = (yMaxValue - yMinValue) / yNum; for (int i = 0; i < 6; i++) { canvas.drawText((yMinValue + spaceValue * i) + "", leftPadding + yTextWidth - 5, height - bottomPadding - yTextWidth - (spaceLength * i), paint); }*/ canvas.drawText( "优秀", leftPadding + (yTextWidth + leftTextSize) / 2, height - bottomPadding - xTextHeight - (spaceYLength * 5) + leftTextSize / 2, paint); canvas.drawText( "良好", leftPadding + (yTextWidth + leftTextSize) / 2, height - bottomPadding - xTextHeight - (spaceYLength * 4) + leftTextSize / 2, paint); canvas.drawText( "一般", leftPadding + (yTextWidth + leftTextSize) / 2, height - bottomPadding - xTextHeight - (spaceYLength * 3) + leftTextSize / 2, paint); canvas.drawText( "差劲", leftPadding + (yTextWidth + leftTextSize) / 2, height - bottomPadding - xTextHeight - (spaceYLength * 2) + leftTextSize / 2, paint); canvas.drawText( "恶劣", leftPadding + (yTextWidth + leftTextSize) / 2, height - bottomPadding - xTextHeight - (spaceYLength * 1) + leftTextSize / 2, paint); }
/** * 画折线图,网格竖线,X坐标值 * * @param canvas */ private void drawChartLine(Canvas canvas) { Paint paintChart = new Paint(); paintChart.setColor(chartLineColor); paintChart.setAntiAlias(true); paintChart.setStyle(Style.STROKE); paintChart.setStrokeWidth(chartLineSize); Paint paintGrid = new Paint(); paintGrid.setColor(gridLineColor); paintGrid.setAntiAlias(true); paintGrid.setStyle(Style.STROKE); paintGrid.setStrokeWidth(gridLineSize); Paint paintTopText = new Paint(); paintTopText.setColor(axesLineColor); paintTopText.setAntiAlias(true); paintTopText.setSubpixelText(true); paintTopText.setTypeface(Typeface.MONOSPACE); paintTopText.setTextSize(topTextSize); paintTopText.setTextAlign(Align.CENTER); Paint paintBottomText = new Paint(); paintBottomText.setColor(axesLineColor); paintBottomText.setAntiAlias(true); paintBottomText.setSubpixelText(true); paintBottomText.setTypeface(Typeface.MONOSPACE); paintBottomText.setTextSize(bottomTextSize); paintBottomText.setTextAlign(Align.CENTER); Paint paintPoint = new Paint(); paintPoint.setColor(pointColor); paintPoint.setAntiAlias(true); paintPoint.setStyle(Style.FILL); pressedPaint = new Paint(); pressedPaint.setColor(pressedColor); pressedPaint.setAntiAlias(true); pressedPaint.setStyle(Style.FILL); Paint paintShape = new Paint(); paintShape.setStyle(Style.FILL); paintShape.setColor(shapeColor); paintShape.setAlpha(shapeAlpha); if (axesData.size() > 0) { for (int i = 0; i < axesData.size(); i++) { if (i < axesData.size() - 1) { // 画折线 canvas.drawLine( axesData.get(i).X, axesData.get(i).Y, axesData.get(i + 1).X, axesData.get(i + 1).Y, paintChart); if (isShapeShow) { // 画阴影 Path path = new Path(); path.moveTo(axesData.get(i).X, height - bottomPadding - xTextHeight); path.lineTo(axesData.get(i).X, axesData.get(i).Y + chartLineSize / 2); path.lineTo(axesData.get(i + 1).X, axesData.get(i + 1).Y + chartLineSize / 2); path.lineTo(axesData.get(i + 1).X, height - bottomPadding - xTextHeight); canvas.drawPath(path, paintShape); } } // 画网格竖线 // canvas.drawLine(axesData.get(i).X, height - bottomPadding - xTextHeight, // axesData.get(i).X, topPadding + topTextHeight, paintGrid); // 写X轴坐标的刻度值 if (!TextUtils.isEmpty(axesData.get(i).getxText())) { setTextSizeForWidth( paintBottomText, spaceXLength - 10, axesData.get(i).getxText()); // 10为相邻日期文字间隔 canvas.drawText( axesData.get(i).getxText(), axesData.get(i).X, height - bottomPadding - xTextHeight / 2, paintBottomText); } // 写顶部的刻度值 if (!TextUtils.isEmpty(axesData.get(i).getTopText())) { // 取消了顶部隔年显示的数据 // canvas.drawText(axesData.get(i).getTopText(), axesData.get(i).X, topPadding, // paintTopText); } // 画数据点 if (pointBitmap == null) { canvas.drawCircle(axesData.get(i).X, axesData.get(i).Y, pointSize + 1, paintChart); canvas.drawCircle(axesData.get(i).X, axesData.get(i).Y, pointSize, paintPoint); } else { Matrix matrix = new Matrix(); canvas.drawBitmap(pointBitmap, matrix, paintPoint); } } // 画最后一个数据的网格竖线 // canvas.drawLine(axesData.get(axesData.size() - 1).X, height - bottomPadding - xTextHeight, // axesData.get(axesData.size() - 1).X, // topPadding + topTextHeight, paintGrid); // 写X轴坐标的最后一个值的刻度值 /* * canvas.drawText(axesData.get(axesData.size() - 1).X + "", * axesData.get(axesData.size() - 1).X, height - bottomPadding - * xTextHeight / 2, paintBottomText); */ // 写顶部的最后一个刻度值 // canvas.drawText("2014", axesData.get(axesData.size() - 1).X, // topPadding, paintTopText); // 画数最后一个据点 if (pointBitmap == null) { // canvas.drawCircle(axesData.get(axesData.size() - 1).X, // axesData.get(axesData.size() - 1).Y, pointSize + 1, // paintChart); // canvas.drawCircle(axesData.get(axesData.size() - 1).X, // axesData.get(axesData.size() - 1).Y, pointSize, paintPoint); } else { Matrix matrix = new Matrix(); canvas.drawBitmap(pointBitmap, matrix, paintPoint); } } if (currentPressedPoint != null) { // canvas.drawCircle(currentPressedPoint.X, currentPressedPoint.Y, pointSize, pressedPaint); } }
private void stroke(Seria s, Canvas canvas) { mPlotPaint.setStyle(Paint.Style.STROKE); // draw line if (s.mWidth > 0) { // create the path mPath.reset(); mPath.moveTo(calcX(s.x[0]), calcY(s.y[0])); for (int i = 1; i < s.sz(); i++) { mPath.lineTo(calcX(s.x[i]), calcY(s.y[i])); } // draw the path mPlotPaint.setColor(s.mColor); mPlotPaint.setStrokeWidth(s.mWidth); mPlotPaint.setPathEffect(s.mEffect); canvas.drawPath(mPath, mPlotPaint); } // draw images seria if (s.bms != null) { for (int i = 0; i < s.sz(); i++) { Bitmap bm; if (s.bms.length > 1) { bm = s.bms[i]; } else { bm = s.bms[0]; } float left = calcX(s.x[i]); float top = calcY(s.y[i]); if (s.hAlign == HALIGN.CENTER) { left -= bm.getWidth() / 2; } else if (s.hAlign == HALIGN.RIGHT) { left -= bm.getWidth(); } if (s.vAlign == VALIGN.CENTER) { top -= bm.getHeight() / 2; } else if (s.vAlign == VALIGN.BOTTOM) { top -= bm.getHeight(); } canvas.drawBitmap(s.bms[i], left, top, mPlotPaint); } } if (s.texts != null) { Align align = Align.CENTER; switch (s.hAlign) { case LEFT: align = Align.RIGHT; case RIGHT: align = Align.LEFT; } mPlotPaint.setTextAlign(align); mPlotPaint.setStrokeWidth(0); mPlotPaint.setColor(s.mColor); for (int i = 0; i < s.sz(); i++) { float x = calcX(s.x[i]); float y = calcY(s.y[i]); switch (s.vAlign) { case TOP: y -= mPlotPaint.getTextSize(); case CENTER: y -= 0.5 * mPlotPaint.getTextSize(); } if (s.deg != 0) { canvas.save(); canvas.rotate(s.deg, x, y); } canvas.drawText(s.texts[i], x, y, mPlotPaint); if (s.deg != 0) { canvas.restore(); } } } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw border mPlotPaint.setStyle(Paint.Style.STROKE); mPlotPaint.setColor(Color.BLACK); canvas.drawRect(mPlotBounds, mPlotPaint); // draw grid mPlotPaint.setColor(gridColor); // x grid if (xTicks != null) { for (float tick : xTicks) { int x = calcX(tick); canvas.drawLine(x, mPlotBounds.bottom, x, mPlotBounds.top, mPlotPaint); } } if (yTicks != null) { for (float tick : yTicks) { int y = calcY(tick); canvas.drawLine(mPlotBounds.left, y, mPlotBounds.right, y, mPlotPaint); } } /* ************************* * draw the data ***************************/ for (Seria s : mData) { stroke(s, canvas); } // draw rectangles that hide out of bounds lines mPlotPaint.setStyle(Style.FILL); mPlotPaint.setColor(Color.WHITE); // left canvas.drawRect(0, 0, mPlotBounds.left, this.getBottom(), mPlotPaint); // bottom canvas.drawRect(0, mPlotBounds.bottom + 1, this.getRight(), this.getBottom(), mPlotPaint); // right canvas.drawRect(mPlotBounds.right + 1, 0, this.getRight(), this.getBottom(), mPlotPaint); // top canvas.drawRect(0, 0, this.getRight(), mPlotBounds.top, mPlotPaint); // x ticks mPlotPaint.setStyle(Paint.Style.STROKE); mPlotPaint.setTextAlign(Align.CENTER); mPlotPaint.setTextSize(xTickTextHeight); mPlotPaint.setColor(xTickColor); if (xTicks != null) { for (float tick : xTicks) { textFigure( xTickFormat.format(tick).toString(), calcX(tick), mPlotBounds.bottom + xTickTextHeight, canvas); } } mPlotPaint.setTextSize(labelTextHeight); mPlotPaint.setTextAlign(Align.RIGHT); if (labelX != null) { textFigure(labelX, mPlotBounds.right, mPlotBounds.bottom + axisXSpacing, canvas); } // y ticks mPlotPaint.setTextAlign(Align.CENTER); mPlotPaint.setTextSize(yTickTextHeight); mPlotPaint.setColor(yTickColor); if (yTicks != null) { for (float tick : yTicks) { textFigure( yTickFormat.format(tick).toString(), mPlotBounds.left - yTickTextHeight, calcY(tick), 90, canvas); } } mPlotPaint.setTextSize(labelTextHeight); mPlotPaint.setTextAlign(Align.RIGHT); if (labelX != null) { textFigure(labelX, mPlotBounds.right, mPlotBounds.bottom + axisXSpacing, canvas); } mPlotPaint.setTextAlign(Align.LEFT); if (labelY != null) { textFigure(labelY, mPlotBounds.left - axisYSpacing, mPlotBounds.top, 90, canvas); } }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.d(TAG, "Slice Count: " + mData.size()); if (mData.size() > 0) { // // Set dimensions for text, pie chart, etc // // Account for padding float xpad = (float) (getPaddingLeft() + getPaddingRight()); float ypad = (float) (getPaddingTop() + getPaddingBottom()); // Account for the label if (mShowText) xpad += mTextWidth; float ww = (float) w - xpad; float hh = (float) h - ypad; // Figure out how big we can make the pie. float diameter = Math.min(ww, hh); mPieBounds = new RectF(0.0f, 0.0f, diameter, diameter); mPieBounds.offsetTo(getPaddingLeft(), getPaddingTop()); // Log.d(TAG, "mPieBounds>> "+mPieBounds); mPointerY = mTextY - (mTextHeight / 2.0f); float pointerOffset = mPieBounds.centerY() - mPointerY; // Make adjustments based on text position if (mTextPos == TEXTPOS_LEFT) { mTextPaint.setTextAlign(Paint.Align.RIGHT); if (mShowText) mPieBounds.offset(mTextWidth, 0.0f); mTextX = mPieBounds.left; if (pointerOffset < 0) { pointerOffset = -pointerOffset; mCurrentItemAngle = 225; } else { mCurrentItemAngle = 135; } mPointerX = mPieBounds.centerX() - pointerOffset; } else { mTextPaint.setTextAlign(Paint.Align.LEFT); mTextX = mPieBounds.right; if (pointerOffset < 0) { pointerOffset = -pointerOffset; mCurrentItemAngle = 315; } else { mCurrentItemAngle = 45; } mPointerX = mPieBounds.centerX() + pointerOffset; } mShadowBounds = new RectF( mPieBounds.left + 10, mPieBounds.bottom + 10, mPieBounds.right - 10, mPieBounds.bottom + 20); // Lay out the child view that actually draws the pie. mPieView.layout( (int) mPieBounds.left, (int) mPieBounds.top, (int) mPieBounds.right, (int) mPieBounds.bottom); mPieView.setPivot(mPieBounds.width() / 2, mPieBounds.height() / 2); mPointerView.layout(0, 0, w, h); onDataChanged(); } }