View生成Bitmap

高德地图对自定义Marker的处理,它是通过把我们的自定义View生成Bitmap,然后把图片放到地图中。

/**
 * view转化为图片
 *
 * @param context
 * @param var0
 * @return
 */
  public static Bitmap convertViewToBitmap(Context context, View view) {
      try {
          FrameLayout frameLayout = new FrameLayout(context);
          frameLayout.addView(view);
          frameLayout.setDrawingCacheEnabled(true);
          checkView(frameLayout);
          frameLayout.destroyDrawingCache();
          frameLayout.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
          frameLayout.layout(0, 0, frameLayout.getMeasuredWidth(), frameLayout.getMeasuredHeight());
          Bitmap bitmap = frameLayout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
          return bitmap;
      } catch (Throwable var2) {
          az.a(var2, "Utils", "getBitmapFromView");
          var2.printStackTrace();
          return null;
      }
  }

  private static void checkView(View view) {
      if (view instanceof ViewGroup) {
          for (int i = 0; i < ((ViewGroup) view).getChildCount(); ++i) {
              b(((ViewGroup) view).getChildAt(i));
          }
      } else if (view instanceof TextView) {
          ((TextView) view).setHorizontallyScrolling(false);
      }
  }

你可能感兴趣的:(android)