1 /* 2 * Copyright 2012 AndroidPlot.com 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.androidplot.ui.widget; 18 19 import android.graphics.*; 20 import android.util.Log; 21 import com.androidplot.ui.*; 22 import com.androidplot.util.FontUtils; 23 24 public class TextLabelWidget extends Widget { 25 private static final String TAG = TextLabelWidget.class.getName(); 26 27 private String text; 28 private Paint labelPaint; 29 30 private TextOrientationType orientation; 31 32 private boolean autoPackEnabled = true; 33 34 { 35 labelPaint = new Paint(); 36 labelPaint.setColor(Color.WHITE); 37 labelPaint.setAntiAlias(true); 38 labelPaint.setTextAlign(Paint.Align.CENTER); 39 } 40 41 public TextLabelWidget(LayoutManager layoutManager, SizeMetrics sizeMetrics) { 42 this(layoutManager, sizeMetrics, TextOrientationType.HORIZONTAL); 43 } 44 45 public TextLabelWidget(LayoutManager layoutManager, String title, SizeMetrics sizeMetrics, TextOrientationType orientation) { 46 this(layoutManager, sizeMetrics, orientation); 47 setText(title); 48 } 49 50 public TextLabelWidget(LayoutManager layoutManager, SizeMetrics sizeMetrics, TextOrientationType orientation) { 51 super(layoutManager, new SizeMetrics(0, SizeLayoutType.ABSOLUTE, 0, SizeLayoutType.ABSOLUTE)); 52 //this.plot = plot; 53 //this.setWidth(labelPaint.measureText(plot.getTitle())); 54 //this.setHeight(labelPaint.getFontMetrics().top); 55 setSize(sizeMetrics); 56 this.orientation = orientation; 57 } 58 59 @Override 60 protected void onMetricsChanged(SizeMetrics olds, SizeMetrics news) { 61 if(autoPackEnabled) { 62 pack(); 63 } 64 } 65 66 @Override 67 public void onPostInit() { 68 if(autoPackEnabled) { 69 pack(); 70 } 71 } 72 73 //protected abstract String getText(); 74 75 /** 76 * Sets the dimensions of the widget to exactly contain the text contents 77 */ 78 public void pack() { 79 Log.d(TAG, "Packing..."); 80 Rect size = FontUtils.getStringDimensions(text, getLabelPaint()); 81 if(size == null) { 82 Log.w(TAG, "Attempt to pack empty text."); 83 return; 84 } 85 switch(orientation) { 86 case HORIZONTAL: 87 setSize(new SizeMetrics(size.height(), SizeLayoutType.ABSOLUTE, size.width()+2, SizeLayoutType.ABSOLUTE)); 88 break; 89 case VERTICAL_ASCENDING: 90 case VERTICAL_DESCENDING: 91 setSize(new SizeMetrics(size.width(), SizeLayoutType.ABSOLUTE, size.height()+2, SizeLayoutType.ABSOLUTE)); 92 break; 93 } 94 refreshLayout(); 95 96 } 97 98 /** 99 * Do not call this method directly. It is indirectly invoked every time a plot is 100 * redrawn. 101 * @param canvas The Canvas to draw onto 102 * @param widgetRect the size and coordinates of this widget 103 */ 104 @Override 105 public void doOnDraw(Canvas canvas, RectF widgetRect) { 106 if(text == null || text.length() == 0) { 107 return; 108 } 109 //FontUtils.getStringDimensions(text, labelPaint); 110 float vOffset = labelPaint.getFontMetrics().descent; 111 PointF start = getAnchorCoordinates(widgetRect, 112 AnchorPosition.CENTER); 113 114 // BEGIN ROTATION CALCULATION 115 //int canvasState = canvas.save(Canvas.ALL_SAVE_FLAG); 116 117 try { 118 canvas.save(); 119 canvas.translate(start.x, start.y); 120 switch (orientation) { 121 case HORIZONTAL: 122 break; 123 case VERTICAL_ASCENDING: 124 canvas.rotate(-90); 125 break; 126 case VERTICAL_DESCENDING: 127 canvas.rotate(90); 128 break; 129 default: 130 131 throw new UnsupportedOperationException("Orientation " + orientation + " not yet implemented for TextLabelWidget."); 132 } 133 canvas.drawText(text, 0, vOffset, labelPaint); 134 } finally { 135 //canvas.restoreToCount(canvasState); 136 canvas.restore(); 137 } 138 139 // END ROTATION CALCULATION 140 } 141 142 public Paint getLabelPaint() { 143 return labelPaint; 144 } 145 146 public void setLabelPaint(Paint labelPaint) { 147 this.labelPaint = labelPaint; 148 149 // when paint changes, packing params change too so check 150 // to see if we need to resize: 151 if(autoPackEnabled) { 152 pack(); 153 } 154 } 155 156 public TextOrientationType getOrientation() { 157 return orientation; 158 } 159 160 public void setOrientation(TextOrientationType orientation) { 161 this.orientation = orientation; 162 if(autoPackEnabled) { 163 pack(); 164 } 165 } 166 167 public boolean isAutoPackEnabled() { 168 return autoPackEnabled; 169 } 170 171 public void setAutoPackEnabled(boolean autoPackEnabled) { 172 this.autoPackEnabled = autoPackEnabled; 173 if(autoPackEnabled) { 174 pack(); 175 } 176 } 177 178 public void setText(String text) { 179 Log.d(TAG, "Setting textLabel to: " + text); 180 this.text = text; 181 if(autoPackEnabled) { 182 pack(); 183 } 184 } 185 186 public String getText() { 187 return text; 188 } 189 } 190