feat(map): Android 端改用原生 Canvas 渲染设备地图 Marker 图标
This commit is contained in:
1 parent
59988fdda1
commit
d286dae8a5
3 files changed
+95
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package com.ttstd.familycare;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
final class DeviceMarkerRenderer {
|
||||
private DeviceMarkerRenderer() {}
|
||||
|
||||
static byte[] render(String title) {
|
||||
final float bubbleHeight = 80;
|
||||
final float pinTop = 96;
|
||||
final float pinTip = 168;
|
||||
final int outline = 0xFF1F2937;
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setTextSize(32);
|
||||
while (paint.measureText(title) > 180 && paint.getTextSize() > 20) {
|
||||
paint.setTextSize(paint.getTextSize() - 2);
|
||||
}
|
||||
|
||||
int width = (int) Math.ceil(paint.measureText(title) + 60);
|
||||
float center = width / 2f;
|
||||
Bitmap bitmap = Bitmap.createBitmap(width + 2, 170, Bitmap.Config.ARGB_8888);
|
||||
try {
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.translate(1, 1);
|
||||
|
||||
Path bubble = new Path();
|
||||
bubble.addRoundRect(0, 0, width, bubbleHeight, 10, 10, Path.Direction.CW);
|
||||
drawShape(canvas, bubble, paint, Color.WHITE, outline);
|
||||
|
||||
Path tail = new Path();
|
||||
tail.moveTo(center - 9, bubbleHeight);
|
||||
tail.lineTo(center, pinTop);
|
||||
tail.lineTo(center + 9, bubbleHeight);
|
||||
tail.close();
|
||||
drawShape(canvas, tail, paint, Color.WHITE, outline);
|
||||
|
||||
paint.setColor(outline);
|
||||
paint.setTextAlign(Paint.Align.CENTER);
|
||||
float baseline = (bubbleHeight - paint.ascent() - paint.descent()) / 2;
|
||||
canvas.drawText(title, center, baseline, paint);
|
||||
|
||||
Path pin = new Path();
|
||||
pin.moveTo(center, pinTop);
|
||||
pin.cubicTo(center - 20, pinTop, center - 30, pinTop + 12, center - 30, pinTop + 24);
|
||||
pin.cubicTo(center - 30, pinTop + 50, center, pinTip, center, pinTip);
|
||||
pin.cubicTo(center, pinTip, center + 30, pinTop + 50, center + 30, pinTop + 24);
|
||||
pin.cubicTo(center + 30, pinTop + 12, center + 20, pinTop, center, pinTop);
|
||||
pin.close();
|
||||
drawShape(canvas, pin, paint, 0xFFE5484D, 0xFFC0392B);
|
||||
|
||||
paint.setColor(Color.WHITE);
|
||||
canvas.drawCircle(center, pinTop + 32, 8, paint);
|
||||
|
||||
ByteArrayOutputStream png = new ByteArrayOutputStream();
|
||||
return bitmap.compress(Bitmap.CompressFormat.PNG, 100, png) ? png.toByteArray() : null;
|
||||
} finally {
|
||||
bitmap.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawShape(Canvas canvas, Path path, Paint paint, int fill, int stroke) {
|
||||
paint.setColor(fill);
|
||||
canvas.drawPath(path, paint);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
paint.setStrokeWidth(2);
|
||||
paint.setColor(stroke);
|
||||
canvas.drawPath(path, paint);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,9 @@ public class MainActivity extends FlutterActivity {
|
||||
result.success(true);
|
||||
} else if (call.method.equals("requestLocalNetwork")) {
|
||||
requestLocalNetwork(result);
|
||||
} else if (call.method.equals("renderDeviceMarker")) {
|
||||
String title = call.arguments();
|
||||
result.success(DeviceMarkerRenderer.render(title));
|
||||
} else if (call.method.equals("dialPhone")) {
|
||||
// 打开系统拨号盘拨打指定号码(ACTION_DIAL 无需拨号权限)
|
||||
String phone = call.argument("phone");
|
||||
|
||||
Reference in new issue
Block a user