com.bumptech.glide.load.resource.SimpleResource#com.caverock.androidsvg.SVG源码实例Demo

下面列出了com.bumptech.glide.load.resource.SimpleResource#com.caverock.androidsvg.SVG 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: NaviBee   文件: GroupConversation.java
private static Bitmap imageFromString(String svgAsString) throws SVGParseException {

        SVG svg = SVG.getFromString(svgAsString);

        // Create a bitmap and canvas to draw onto
        float   svgWidth = (svg.getDocumentWidth() != -1) ? svg.getDocumentWidth() : 500f;
        float   svgHeight = (svg.getDocumentHeight() != -1) ? svg.getDocumentHeight() : 500f;

        Bitmap  newBM = Bitmap.createBitmap(Math.round(svgWidth),
                Math.round(svgHeight),
                Bitmap.Config.ARGB_8888);
        Canvas bmcanvas = new Canvas(newBM);

        // Clear background to white if you want
        bmcanvas.drawRGB(255, 255, 255);

        // Render our document onto our canvas
        svg.renderToCanvas(bmcanvas);

        return newBM;
    }
 
源代码2 项目: SvgGlidePlugins   文件: SvgUtils.java
public static void fix(@NonNull SVG svg) throws IOException {
    RectF viewBox = svg.getDocumentViewBox();
    float docWidth = svg.getDocumentWidth();
    float docHeight = svg.getDocumentHeight();

    if (viewBox == null) {
        if (docWidth > 0 && docHeight > 0) {
            svg.setDocumentViewBox(0F, 0F, docWidth, docHeight);
        } else {
            throw new IOException("SVG must have specify 'width' & 'height' tags or 'viewbox'");
        }

    } else if (docWidth <= 0 && docHeight <= 0) {
        svg.setDocumentWidth(viewBox.width());
        svg.setDocumentHeight(viewBox.height());

    } else if (docWidth <= 0) {
        svg.setDocumentWidth(aspectRation(viewBox) * docHeight);

    } else if (docHeight <= 0) {
        svg.setDocumentHeight(docWidth / aspectRation(viewBox));
    }
}
 
private void prepareSvg(@NonNull Resource<SVG> toTranscode, @Nullable Options options) {
    if (!(toTranscode instanceof SvgResource)) {
        return;
    }

    DownsampleStrategy strategy =
            options == null ? null : options.get(DownsampleStrategy.OPTION);
    if (strategy != null) {
        float scaleFactor = strategy.getScaleFactor(
                Math.round(toTranscode.get().getDocumentWidth()),
                Math.round(toTranscode.get().getDocumentHeight()),
                ((SvgResource) toTranscode).getWidth(),
                ((SvgResource) toTranscode).getHeight()
        );
        SvgUtils.scaleDocumentSize(toTranscode.get(), scaleFactor);
    }
}
 
源代码4 项目: Markwon   文件: SvgMediaDecoder.java
@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 float w = svg.getDocumentWidth();
    final float h = svg.getDocumentHeight();
    final float density = resources.getDisplayMetrics().density;

    final int width = (int) (w * density + .5F);
    final int height = (int) (h * density + .5F);

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    final Canvas canvas = new Canvas(bitmap);
    canvas.scale(density, density);
    svg.renderToCanvas(canvas);

    return new BitmapDrawable(resources, bitmap);
}
 
源代码5 项目: microMathematics   文件: CustomImageView.java
/**
 * Parcelable interface: procedure writes the formula state
 */
@Override
@SuppressLint("MissingSuperCall")
public Parcelable onSaveInstanceState()
{
    Bundle bundle = new Bundle();
    bundle.putString(STATE_IMAGE_TYPE, imageType.toString());
    if (externalUri != null)
    {
        bundle.putString(STATE_IMAGE_URI, externalUri.toString());
    }
    else if (imageType == ImageType.BITMAP && bitmap != null)
    {
        bundle.putString(STATE_IMAGE_BITMAP, getEncodedImage(bitmap));
    }
    else if (imageType == ImageType.SVG && svgData != null)
    {
        bundle.putString(STATE_IMAGE_SVG, svgData);
    }
    return bundle;
}
 
源代码6 项目: microMathematics   文件: CustomImageView.java
private void setSvg(String svgData)
{
    svg = null;
    try
    {
        svg = SVG.getFromString(svgData);
        originalWidth = (int) svg.getDocumentWidth();
        originalHeight = (int) svg.getDocumentHeight();
        svg.setDocumentWidth("100%");
        svg.setDocumentHeight("100%");
        svg.setDocumentViewBox(0, 0, originalWidth, originalHeight);
    }
    catch (SVGParseException e)
    {
        // nothing to do
    }
    if (svg != null)
    {
        this.svgData = svgData;
    }
    imageType = this.svg == null ? ImageType.NONE : ImageType.SVG;
}
 
