android.graphics.drawable.PictureDrawable Java Examples

The following examples show how to use android.graphics.drawable.PictureDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @returns A {@link Bitmap} for a {@link Drawable}.
 */
public static Bitmap drawableToBitmap(final Drawable drawable) {
    if (null == drawable) return null;
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    final Bitmap bitmap = Bitmap.createBitmap(Math.max(0, drawable.getIntrinsicWidth()),
            Math.max(0, drawable.getIntrinsicHeight()), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);

    // PictureDrawable's get handled separately.
    if (drawable instanceof PictureDrawable) {
        canvas.drawPicture(((PictureDrawable) drawable).getPicture());
        return bitmap;
    }

    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
 
Example #2
Source File: ImageZoomPresenter.java    From incubator-taverna-mobile with Apache License 2.0 6 votes vote down vote up
@Override
public void attachView(ImageZoomMvpView mvpView) {

    super.attachView(mvpView);

    requestBuilder = Glide.with(getMvpView().getAppContext())
            .using(Glide.buildStreamModelLoader(Uri.class,
                    getMvpView().getAppContext()), InputStream.class)
            .from(Uri.class)
            .as(SVG.class)
            .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
            .sourceEncoder(new StreamEncoder())
            .cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
            .decoder(new SvgDecoder())
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.placeholder)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .animate(android.R.anim.fade_in);

}
 
Example #3
Source File: Utils.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @returns A {@link Bitmap} for a {@link Drawable}.
 */
public static Bitmap drawableToBitmap(final Drawable drawable) {
    if (null == drawable) return null;
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    final Bitmap bitmap = Bitmap.createBitmap(Math.max(0, drawable.getIntrinsicWidth()),
            Math.max(0, drawable.getIntrinsicHeight()), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);

    // PictureDrawable's get handled separately.
    if (drawable instanceof PictureDrawable) {
        canvas.drawPicture(((PictureDrawable) drawable).getPicture());
        return bitmap;
    }

    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
 
Example #4
Source File: PdDroidPatchView.java    From PdDroidPublisher with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap picture2Bitmap(Picture picture){
    PictureDrawable pictureDrawable = new PictureDrawable(picture);
    Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
    //Log.e(TAG, "picture size: " + pictureDrawable.getIntrinsicWidth() + " " + pictureDrawable.getIntrinsicHeight());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawPicture(pictureDrawable.getPicture());
    return bitmap;
}
 
Example #5
Source File: SVG.java    From CustomShapeImageView with Apache License 2.0 5 votes vote down vote up
/**
     * Create a picture drawable from the SVG.
     * @return the PictureDrawable.
     */
    public PictureDrawable createPictureDrawable() {
        return new PictureDrawable(picture);
//        return new PictureDrawable(picture) {
//            @Override
//            public int getIntrinsicWidth() {
//                if (bounds != null) {
//                    return (int) bounds.width();
//                } else if (limits != null) {
//                    return (int) limits.width();
//                } else {
//                    return -1;
//                }
//            }
//
//            @Override
//            public int getIntrinsicHeight() {
//                if (bounds != null) {
//                    return (int) bounds.height();
//                } else if (limits != null) {
//                    return (int) limits.height();
//                } else {
//                    return -1;
//                }
//            }
//        };
    }
 
Example #6
Source File: Pictures.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
public SampleView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);

    mPicture = new Picture();
    drawSomething(mPicture.beginRecording(200, 100));
    mPicture.endRecording();

    mDrawable = new PictureDrawable(mPicture);
}
 
Example #7
Source File: SVG.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
     * Create a picture drawable from the SVG.
     * @return the PictureDrawable.
     */
    public PictureDrawable createPictureDrawable() {
        return new PictureDrawable(picture);
//        return new PictureDrawable(picture) {
//            @Override
//            public int getIntrinsicWidth() {
//                if (bounds != null) {
//                    return (int) bounds.width();
//                } else if (limits != null) {
//                    return (int) limits.width();
//                } else {
//                    return -1;
//                }
//            }
//
//            @Override
//            public int getIntrinsicHeight() {
//                if (bounds != null) {
//                    return (int) bounds.height();
//                } else if (limits != null) {
//                    return (int) limits.height();
//                } else {
//                    return -1;
//                }
//            }
//        };
    }
 
