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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
| public class Today24HourView extends View { private static final String TAG = "Today24HourView"; private static final int ITEM_SIZE = 24; private int ITEM_WIDTH; private int MARGIN_LEFT_ITEM; private int MARGIN_RIGHT_ITEM; private int bottomTextHeight; private int scrollWidth;
private int mHeight, mWidth; private int tempBaseTop; private int tempBaseBottom; private Paint bitmapPaint, linePaint, rectPaint, indicatorLinePaint; private TextPaint textPaint;
private List<WeatherHoursModel> listItems; private int maxScrollOffset = 0; private int scrollOffset = 0; private int currentItemIndex = 0;
private int maxTemp; private int minTemp;
public Today24HourView(Context context) { this(context, null); }
public Today24HourView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public Today24HourView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); }
private void init() { MARGIN_LEFT_ITEM = DisplayUtil.dip2px(getContext(), 2); MARGIN_RIGHT_ITEM = DisplayUtil.dip2px(getContext(), 20); ITEM_WIDTH = DisplayUtil.dip2px(getContext(), 30); bottomTextHeight = DisplayUtil.dip2px(getContext(), 16); mWidth = MARGIN_LEFT_ITEM + MARGIN_RIGHT_ITEM + ITEM_SIZE * ITEM_WIDTH; mHeight = DisplayUtil.dip2px(getContext(), 140); tempBaseTop = (mHeight - bottomTextHeight) / 3; tempBaseBottom = (mHeight - bottomTextHeight) * 3 / 4; listItems = new ArrayList<>(); initPaint(); }
private void initPaint() {
rectPaint = new Paint(); rectPaint.setColor(Color.parseColor("#1AFFFFFF")); rectPaint.setAntiAlias(true); rectPaint.setStyle(Paint.Style.FILL); rectPaint.setStrokeCap(Paint.Cap.ROUND); rectPaint.setStrokeJoin(Paint.Join.ROUND); rectPaint.setStrokeWidth(1);
PathEffect pathEffect = new CornerPathEffect(180); linePaint = new Paint(); linePaint.setColor(Color.WHITE); linePaint.setPathEffect(pathEffect); linePaint.setAntiAlias(true); linePaint.setStrokeCap(Paint.Cap.ROUND); linePaint.setStrokeJoin(Paint.Join.ROUND); linePaint.setStyle(Paint.Style.STROKE); linePaint.setStrokeWidth(10);
textPaint = new TextPaint(); textPaint.setColor(Color.WHITE); textPaint.setAntiAlias(true);
bitmapPaint = new Paint(); bitmapPaint.setAntiAlias(true);
indicatorLinePaint = new Paint(); indicatorLinePaint = new Paint(); indicatorLinePaint.setColor(Color.WHITE); indicatorLinePaint.setAntiAlias(true); indicatorLinePaint.setStrokeCap(Paint.Cap.ROUND); indicatorLinePaint.setStyle(Paint.Style.STROKE); indicatorLinePaint.setStrokeWidth(2); }
public void setHourItems(List<WeatherHoursModel> listItems) { this.listItems.clear(); List<WeatherHoursModel> list = new ArrayList<>(listItems); maxTemp = list.get(0).getTemperature(); minTemp = list.get(0).getTemperature(); for (WeatherHoursModel listItem : list) { if (listItem.getTemperature() > maxTemp) maxTemp = listItem.getTemperature(); if (listItem.getTemperature() < minTemp) minTemp = listItem.getTemperature(); } int icon = list.get(0).getIcon(); for (int i = 0; i < list.size(); i++) { int left = MARGIN_LEFT_ITEM + i * ITEM_WIDTH; int right = left + ITEM_WIDTH; Point point = calculateTempPoint(left, right, list.get(i).getTemperature()); if (i != 0 && icon == list.get(i).getIcon()) { list.get(i).setIcon(-1); } else { icon = list.get(i).getIcon(); } list.get(i).setTempPoint(point); } this.listItems.addAll(list); invalidate(); }
private Point calculateTempPoint(int left, int right, int temp) { double minHeight = tempBaseTop; double maxHeight = tempBaseBottom; double tempY = maxHeight - (temp - minTemp) * 1.0 / (maxTemp - minTemp) * (maxHeight - minHeight); Point point = new Point((left + right) / 2, (int) tempY); return point; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(mWidth, mHeight); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); onDrawLine(canvas);
for (int i = 0; i < listItems.size(); i++) { onDrawTemp(canvas, i); onDrawText(canvas, i); } }
private void onDrawTemp(Canvas canvas, int i) { WeatherHoursModel item = listItems.get(i); if (currentItemIndex == i) { int Y = getTempBarY(); Rect targetRect = new Rect( getScrollBarX(), Y - DisplayUtil.dip2px(getContext(), 40), getScrollBarX() + DisplayUtil.dip2px(getContext(), 92), Y - DisplayUtil.dip2px(getContext(), 14)); Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_indicator_text); drawable.setBounds(targetRect); drawable.draw(canvas); Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt(); int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2; textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setTextSize(DisplayUtil.sp2px(getContext(), 10)); canvas.drawText(TimeUtils.getDateHHmm(item.getTempStamp()) + " " + item.getWeather() + " " + item.getTemperature() + "°", targetRect.centerX(), baseline, textPaint); int height = mHeight - bottomTextHeight - DisplayUtil.dip2px(getContext(), 4); canvas.drawLine(targetRect.centerX(), targetRect.bottom + DisplayUtil.dip2px(getContext(), 4), targetRect.centerX(), height, indicatorLinePaint); } }
private void onDrawLine(Canvas canvas) { if (listItems.size() > 0) { Path path = new Path(); Point point0 = listItems.get(0).getTempPoint(); path.moveTo(point0.x, point0.y); Path pathBG = new Path(); pathBG.moveTo(point0.x, point0.y);
for (int i = 0; i < listItems.size(); i++) { Point point = listItems.get(i).getTempPoint(); if (i != 0) { Point pointPre = listItems.get(i - 1).getTempPoint(); if (i == 1) { path.lineTo(point.x, point.y); pathBG.lineTo(point.x, point.y); } else { path.rLineTo(point.x - pointPre.x, point.y - pointPre.y); if (listItems.get(i).getIcon() != -1) pathBG.rLineTo(point.x - pointPre.x - DisplayUtil.dip2px(getContext(), 1), point.y - pointPre.y); else pathBG.rLineTo(point.x - pointPre.x, point.y - pointPre.y); }
Point pointBackup = listItems.get(0).getTempPoint(); if (listItems.get(i).getIcon() != -1 || (getGoneBehind(i) && i == listItems.size() - 1)) { int icon = -1; int indexBackUp = 0; for (int j = 0; j < i; j++) { if (listItems.get(j).getIcon() != -1) { icon = listItems.get(j).getIcon(); indexBackUp = j; pointBackup = listItems.get(j).getTempPoint(); } } if (pointBackup.x < getScrollBarX() + DisplayUtil.dip2px(getContext(), 46) && getScrollBarX() + DisplayUtil.dip2px(getContext(), 46) < point.x) { rectPaint.setColor(Color.parseColor("#33FFFFFF")); } else { rectPaint.setColor(Color.parseColor("#1AFFFFFF")); } if (listItems.get(i).getTempPoint() != pointBackup) { int height = mHeight - bottomTextHeight - DisplayUtil.dip2px(getContext(), 4) - point.y; pathBG.rLineTo(0, height); pathBG.rLineTo(pointBackup.x - point.x + DisplayUtil.dip2px(getContext(), 1), 0); canvas.drawPath(pathBG, rectPaint); pathBG.reset(); pathBG.moveTo(point.x, point.y);
int left = (point.x - pointBackup.x) / 2 + pointBackup.x - DisplayUtil.dip2px(getContext(), 10); int right = (point.x - pointBackup.x) / 2 + pointBackup.x + DisplayUtil.dip2px(getContext(), 10); int newLeft = (point.x - (pointBackup.x - getItemLeftMargin(indexBackUp))) / 2 + (pointBackup.x - getItemLeftMargin(indexBackUp)); int newRight = ((point.x + getItemRightMargin(i)) - pointBackup.x) / 2 + pointBackup.x; if (getItemLeftMargin(indexBackUp) < 0 && newLeft + DisplayUtil.dip2px(getContext(), 20) < point.x && i - indexBackUp > 1) { left = newLeft - DisplayUtil.dip2px(getContext(), 10); right = left + DisplayUtil.dip2px(getContext(), 20); } else if (getItemLeftMargin(indexBackUp) < 0 && newLeft + DisplayUtil.dip2px(getContext(), 40) >= point.x && i - indexBackUp > 1) { left = point.x - DisplayUtil.dip2px(getContext(), 30); right = left + DisplayUtil.dip2px(getContext(), 20); } if (getItemRightMargin(i) < 0 && newRight > pointBackup.x + DisplayUtil.dip2px(getContext(), 10) && i - indexBackUp > 1) { right = newRight + DisplayUtil.dip2px(getContext(), 10); left = right - DisplayUtil.dip2px(getContext(), 20); } if (getItemLeftMargin(indexBackUp) < 0 && getItemRightMargin(i) < 0) { left = pointBackup.x - getItemLeftMargin(indexBackUp) + scrollWidth / 2 - DisplayUtil.dip2px(getContext(), 10); right = left + DisplayUtil.dip2px(getContext(), 20); } Drawable drawable = ContextCompat.getDrawable(getContext(), icon); drawable.setBounds(left, tempBaseBottom + DisplayUtil.dip2px(getContext(), 5), right, tempBaseBottom + DisplayUtil.dip2px(getContext(), 25)); drawable.draw(canvas); } } } } canvas.drawPath(path, linePaint); } }
private boolean getGoneBehind(int index) { List<Boolean> data = new ArrayList<>(); for (int k = index; k < listItems.size(); k++) { data.add(listItems.get(k).getIcon() == -1); } return !data.contains(false); }
private void onDrawText(Canvas canvas, int i) { textPaint.setTextAlign(Paint.Align.CENTER); String text = TimeUtils.getDateHHmm(listItems.get(i).getTempStamp()); int left = MARGIN_LEFT_ITEM + i * ITEM_WIDTH; int right = left + ITEM_WIDTH - 1; int bottom = mHeight - bottomTextHeight; Rect targetRect = new Rect(left, bottom, right, bottom + bottomTextHeight); Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt(); int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2; textPaint.setTextSize(DisplayUtil.sp2px(getContext(), 12)); if (i % 2 == 0) canvas.drawText(text, left + (ITEM_WIDTH - 1) / 2, baseline, textPaint); }
public void drawLeftTempText(Canvas canvas) { if (listItems.size() > 0) { textPaint.setTextAlign(Paint.Align.LEFT); textPaint.setTextSize(DisplayUtil.sp2px(getContext(), 13)); canvas.drawText(maxTemp + "°", DisplayUtil.sp2px(getContext(), 15), tempBaseTop, textPaint); canvas.drawText(minTemp + "°", DisplayUtil.sp2px(getContext(), 15), tempBaseBottom, textPaint); } }
public void setScrollOffset(int offset, int maxScrollOffset, int scrollWidth) { this.maxScrollOffset = maxScrollOffset; this.scrollWidth = scrollWidth; scrollOffset = offset; currentItemIndex = calculateItemIndex(); invalidate(); }
private int getItemLeftMargin(int i) { int left = MARGIN_LEFT_ITEM + i * ITEM_WIDTH + (ITEM_WIDTH - 1) / 2; return left - scrollOffset; }
private int getItemRightMargin(int i) { int left = MARGIN_LEFT_ITEM + i * ITEM_WIDTH + (ITEM_WIDTH - 1) / 2; return scrollWidth - (left - scrollOffset); }
private int calculateItemIndex() { int x = getScrollBarX(); int sum = MARGIN_LEFT_ITEM - ITEM_WIDTH; for (int i = 0; i < ITEM_SIZE; i++) { sum += ITEM_WIDTH; if (x < sum) return i; } return ITEM_SIZE - 1; }
private int getScrollBarX() { int x = (ITEM_SIZE - 5) * ITEM_WIDTH * scrollOffset / maxScrollOffset; x = x + MARGIN_LEFT_ITEM; return x; }
private int getTempBarY() { int x = getScrollBarX(); int sum = MARGIN_LEFT_ITEM; Point startPoint = null, endPoint; int i; for (i = 0; i < ITEM_SIZE; i++) { sum += ITEM_WIDTH; if (x < sum) { startPoint = listItems.get(i).getTempPoint(); break; } } if (i + 1 >= ITEM_SIZE || startPoint == null) return listItems.get(ITEM_SIZE - 1).getTempPoint().y; endPoint = listItems.get(i + 1).getTempPoint(); int left = MARGIN_LEFT_ITEM + i * ITEM_WIDTH; int y = (int) (startPoint.y + (x - left) * 1.0 / ITEM_WIDTH * (endPoint.y - startPoint.y)); return y; } }
|