源代码7 项目: proofmode   文件: UIHelpers.java
public static void populateContainerWithSVG(View rootView, int offsetX, int idSVG, int idContainer) {
	try {
		SVG svg = SVG.getFromResource(rootView.getContext(), idSVG);
		if (offsetX != 0) {
			RectF viewBox = svg.getDocumentViewBox();
			viewBox.offset(offsetX, 0);
			svg.setDocumentViewBox(viewBox.left, viewBox.top, viewBox.width(), viewBox.height());
		}
		SVGImageView svgImageView = new SVGImageView(rootView.getContext());
		svgImageView.setFocusable(false);
		svgImageView.setFocusableInTouchMode(false);
		svgImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		svgImageView.setSVG(svg);
		svgImageView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		ViewGroup layout = (ViewGroup) rootView.findViewById(idContainer);
		layout.addView(svgImageView);
	} catch (SVGParseException e) {
		e.printStackTrace();
	}
}
 
源代码8 项目: Carbon   文件: VectorDrawable.java
public VectorDrawable(Resources res, int resId) {
    if (resId == 0)
        return;
    try {
        SVG svg = cache.get(resId);
        if (svg == null) {
            svg = SVG.getFromResource(res, resId);
            cache.put(resId, svg);
        }
        float density = res.getDisplayMetrics().density;

        float width = svg.getDocumentViewBox().width();
        float height = svg.getDocumentViewBox().height();

        int intWidth = (int) (width * density);
        int intHeight = (int) (height * density);
        state = new VectorDrawableState(svg, intWidth, intHeight);
        setBounds(0, 0, state.intWidth, state.intHeight);
    } catch (SVGParseException e) {

    }
}
 
@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);

}
 
源代码10 项目: trekarta   文件: AndroidSvgBitmap.java
public static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent, int color) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(inputStream);
        Picture picture;
        if (color != 0) {
            RenderOptions renderOpts = RenderOptions.create().css("* { fill: #" + String.format("%06x", color & 0x00ffffff) + "; }");
            picture = svg.renderToPicture(renderOpts);
        } else {
            picture = svg.renderToPicture();
        }

        double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);

        float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent);

        android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]),
                (int) Math.ceil(bmpSize[1]), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(picture, new RectF(0, 0, bmpSize[0], bmpSize[1]));

        return bitmap;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
源代码11 项目: PowerFileExplorer   文件: SvgDecoder.java
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
源代码12 项目: PowerFileExplorer   文件: SvgDrawableTranscoder.java
@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);
}
 
源代码13 项目: GlideToVectorYou   文件: SvgDecoder.java
public Resource<SVG> decode(@NonNull InputStream source, int width, int height,
                            @NonNull Options options)
    throws IOException {
  try {
    SVG svg = SVG.getFromInputStream(source);
    return new SimpleResource<>(svg);
  } catch (SVGParseException ex) {
    throw new IOException("Cannot load SVG from stream", ex);
  }
}
 
源代码14 项目: GlideToVectorYou   文件: SvgDrawableTranscoder.java
@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);
}
 
源代码15 项目: SvgGlidePlugins   文件: SvgUtils.java
public static SVG getSvg(@NonNull File file) throws SVGParseException, IOException {
    if (!file.exists()) {
        throw new FileNotFoundException("File: '" + file.getAbsolutePath() + "' not exists");
    }

    try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
        return SVG.getFromInputStream(is);
    }
}
 
源代码16 项目: SvgGlidePlugins   文件: SvgUtils.java
public static SVG getSvg(@NonNull FileDescriptor descriptor)
        throws SVGParseException, IOException {

    try (InputStream is = new BufferedInputStream(new FileInputStream(descriptor))) {
        return SVG.getFromInputStream(is);
    }
}
 
源代码17 项目: SvgGlidePlugins   文件: SvgUtils.java
public static void scaleDocumentSize(
        @NonNull SVG svg,
        @FloatRange(from = 0, fromInclusive = false) float scale
) {
    svg.setDocumentWidth(svg.getDocumentWidth() * scale);
    svg.setDocumentHeight(svg.getDocumentHeight() * scale);
}
 
源代码18 项目: SvgGlidePlugins   文件: SvgUtils.java
@NonNull
public static Bitmap toBitmap(
        @NonNull SVG svg,
        @NonNull BitmapProvider provider,
        @NonNull Bitmap.Config config
) {
    int outImageWidth = Math.round(svg.getDocumentWidth());
    int outImageHeight = Math.round(svg.getDocumentHeight());
    Bitmap bitmap = provider.get(outImageWidth, outImageHeight, config);
    Canvas canvas = new Canvas(bitmap);
    svg.renderToCanvas(canvas);
    return bitmap;
}
 