Example #8
Source File: SVG.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
     * Create a picture drawable from the SVG.
     * @return the PictureDrawable.
     */
    public PictureDrawable createPictureDrawable() {
        return new PictureDrawable(picture);
//        return new PictureDrawable(picture) {
//            @Override
//            public int getIntrinsicWidth() {
//                if (bounds != null) {
//                    return (int) bounds.width();
//                } else if (limits != null) {
//                    return (int) limits.width();
//                } else {
//                    return -1;
//                }
//            }
//
//            @Override
//            public int getIntrinsicHeight() {
//                if (bounds != null) {
//                    return (int) bounds.height();
//                } else if (limits != null) {
//                    return (int) limits.height();
//                } else {
//                    return -1;
//                }
//            }
//        };
    }
 
Example #9
Source File: SVGImageView.java    From XDroidAnimation with Apache License 2.0 5 votes vote down vote up
public void setImageFile(String filename) {
    try {
        SVG svg = SVG.getFromFile(getContext(), filename);
        setSoftwareLayerType();
        setImageDrawable(new PictureDrawable(svg.renderToPicture()));
    } catch (Exception e) {
        Log.w("SVGImageView", "Unable to find asset file: " + filename, e);
    }
}
 
Example #10
Source File: SVGImageView.java    From XDroidAnimation with Apache License 2.0 5 votes vote down vote up
/**
 * Load an SVG image from the given asset filename.
 */
public void setImageAsset(String filename) {
    try {
        SVG svg = SVG.getFromAsset(getContext().getAssets(), filename);
        setSoftwareLayerType();
        setImageDrawable(new PictureDrawable(svg.renderToPicture()));
    } catch (Exception e) {
        Log.w("SVGImageView", "Unable to find asset file: " + filename, e);
    }
}
 
Example #11
Source File: SVGImageView.java    From XDroidAnimation with Apache License 2.0 5 votes vote down vote up
/**
 * Load an SVG image from the given resource id.
 */
@Override
public void setImageResource(int resourceId) {
    try {
        SVG svg = SVG.getFromResource(getContext(), resourceId);
        setSoftwareLayerType();
        setImageDrawable(new PictureDrawable(svg.renderToPicture()));
    } catch (SVGParseException e) {
        Log.w("SVGImageView", "Unable to find resource: " + resourceId, e);
    }
}
 
Example #12
Source File: SvgDrawableTranscoder.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
 
Example #13
Source File: SvgSoftwareLayerSetter.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onResourceReady(PictureDrawable resource, T model
        , Target<PictureDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
        view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
    }
    return false;
}
 
Example #14
Source File: SvgSoftwareLayerSetter.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onException(Exception e, T model, Target<PictureDrawable> target,
                           boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
        view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
    }
    return false;
}
 
Example #15
Source File: SVG.java    From PdDroidPublisher with GNU General Public License v3.0 5 votes vote down vote up
/**
     * Create a picture drawable from the SVG.
     * @return the PictureDrawable.
     */
    public PictureDrawable createPictureDrawable() {
        return new PictureDrawable(picture);
//        return new PictureDrawable(picture) {
//            @Override
//            public int getIntrinsicWidth() {
//                if (bounds != null) {
//                    return (int) bounds.width();
//                } else if (limits != null) {
//                    return (int) limits.width();
//                } else {
//                    return -1;
//                }
//            }
//
//            @Override
//            public int getIntrinsicHeight() {
//                if (bounds != null) {
//                    return (int) bounds.height();
//                } else if (limits != null) {
//                    return (int) limits.height();
//                } else {
//                    return -1;
//                }
//            }
//        };
    }
 
