导言:
我使用mapbox sdk开发了一个停车场应用程序(这是一个非常古老的库)。我在屏幕上有一张离线Map,当用户长时间点击Map时,它会从Map上获取lat long,并根据该lat long从数据库中获取关于该点停车情况的数据。数据应该以不同颜色的圆圈显示。每种颜色代表一种特定情况(如停车场关闭、付费或免费)。为了更好地理解,请查看此屏幕截图
我所做的:
目前我所做的是:我在位图上绘制颜色,然后我绘制位图并将其添加到标记,然后将该标记添加到Map。然后别针就出现了。
在位图上绘制颜色:
private Bitmap createCustomGauge(Date date, int radius, boolean shouldTransparent,
List<MyPeriod> periods) {
int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;
int RED = 0xFFD6263B;
int BLUE = 0xFF2770A7;
int GREEN = 0xFF00A260;
int GRAY = 0xFF939598;
int TRANSPARENT = 0x1AFFFFFF;
int BACKGROUND_COLOR = shouldTransparent ? TRANSPARENT : WHITE;
int gWidth = radius * 2;
int gHeight = radius * 2;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(gWidth, gHeight, conf);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
bitmap.setHasAlpha(true);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(BLACK);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(gHeight / 10.5F);
float left = gWidth / 12.0F;
float top = gHeight / 12.0F;
RectF rectF = new RectF(left, top, gWidth - left, gHeight - top);
//max drawn angle is 180°: 270°-90° = 180° on left side.
float leftStartAngle = 90;
final float leftEndAngle = 270;
//max drawn angle is 180°: 450°-270° = 180° on right side.
float rightStartAngle = 270;
final float rightEndAngle = 450;
final float maxDrawnAngle = 180; // max drawn angle is always 180°
int leftArrowColor = RED;
int rightArrowColor = RED;
if (periods.isEmpty()) {
p.setColor(RED);
canvas.drawArc(rectF, rightStartAngle, (rightEndAngle - rightStartAngle), false, p);
canvas.drawArc(rectF, leftStartAngle, (leftEndAngle - leftStartAngle), false, p);
} else {
for (int i = 0; i < periods.size(); i++) {
MyPeriod period = periods.get(i);
int leftColor;
int rightColor;
if (period.direction == 0) {
leftColor = (period.getParkingType() == 0) ?
RED : period.getParkingType() == 1 ? BLUE : GREEN;
} else {
leftColor = RED;
}
if (period.direction == 1) {
rightColor = (period.getParkingType() == 0) ?
RED : period.getParkingType() == 1 ? BLUE : GREEN;
} else {
rightColor = RED;
}
if (period.direction == 0) {
p.setColor(leftColor);
float totalSweepAngle =
(float) getDateDifference(period.startDate, period.endDate) * 15;
if (leftStartAngle == 90) {
leftArrowColor = leftColor;
totalSweepAngle = Math.min(totalSweepAngle, maxDrawnAngle);
canvas.drawArc(rectF, leftStartAngle, totalSweepAngle, false, p);
leftStartAngle += totalSweepAngle;
} else if (leftStartAngle < leftEndAngle) {
totalSweepAngle = Math.min(totalSweepAngle, (leftEndAngle - leftStartAngle));
canvas.drawArc(rectF, leftStartAngle, totalSweepAngle, false, p);
leftStartAngle += totalSweepAngle;
}
}
if (period.direction == 1) {
p.setColor(rightColor);
float totalSweepAngle = (float) getDateDifference(period.startDate,
period.endDate) * 15;
if (rightStartAngle == leftEndAngle) {
totalSweepAngle = Math.min(totalSweepAngle, maxDrawnAngle);
rightArrowColor = rightColor;
canvas.drawArc(rectF, rightStartAngle, totalSweepAngle, false, p);
rightStartAngle += totalSweepAngle;
} else if (rightStartAngle < rightEndAngle) {
totalSweepAngle = Math.min(totalSweepAngle, (rightEndAngle - rightStartAngle));
canvas.drawArc(rectF, rightStartAngle, totalSweepAngle, false, p);
rightStartAngle += totalSweepAngle;
}
}
if (i == periods.size() - 1 && rightStartAngle < rightEndAngle) {
p.setColor(rightColor);
//rightArrowColor = RED;
canvas.drawArc(rectF, rightStartAngle, (rightEndAngle - rightStartAngle), false, p);
}
if (i == periods.size() - 1 && leftStartAngle < leftEndAngle) {
p.setColor(leftColor);
//leftArrowColor = RED;
canvas.drawArc(rectF, leftStartAngle, (leftEndAngle - leftStartAngle), false, p);
}
}
}
p.setTextAlign(Paint.Align.CENTER);
p.setColor(BLACK);
p.setStyle(Paint.Style.FILL);
p.setTypeface(Typeface.create("sans-serif", Typeface.BOLD));
p.setTextSize(gHeight / 6.0F);
float bitmapX = bitmap.getWidth() / 2.0F;
float bitmapY = bitmap.getHeight() / 2.0F;
p.setColor(rightArrowColor);
//String rightArrow = "▶";
String rightArrow = "▲";
canvas.drawText(rightArrow, bitmapX - (bitmapX * 0.0052F), bitmapY - (bitmapY * 0.52F), p);
p.setColor(leftArrowColor);
//String leftArrow = "◀";
String leftArrow = "▼";
//float overflow = gHeight / 6.0F;
canvas.drawText(leftArrow, bitmapX - (bitmapX * 0.0070F), bitmapY + (bitmapY * 0.72F), p);
SimpleDateFormat sdfDate = new SimpleDateFormat("h:mma", Locale.getDefault());
//Date now = new Date();
String text = sdfDate.format(date);
p.setColor(BLACK);
p.setTextAlign(Paint.Align.CENTER);
p.setTextSize(gHeight / 8.5F);
p.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
//canvas.drawText(text, rectF.centerX() - text.length() * 2 - 15F, rectF.centerY() + 5, p);
canvas.drawText(text, canvas.getWidth() / 2.0F,
canvas.getHeight() / 2.0F + (gHeight / 25.0F), p);
p.setColor(GRAY);
p.setTextSize(gHeight / 8.0F);
p.setTextAlign(Paint.Align.CENTER);
//String right = "R";
float lWidth = (bitmap.getWidth() / 2.0F) + p.getTextSize();
canvas.drawText("R", lWidth, bitmap.getHeight() / 2.60F, p);
p.setTextAlign(Paint.Align.CENTER);
//String left = "L";
float rWidth = (bitmap.getWidth() / 2.0F) - p.getTextSize();
canvas.drawText("L", rWidth, bitmap.getHeight() / 1.42F, p);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
c.drawARGB(0, 0, 0, 0);
c.drawCircle(bitmap.getWidth() / 2.0F, bitmap.getHeight() / 2.0F,
bitmap.getWidth() / 2.0F, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
c.drawBitmap(bitmap, rect, rect, paint);
return output;
}
所有这些都发生在后台进程中,然后在主线程上:我使用该位图制作如下标记:
Marker marker = new Marker(mapView, null, null, coordinate);
Drawable mDrawable = new BitmapDrawable(requireActivity().getResources(),mapBitmap);
marker.setMarker(mDrawable);
marker.setAnchor(new PointF(0.5F, 0.6F));
mapView.addMarker(marker);
我想要的是:
我想动画这些颜色一样,用户应该能够查看动画的颜色绘制。与所有这些任务一样,从数据库获取数据以及计算和绘制绘图的过程是一个很长的过程,不能在主线程上完成,因此所有这些都发生在后台线程中。
我的问题是:是否有任何方法向用户显示实时动画?是否有任何方法为绘制动画?
因为:
首先从数据库中提取数据。
然后对数据进行计算和组织。
然后,我在位图上绘制并返回它。
将位图转换为可绘制的。
使马克笔从可拉拔。
将标记添加到Map中,最终pin会显示给用户。
记住所有这些要点和限制,是否有可能制作动画,或者有什么方法可以做到这一点?
暂无答案!
目前还没有任何答案,快来回答吧!