源代码19 项目: SvgGlidePlugins   文件: ByteBufferSvgDecoder.java
@Override
SVG loadSvg(@NonNull ByteBuffer source, int width, int height, @NonNull Options options) throws SvgParseException {
    try (InputStream is = ByteBufferUtil.toStream(source)) {
        return SVG.getFromInputStream(is);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
源代码20 项目: SvgGlidePlugins   文件: SvgDecoder.java
@Nullable
@Override
public Resource<SVG> decode(@NonNull T source, int width, int height, @NonNull Options options)
        throws IOException {
    try {
        int sourceSize = getSize(source);
        SVG svg = loadSvg(source, width, height, options);
        SvgUtils.fix(svg);
        int[] sizes = getResourceSize(svg, width, height);
        return new SvgResource(svg, sizes[0], sizes[1], sourceSize);
    } catch (SvgParseException e) {
        throw new IOException("Cannot load SVG", e);
    }
}
 
源代码21 项目: SvgGlidePlugins   文件: SvgDecoder.java
private static int[] getResourceSize(@NonNull SVG svg, int width, int height) {
    int[] sizes = new int[]{width, height};
    if (width == Target.SIZE_ORIGINAL && height == Target.SIZE_ORIGINAL) {
        sizes[0] = Math.round(svg.getDocumentWidth());
        sizes[1] = Math.round(svg.getDocumentHeight());

    } else if (width == Target.SIZE_ORIGINAL) {
        sizes[0] = Math.round(svg.getDocumentAspectRatio() * height);

    } else if (height == Target.SIZE_ORIGINAL) {
        sizes[1] = Math.round(width / svg.getDocumentAspectRatio());
    }
    return sizes;
}
 
源代码22 项目: SvgGlidePlugins   文件: InputStreamSvgDecoder.java
@Override
SVG loadSvg(@NonNull InputStream source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromInputStream(source);
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
源代码23 项目: SvgGlidePlugins   文件: FileSvgDecoder.java
@Override
SVG loadSvg(@NonNull File source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SvgUtils.getSvg(source);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
源代码24 项目: SvgGlidePlugins   文件: RawResourceSvgDecoder.java
@Override
SVG loadSvg(@NonNull Uri source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromResource(mResources, ResourceUtils.getRawResourceId(mResources, source));
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
源代码25 项目: SvgGlidePlugins   文件: StringSvgDecoder.java
@Override
SVG loadSvg(@NonNull String source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromString(source);
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
@Override
SVG loadSvg(
        @NonNull ParcelFileDescriptor source,
        int width,
        int height,
        @NonNull Options options
) throws SvgParseException {
    return mDecoder.loadSvg(source.getFileDescriptor(), width, height, options);
}
 
源代码27 项目: SvgGlidePlugins   文件: FileDescriptorSvgDecoder.java
@Override
SVG loadSvg(
        @NonNull FileDescriptor source,
        int width,
        int height,
        @NonNull Options options
) throws SvgParseException {
    try {
        return SvgUtils.getSvg(source);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
源代码28 项目: SvgGlidePlugins   文件: SvgModule.java
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    registry.register(SVG.class, BitmapDrawable.class, new SvgBitmapDrawableTranscoder(context, glide))
            .append(SVG.class, SVG.class, UnitModelLoader.Factory.getInstance())
            .append(String.class, String.class, StringLoader.Factory.getInstance());
    registerDecoders(context, registry);
}
 
源代码29 项目: SvgGlidePlugins   文件: SvgModule.java
private void registerDecoders(@NonNull Context context, @NonNull Registry registry) {
    registry.append(REGISTRY, InputStream.class, SVG.class, new InputStreamSvgDecoder())
            .append(REGISTRY, File.class, SVG.class, new FileSvgDecoder())
            .append(REGISTRY, FileDescriptor.class, SVG.class, new FileDescriptorSvgDecoder())
            .append(REGISTRY, ParcelFileDescriptor.class, SVG.class, new ParcelFileDescriptorSvgDecoder())
            .append(REGISTRY, SVG.class, SVG.class, new UnitSVGDecoder())
            .append(REGISTRY, ByteBuffer.class, SVG.class, new ByteBufferSvgDecoder())
            .append(REGISTRY, String.class, SVG.class, new StringSvgDecoder())
            .append(REGISTRY, Uri.class, SVG.class, new RawResourceSvgDecoder(context));
}
 
源代码30 项目: SvgGlidePlugins   文件: SvgResource.java
public SvgResource(
        @NonNull SVG svg,
        @IntRange(from = 1) int width,
        @IntRange(from = 1) int height,
        @IntRange(from = 0) int size
) throws IOException {
    SvgUtils.fix(svg);
    mSvg = svg;
    mWidth = width;
    mHeight = height;
    mSize = size;
}