@Implementation // todo: this sucks, it's all just so we can detect 9-patches
  public static Drawable createFromResourceStream(
      Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) {
    if (is == null) {
      return null;
    }
    Rect pad = new Rect();
    if (opts == null) opts = new BitmapFactory.Options();
    opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT;

    Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts);
    if (bm != null) {
      boolean isNinePatch = srcName != null && srcName.contains(".9.");
      if (isNinePatch) {
        ReflectionHelpers.callInstanceMethod(
            bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0]));
      }
      byte[] np = bm.getNinePatchChunk();
      if (np == null || !NinePatch.isNinePatchChunk(np)) {
        np = null;
        pad = null;
      }

      if (np != null) {
        // todo: wrong
        return new NinePatchDrawable(res, bm, np, pad, srcName);
      }

      return new BitmapDrawable(res, bm);
    }
    return null;
  }
Esempio n. 2
0
  public static Drawable getDraw9(final InitPackageResourcesParam resparam, final String nama) {

    Bitmap bitmap = BitmapFactory.decodeFile(S.DEFAULT_ICON_DIR + "/" + nama + ".png");
    final byte[] chunk = bitmap.getNinePatchChunk();
    if (NinePatch.isNinePatchChunk(chunk))
      return new NinePatchDrawable(
          resparam.res, bitmap, chunk, Npatch.deserialize(chunk).mPaddings, null);
    else return Drawable.createFromPath(S.DEFAULT_ICON_DIR + "/" + nama + ".png");
  }
Esempio n. 3
0
  private static Drawable getNinePathDrawable(InitPackageResourcesParam resparam, String str) {
    Bitmap bitmap = BitmapFactory.decodeFile(str);
    final byte[] chunk = bitmap.getNinePatchChunk();
    Drawable d;
    if (NinePatch.isNinePatchChunk(chunk)) {
      d =
          new NinePatchDrawable(
              resparam.res, bitmap, chunk, Npatch.deserialize(chunk).mPaddings, null);
    } else {
      d = Drawable.createFromPath(str);
    }

    return d;
  }
Esempio n. 4
0
  /**
   * Create a drawable from an inputstream, using the given resources and value to determine density
   * information.
   */
  public static Drawable createFromResourceStream(
      Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) {
    if (is == null) {
      return null;
    }

    /*  ugh. The decodeStream contract is that we have already allocated
        the pad rect, but if the bitmap does not had a ninepatch chunk,
        then the pad will be ignored. If we could change this to lazily
        alloc/assign the rect, we could avoid the GC churn of making new
        Rects only to drop them on the floor.
    */
    Rect pad = new Rect();

    // Special stuff for compatibility mode: if the target density is not
    // the same as the display density, but the resource -is- the same as
    // the display density, then don't scale it down to the target density.
    // This allows us to load the system's density-correct resources into
    // an application in compatibility mode, without scaling those down
    // to the compatibility density only to have them scaled back up when
    // drawn to the screen.
    if (opts == null) opts = new BitmapFactory.Options();
    opts.inScreenDensity =
        res != null ? res.getDisplayMetrics().noncompatDensityDpi : DisplayMetrics.DENSITY_DEVICE;
    Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts);
    if (bm != null) {
      byte[] np = bm.getNinePatchChunk();
      if (np == null || !NinePatch.isNinePatchChunk(np)) {
        np = null;
        pad = null;
      }

      final Rect opticalInsets = new Rect();
      bm.getOpticalInsets(opticalInsets);
      return drawableFromBitmap(res, bm, np, pad, opticalInsets, srcName);
    }
    return null;
  }