【问题标题】:How to convert custom view to bitmap for Google Maps Marker icon如何将自定义视图转换为谷歌地图标记图标的位图
【发布时间】:2017-02-15 05:52:30
【问题描述】:

我需要在运行时计算的 Google 地图图标标记。为此,我创建了派生自 View 的类。

class DrawView extends View {

Paint p;
RectF rectf;
String textLabel;

public DrawView(Context context, String text) {
    super(context);
    p = new Paint();
    rectf = new RectF(100,100,300,150);
    this.textLabel=text;
}

@Override
protected void onDraw(Canvas canvas) {
    p.setColor(Color.RED);
    p.setStrokeWidth(10);
    canvas.drawRoundRect(rectf, 20, 20, p);
    //another draw will be here...
    }
}

将视图转换为位图

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return b;
}

添加标记

View view =new DrawView(this,"Some text");
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bitmap=loadBitmapFromView(view);
            Marker marker=mMap.addMarker(new MarkerOptions().position(new LatLng(s.lat,s.lng)).icon(BitmapDescriptorFactory.fromBitmap(bitmap)));

发生 NPE

FATAL EXCEPTION: main
java.lang.NullPointerException
at ru.ch2.activity.CheckInActivity.loadBitmapFromView(CheckInActivity.java:439)
at ru.ch2.activity.CheckInActivity.onMapReady(CheckInActivity.java:254)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:347)
at xk.a(:com.google.android.gms.DynamiteModulesB:82)
at maps.af.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

在loadBitmapFromView就行了

v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

【问题讨论】:

标签: android google-maps android-custom-view bitmapfactory


【解决方案1】:

尝试使用此功能:

调用view.measure强制计算视图大小以获得正确的视图大小

 public static Bitmap loadBitmapFromView(View view) {
    view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);
    return bitmap;
}

【讨论】:

  • v.layout(...) 之前检查view.isLaidOut() 可能会更好。
猜你喜欢
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2018-01-13
  • 2014-05-27
  • 2013-08-21
相关资源
最近更新 更多