1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| public class WeatherLineView extends View {
private static final int defaultMinWidth = 100;
private static final int defaultMinHeight = 80;
private int mTemperTextSize = 16;
private int mWeaTextColor = Color.BLACK;
private int mWeaLineWidth = 1;
private int mWeaDotRadius = 5;
private int mTextDotDistance = 5;
private TextPaint mTextPaint;
private Paint.FontMetrics mTextFontMetrics;
private Paint mDotPaint;
private Paint mLinePaint;
private int mLowestTemperData;
private int mHighestTemperData;
private int mLowTemperData[];
private int mHighTemperData[];
public WeatherLineView(Context context) { this(context, null); }
public WeatherLineView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public WeatherLineView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); initPaint(); }
public void setLowHighData(int low[], int high[]) { mLowTemperData = low; mHighTemperData = high; invalidate(); }
public void setLowHighestData(int low, int high) { mLowestTemperData = low; mHighestTemperData = high; invalidate(); }
private void initPaint() { mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setTextSize(mTemperTextSize); mTextPaint.setColor(mWeaTextColor); mTextFontMetrics = mTextPaint.getFontMetrics();
mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mDotPaint.setStyle(Paint.Style.FILL); mDotPaint.setColor(mWeaTextColor);
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mLinePaint.setStyle(Paint.Style.STROKE); mLinePaint.setStrokeWidth(mWeaLineWidth); mLinePaint.setColor(mWeaTextColor); }
private void init(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WeatherLineView, defStyleAttr, 0); mTemperTextSize = (int) a.getDimension(R.styleable.WeatherLineView_temperTextSize, dp2px(context, mTemperTextSize)); mWeaTextColor = a.getColor(R.styleable.WeatherLineView_weatextColor, Color.parseColor("#b07b5c")); mWeaLineWidth = (int) a.getDimension(R.styleable.WeatherLineView_weaLineWidth, dp2px(context, mWeaLineWidth)); mWeaDotRadius = (int) a.getDimension(R.styleable.WeatherLineView_weadotRadius, dp2px(context, mWeaDotRadius)); mTextDotDistance = (int) a.getDimension(R.styleable.WeatherLineView_textDotDistance, dp2px(context, mTextDotDistance)); a.recycle(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width = getSize(widthMode, widthSize, 0); int height = getSize(heightMode, heightSize, 1); setMeasuredDimension(width, height); }
private int getSize(int mode, int size, int type) { int result; if (mode == MeasureSpec.EXACTLY) { result = size; } else { if (type == 0) { result = dp2px(getContext(), defaultMinWidth) + getPaddingLeft() + getPaddingRight(); } else { int textHeight = (int) (mTextFontMetrics.bottom - mTextFontMetrics.top); result = dp2px(getContext(), defaultMinHeight) + 2 * textHeight + getPaddingTop() + getPaddingBottom() + 2 * mTextDotDistance; }
if (mode == MeasureSpec.AT_MOST) { result = Math.min(result, size); } } return result; }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
if (mLowTemperData == null || mHighTemperData == null || mLowestTemperData == 0 || mHighestTemperData == 0) { return; }
canvas.drawColor(Color.YELLOW);
int textHeight = (int) (mTextFontMetrics.bottom - mTextFontMetrics.top);
int baseHeight = getHeight() - textHeight - mTextDotDistance;
int calowMiddle = baseHeight - cacHeight(mLowTemperData[1]); canvas.drawCircle(getWidth() / 2, calowMiddle, mWeaDotRadius, mDotPaint);
String text = String.valueOf(mLowTemperData[1]) + "°"; int baseX = (int) (canvas.getWidth() / 2 - mTextPaint.measureText(text) / 2); int baseY = (int) (calowMiddle - mTextFontMetrics.top) + mTextDotDistance; canvas.drawText(text, baseX, baseY, mTextPaint);
if (mLowTemperData[0] != 0) { int calowLeft = baseHeight - cacHeight(mLowTemperData[0]); canvas.drawLine(0, calowLeft, getWidth() / 2, calowMiddle, mLinePaint); }
if (mLowTemperData[2] != 0) { int calowRight = baseHeight - cacHeight(mLowTemperData[2]); canvas.drawLine(getWidth() / 2, calowMiddle, getWidth(), calowRight, mLinePaint); }
int calHighMiddle = baseHeight - cacHeight(mHighTemperData[1]); canvas.drawCircle(getWidth() / 2, calHighMiddle, mWeaDotRadius, mDotPaint);
String text2 = String.valueOf(mHighTemperData[1]) + "°"; int baseX2 = (int) (canvas.getWidth() / 2 - mTextPaint.measureText(text2) / 2); int baseY2 = (int) (calHighMiddle - mTextFontMetrics.bottom) - mTextDotDistance; canvas.drawText(text2, baseX2, baseY2, mTextPaint);
if (mHighTemperData[0] != 0) { int calHighLeft = baseHeight - cacHeight(mHighTemperData[0]); canvas.drawLine(0, calHighLeft, getWidth() / 2, calHighMiddle, mLinePaint); }
if (mHighTemperData[2] != 0) { int calHighRight = baseHeight - cacHeight(mHighTemperData[2]); canvas.drawLine(getWidth() / 2, calHighMiddle, getWidth(), calHighRight, mLinePaint); } }
private int cacHeight(int tem) { int temDistance = mHighestTemperData - mLowestTemperData; int textHeight = (int) (mTextFontMetrics.bottom - mTextFontMetrics.top); int viewDistance = getHeight() - 2 * textHeight - 2 * mTextDotDistance; int currTemDistance = tem - mLowestTemperData; return currTemDistance * viewDistance / temDistance; }
public static int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); } }
|