public static Paint createTypePaint( Typeface typeface, float textSize, int color, boolean antialias, Paint.Align align) { Paint paint = new Paint(); paint.setColor(color); paint.setTypeface(typeface); paint.setTextSize(textSize); paint.setAntiAlias(antialias); paint.setTextAlign(align); paint.setHinting(Paint.HINTING_ON); paint.setDither(true); return paint; }
@Override public void onLoadFinished(Loader<Cursor> ldr, Cursor data) { int l = data.getCount(); data.moveToFirst(); long maxCents = 0; long minCents = Long.MAX_VALUE; long maxTime = 0; long minTime = Long.MAX_VALUE; for (int i = 0; i != l; ++i) { long cents = data.getLong(0); if (cents > maxCents) maxCents = cents; if (cents < minCents) minCents = cents; long time = data.getLong(3); if (time > maxTime) maxTime = time; if (time < minTime) minTime = time; data.moveToNext(); } ImageView view = (ImageView) getView(); int cardSpacing = getActivity().getResources().getDimensionPixelSize(R.dimen.cardSpacing); int cardPadding = getActivity().getResources().getDimensionPixelSize(R.dimen.cardPadding); int textSize = cardPadding * 2; int width = getActivity().getWindow().getWindowManager().getDefaultDisplay().getWidth() - 2 * (cardSpacing) - 2 * (cardPadding); Log.d("Budget", "GraphFragment.onLoadFinished(): width=" + width); int height = getActivity().getResources().getDimensionPixelSize(R.dimen.graphHeight); Log.d("Budget", "GraphFragment.onLoadFinished(): height=" + height); Bitmap chart = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas chartCanvas = new Canvas(chart); chartCanvas.drawColor(Color.TRANSPARENT); Paint pen = new Paint(); pen.setColor(0xFF000000); pen.setTextAlign(Paint.Align.CENTER); pen.setTextSize(textSize); Paint brush = new Paint(); brush.setDither(true); brush.setHinting(Paint.HINTING_ON); brush.setStyle(Paint.Style.STROKE); float stroke = getActivity().getResources().getDimension(R.dimen.graphStroke); brush.setStrokeWidth(stroke); int currentEnvelope = -1; Path currentPath = null; float usableHeight = height - (2 * stroke) - textSize - cardPadding; float usableWidth = width - (2 * stroke) - textSize; data.moveToFirst(); for (int i = 0; i != l; ++i) { int envelope = data.getInt(1); long cents = data.getLong(0); long time = data.getLong(3); float pointHeight = usableHeight - (float) ((cents - minCents) * usableHeight / ((double) (maxCents - minCents))) + stroke; float pointPosition = (float) ((time - minTime) * usableWidth / ((double) (maxTime - minTime))) + stroke + textSize; Log.d("Budget", "GraphFragment.onLoadFinished(): envelope=" + envelope); Log.d("Budget", "GraphFragment.onLoadFinished(): envelope.name=" + data.getString(4)); Log.d("Budget", "GraphFragment.onLoadFinished(): cents=" + cents); Log.d("Budget", "GraphFragment.onLoadFinished(): pointHeight=" + pointHeight); Log.d("Budget", "GraphFragment.onLoadFinished(): time=" + time); Log.d("Budget", "GraphFragment.onLoadFinished(): pointPosition=" + pointPosition); if (envelope != currentEnvelope) { if (currentPath != null) { currentPath.rLineTo(usableWidth, 0); chartCanvas.drawPath(currentPath, brush); } int color = data.getInt(2); // brush = new Paint(brush); brush.setColor(color); currentEnvelope = envelope; currentPath = new Path(); currentPath.moveTo(pointPosition, pointHeight); } else { currentPath.lineTo(pointPosition, pointHeight); } data.moveToNext(); } if (currentPath != null) { currentPath.rLineTo(usableWidth, 0); chartCanvas.drawPath(currentPath, brush); } Path side = new Path(); side.moveTo(textSize, height); side.lineTo(textSize, 0); chartCanvas.drawTextOnPath( getActivity().getString(R.string.envelopeDetails_balance), side, 0, 0, pen); Path bottom = new Path(); bottom.moveTo(0, height - cardPadding); bottom.lineTo(width, height - cardPadding); chartCanvas.drawTextOnPath(getActivity().getString(R.string.graph_time), bottom, 0, 0, pen); if (maxTime != 0) { Date maxTimeD = new Date(maxTime); Date minTimeD = new Date(minTime); DateFormat timeFormat = android.text.format.DateFormat.getDateFormat(getActivity()); pen.setTextAlign(Paint.Align.LEFT); chartCanvas.drawTextOnPath(timeFormat.format(minTimeD), bottom, 0, 0, pen); pen.setTextAlign(Paint.Align.RIGHT); chartCanvas.drawTextOnPath(timeFormat.format(maxTimeD), bottom, 0, 0, pen); } view.setImageBitmap(chart); }