Example #16
Source File: SvgSoftwareLayerSetter.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onException(Exception e, T model, Target<PictureDrawable> target, boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
        view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
    }
    return false;
}
 
Example #17
Source File: SVG.java    From ZzBeeLayout with Apache License 2.0 5 votes vote down vote up
/**
     * Create a picture drawable from the SVG.
     * @return the PictureDrawable.
     */
    public PictureDrawable createPictureDrawable() {
        return new PictureDrawable(picture);
//        return new PictureDrawable(picture) {
//            @Override
//            public int getIntrinsicWidth() {
//                if (bounds != null) {
//                    return (int) bounds.width();
//                } else if (limits != null) {
//                    return (int) limits.width();
//                } else {
//                    return -1;
//                }
//            }
//
//            @Override
//            public int getIntrinsicHeight() {
//                if (bounds != null) {
//                    return (int) bounds.height();
//                } else if (limits != null) {
//                    return (int) limits.height();
//                } else {
//                    return -1;
//                }
//            }
//        };
    }
 
Example #18
Source File: ViewUtils.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap pictureToBitmap(final Picture picture, final int w, final int h)
{
    final PictureDrawable pd = new PictureDrawable(picture);
    final Bitmap bitmap = Bitmap.createBitmap(w, h, getBitmapConfig());
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawPicture(pd.getPicture());
    return bitmap;
}
 
Example #19
Source File: SVGImageView.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private void  doRender()
{
   if (svg == null)
      return;
   Picture  picture = this.svg.renderToPicture(renderOptions);
   setSoftwareLayerType();
   setImageDrawable(new PictureDrawable(picture));
}
 
Example #20
Source File: SvgPictureMediaDecoder.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final Picture picture = svg.renderToPicture();
    return new PictureDrawable(picture);
}
 
Example #21
Source File: SvgDrawableTranscoder.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Resource<PictureDrawable> transcode(@NonNull Resource<SVG> toTranscode,
                                           @NonNull Options options) {
  SVG svg = toTranscode.get();
  Picture picture = svg.renderToPicture();
  PictureDrawable drawable = new PictureDrawable(picture);
  return new SimpleResource<>(drawable);
}
 
Example #22
Source File: SvgSoftwareLayerSetter.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onResourceReady(PictureDrawable resource, Object model,
                               Target<PictureDrawable> target, DataSource dataSource, boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);

    if (customListener != null) {
        customListener.onResourceReady();
    }

    return false;
}
 
Example #23
Source File: SvgSoftwareLayerSetter.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLoadFailed(GlideException e, Object model, Target<PictureDrawable> target,
                            boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    view.setLayerType(ImageView.LAYER_TYPE_NONE, null);

    if (customListener != null) {
        customListener.onLoadFailed();
    }
    return false;
}
 
Example #24
Source File: SvgDrawableTranscoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
 
Example #25
Source File: SvgSoftwareLayerSetter.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onResourceReady(PictureDrawable resource, T model, Target<PictureDrawable> target,
                               boolean isFromMemoryCache, boolean isFirstResource) {
    ImageView view = ((ImageViewTarget<?>) target).getView();
    if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
        view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
    }
    return false;
}
 
