public static String[] breakLine(String s, Paint paint, int i) { String as[] = {}; if (s != null && i >= 0) { ArrayList arraylist = new ArrayList(); int j = 0; int k = s.length(); int l = 1; int i1 = paint.breakText(s, 0, k, true, i, null); do { if (j + i1 > k) { arraylist.add(s.substring(j, j + i1)); as = (String[]) arraylist.toArray(new String[l]); return as; } l++; int j1 = s.lastIndexOf(' ', j + i1); if (j1 > j) { arraylist.add(s.substring(j, j1)); j = j1 + 1; } else { arraylist.add(s.substring(j, j + i1)); j += i1; } i1 = paint.breakText(s, j, k, true, i, null); } while (true); } return as; }
/** * Draws the chart legend. * * @param canvas the canvas to paint to * @param renderer the series renderer * @param titles the titles to go to the legend * @param left the left X value of the area to draw to * @param right the right X value of the area to draw to * @param y the y value of the area to draw to * @param width the width of the area to draw to * @param height the height of the area to draw to * @param legendSize the legend size * @param paint the paint to be used for drawing * @param calculate if only calculating the legend size * @return the legend height */ protected int drawLegend( Canvas canvas, DefaultRenderer renderer, String[] titles, int left, int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) { float size = 32; if (renderer.isShowLegend()) { float currentX = left; float currentY = y + height - legendSize + size; paint.setTextAlign(Align.LEFT); paint.setTextSize(renderer.getLegendTextSize()); int sLength = Math.min(titles.length, renderer.getSeriesRendererCount()); for (int i = 0; i < sLength; i++) { final float lineSize = getLegendShapeWidth(i); String text = titles[i]; if (titles.length == renderer.getSeriesRendererCount()) { paint.setColor(renderer.getSeriesRendererAt(i).getColor()); } else { paint.setColor(Color.LTGRAY); } float[] widths = new float[text.length()]; paint.getTextWidths(text, widths); float sum = 0; for (float value : widths) { sum += value; } float extraSize = lineSize + 10 + sum; float currentWidth = currentX + extraSize; if (i > 0 && getExceed(currentWidth, renderer, right, width)) { currentX = left; currentY += renderer.getLegendTextSize(); size += renderer.getLegendTextSize(); currentWidth = currentX + extraSize; } if (getExceed(currentWidth, renderer, right, width)) { float maxWidth = right - currentX - lineSize - 10; if (isVertical(renderer)) { maxWidth = width - currentX - lineSize - 10; } int nr = paint.breakText(text, true, maxWidth, widths); text = text.substring(0, nr) + "..."; } if (!calculate) { drawLegendShape(canvas, renderer.getSeriesRendererAt(i), currentX, currentY, i, paint); drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint); } currentX += extraSize; } } return Math.round(size + renderer.getLegendTextSize()); }
/* * Breaks the given String into string with line breaks at needed positions. */ private static final Object[] getBreakerText(String temp, int width, Paint toCheck) { StringBuilder parts = new StringBuilder(); temp = temp.trim().replace("\u00AD", ""); int rowCount = 0; do { int length = toCheck.breakText(temp, true, width, null); float measured = toCheck.measureText(temp); if (length < temp.length() && measured >= width) { int bestBreak = temp.lastIndexOf("-", length - 1); if (bestBreak == -1) { bestBreak = temp.lastIndexOf(" ", length - 1); } if (bestBreak == -1) { bestBreak = temp.lastIndexOf(",", length - 1); } if (bestBreak == -1) { bestBreak = temp.lastIndexOf("/", length - 1); } if (bestBreak > 0) { parts.append(temp.substring(0, bestBreak + 1).trim()).append("\n"); temp = temp.substring(bestBreak + 1).trim(); } else { parts.append(temp.substring(0, length - 1).trim()).append("\n"); temp = temp.substring(length - 1).trim(); } } else { parts.append(temp); temp = ""; } rowCount++; } while (!temp.trim().isEmpty()); return new Object[] {parts.toString(), rowCount}; }
protected Vector<String> pageDown() { String strParagraph = ""; Vector<String> lines = new Vector<String>(); while (lines.size() < mLineCount && m_mbBufEnd < m_mbBufLen) { byte[] paraBuf = readParagraphForward(m_mbBufEnd); // 读取一个段落 m_mbBufEnd += paraBuf.length; try { strParagraph = new String(paraBuf, m_strCharsetName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String strReturn = ""; if (strParagraph.indexOf("\r\n") != -1) { strReturn = "\r\n"; strParagraph = strParagraph.replaceAll("\r\n", ""); } else if (strParagraph.indexOf("\n") != -1) { strReturn = "\n"; strParagraph = strParagraph.replaceAll("\n", ""); } if (strParagraph.length() == 0) { lines.add(strParagraph); } while (strParagraph.length() > 0) { int nSize = mPaint.breakText(strParagraph, true, mVisibleWidth, null); lines.add(strParagraph.substring(0, nSize)); strParagraph = strParagraph.substring(nSize); if (lines.size() >= mLineCount) { break; } } if (strParagraph.length() != 0) { try { m_mbBufEnd -= (strParagraph + strReturn).getBytes(m_strCharsetName).length; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return lines; }
protected void pageUp() { if (m_mbBufBegin < 0) m_mbBufBegin = 0; Vector<String> lines = new Vector<String>(); String strParagraph = ""; while (lines.size() < mLineCount && m_mbBufBegin > 0) { Vector<String> paraLines = new Vector<String>(); byte[] paraBuf = readParagraphBack(m_mbBufBegin); m_mbBufBegin -= paraBuf.length; try { strParagraph = new String(paraBuf, m_strCharsetName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } strParagraph = strParagraph.replaceAll("\r\n", ""); strParagraph = strParagraph.replaceAll("\n", ""); if (strParagraph.length() == 0) { paraLines.add(strParagraph); } while (strParagraph.length() > 0) { int nSize = mPaint.breakText(strParagraph, true, mVisibleWidth, null); paraLines.add(strParagraph.substring(0, nSize)); strParagraph = strParagraph.substring(nSize); } lines.addAll(0, paraLines); } while (lines.size() > mLineCount) { try { m_mbBufBegin += lines.get(0).getBytes(m_strCharsetName).length; lines.remove(0); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } m_mbBufEnd = m_mbBufBegin; return; }