Example #1
0
  private Bitmap tintImage(Bitmap image, Bitmap image2, KrollDict args) {
    String col = args.optString("color", "");
    String mod1 = args.optString("modeColor", "multiply");
    String mod2 = args.optString("modeImage", "multiply");
    Boolean grad = args.optBoolean("vignette", false);

    if (image != null) {

      Mode filterMode1 = getFilter(mod1);
      Mode filterMode2 = getFilter(mod2);
      int width = image.getWidth();
      int height = image.getHeight();

      Bitmap workingBitmap = Bitmap.createScaledBitmap(image, width, height, true);
      Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
      Canvas canvas = new Canvas(mutableBitmap);

      Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas canvas2 = new Canvas(resultBitmap);

      // add second image
      if (image2 != null) {
        Paint Compose = new Paint();
        Compose.setXfermode(
            new PorterDuffXfermode(filterMode2)); // KAI: fixed error in the original code
        canvas.drawBitmap(image2, 0, 0, Compose);
      }

      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

      // add color filter
      if (col != "") {
        PorterDuffColorFilter cf =
            new PorterDuffColorFilter(
                Color.parseColor(col), filterMode1); // KAI: fixed error in the original code
        paint.setColorFilter(cf);
      }

      // gradient
      if (grad) {
        int[] Colors = {0x00000000, 0xFF000000};
        float[] ColorPosition = {0.10f, 0.99f};
        RadialGradient gradient =
            new RadialGradient(
                width / 2,
                height / 2,
                width - width / 2,
                Colors,
                ColorPosition,
                android.graphics.Shader.TileMode.CLAMP);
        paint.setDither(true);
        paint.setShader(gradient);
      }

      canvas2.drawBitmap(mutableBitmap, 0, 0, paint);
      return resultBitmap;
    }

    return null;
  }
  public TiGradientDrawable(View view, KrollDict properties) {
    super(new RectShape());

    // Determine which type of gradient is being used.
    // Supported types are 'linear' and 'radial'.
    String type = properties.optString("type", "linear");
    if (type.equals("linear")) {
      gradientType = GradientType.LINEAR_GRADIENT;
    } else if (type.equals("radial")) {
      gradientType = GradientType.RADIAL_GRADIENT;

      // TODO: Add support for radial gradients.
      // Android's RadialGradient only supports a single circle.
      // We need to figure out how to support two circle gradients
      // as specified by the HTML Canvas specification.
      return;

    } else {
      throw new IllegalArgumentException("Invalid gradient type. Must be linear or radial.");
    }

    // Load the 'startPoint' property which defines the start of the gradient.
    Object startPointObject = properties.get("startPoint");
    if (startPointObject instanceof HashMap) {
      startPoint = new TiPoint((HashMap) startPointObject, 0, 0);
    }

    // Load the 'endPoint' property which defines the end of the gradient.
    // Note: this is only used for linear gradient since Android does not
    // support an ending circle for radial gradients.
    Object endPointObject = properties.get("endPoint");
    if (endPointObject instanceof HashMap) {
      endPoint = new TiPoint((HashMap) endPointObject, 0, 1);
    }

    startRadius = TiConvert.toTiDimension(properties, "startRadius", TiDimension.TYPE_UNDEFINED);
    if (startRadius == null) {
      startRadius = DEFAULT_RADIUS;
    }

    Object colors = properties.get("colors");
    if (!(colors instanceof Object[])) {
      Log.w(LCAT, "Android does not support gradients without colors.");
      throw new IllegalArgumentException("Must provide an array of colors.");
    }
    loadColors((Object[]) colors);

    this.view = view;

    setShaderFactory(new GradientShaderFactory());
  }