Example #26
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
private Drawable rasterizeSVG(MapMarker aiMarker, SVG markerSvg) {
  SVG.Svg svg = markerSvg.getRootElement();
  final float density = view.getContext().getResources().getDisplayMetrics().density;
  float height = aiMarker.Height() <= 0 ? getBestGuessHeight(svg) : aiMarker.Height();
  float width = aiMarker.Width() <= 0 ? getBestGuessWidth(svg) : aiMarker.Width();
  float scaleH = height / getBestGuessHeight(svg);
  float scaleW = width / getBestGuessWidth(svg);
  float scale = (float) Math.sqrt(scaleH * scaleH + scaleW * scaleW);

  // update fill color of SVG <path>
  Paint fillPaint = new Paint();
  Paint strokePaint = new Paint();
  PaintUtil.changePaint(fillPaint, aiMarker.FillColor());
  PaintUtil.changePaint(strokePaint, aiMarker.StrokeColor());
  SVG.Length strokeWidth = new SVG.Length(aiMarker.StrokeWidth() / scale);
  for (SVG.SvgObject element : svg.getChildren()) {
    if (element instanceof SVG.SvgConditionalElement) {
      SVG.SvgConditionalElement path = (SVG.SvgConditionalElement) element;
      path.baseStyle.fill = new SVG.Colour(fillPaint.getColor());
      path.baseStyle.fillOpacity = fillPaint.getAlpha()/255.0f;
      path.baseStyle.stroke = new SVG.Colour(strokePaint.getColor());
      path.baseStyle.strokeOpacity = strokePaint.getAlpha()/255.0f;
      path.baseStyle.strokeWidth = strokeWidth;
      path.baseStyle.specifiedFlags = 0x3d;
      if (path.style != null) {
        if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
          path.style.fill = new SVG.Colour(fillPaint.getColor());
          path.style.specifiedFlags |= SPECIFIED_FILL;
        }
        if ((path.style.specifiedFlags & SPECIFIED_FILL_OPACITY) == 0) {
          path.style.fillOpacity = fillPaint.getAlpha()/255.0f;
          path.style.specifiedFlags |= SPECIFIED_FILL_OPACITY;
        }
        if ((path.style.specifiedFlags & SPECIFIED_STROKE) == 0) {
          path.style.stroke = new SVG.Colour(strokePaint.getColor());
          path.style.specifiedFlags |= SPECIFIED_STROKE;
        }
        if ((path.style.specifiedFlags & SPECIFIED_STROKE_OPACITY) == 0) {
          path.style.strokeOpacity = strokePaint.getAlpha()/255.0f;
          path.style.specifiedFlags |= SPECIFIED_STROKE_OPACITY;
        }
        if ((path.style.specifiedFlags & SPECIFIED_STROKE_WIDTH) == 0) {
          path.style.strokeWidth = strokeWidth;
          path.style.specifiedFlags |= SPECIFIED_STROKE_WIDTH;
        }
      }
    }
  }

  // draw SVG to Picture and create a BitmapDrawable for rendering
  Picture picture = markerSvg.renderToPicture();
  Picture scaledPicture = new Picture();
  Canvas canvas = scaledPicture.beginRecording((int)((width + 2.0f * aiMarker.StrokeWidth()) * density),
      (int)((height + 2.0f * aiMarker.StrokeWidth()) * density));
  canvas.scale(density * scaleW, density * scaleH);
  canvas.translate(strokeWidth.floatValue(), strokeWidth.floatValue());
  picture.draw(canvas);
  scaledPicture.endRecording();
  return new PictureDrawable(scaledPicture);
}
 
Example #27
Source File: SvgModule.java    From GlideToVectorYou with Apache License 2.0 4 votes vote down vote up
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide,
    @NonNull Registry registry) {
  registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
      .append(InputStream.class, SVG.class, new SvgDecoder());
}
 
Example #28
Source File: GlideToVectorYou.java    From GlideToVectorYou with Apache License 2.0 4 votes vote down vote up
private void createRequestBuilder(Context ctx){
    requestBuilder = GlideApp.with(ctx)
            .as(PictureDrawable.class)
            .diskCacheStrategy(DiskCacheStrategy.DATA)
            .listener(new SvgSoftwareLayerSetter());
}
 
Example #29
Source File: GlideToVectorYou.java    From GlideToVectorYou with Apache License 2.0 4 votes vote down vote up
public RequestBuilder<PictureDrawable> getRequestBuilder() {
    return requestBuilder;
}
 
Example #30
Source File: GlideToVectorYou.java    From GlideToVectorYou with Apache License 2.0 4 votes vote down vote up
public static void justLoadImage(Activity activity, Uri uri, ImageView imageView){
    GlideApp.with(activity)
            .as(PictureDrawable.class)
            .listener(new SvgSoftwareLayerSetter()).load(uri).into(imageView);
}