下面列出了com.bumptech.glide.load.resource.SimpleResource#com.caverock.androidsvg.SVGParseException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public GroupConversation(String id, Date readTimestamp, Date createTimestamp, String name, String icon, Map<String, Boolean> users, String creator) {
super(id, readTimestamp, createTimestamp);
this.name = name;
for (String user:users.keySet()){
members.add(user);
}
this.creator = creator;
this.createDate = createTimestamp;
// generate icon using conversation id
try {
String hash = HashUtilitiesKt.sha256String(id);
String svgString = Jdenticon.Companion.toSvg(hash, 256, 0.08f);
iconBitmap = imageFromString(svgString);
} catch (SVGParseException e) {
iconBitmap = null;
}
}
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;
}
@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);
}
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;
}
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();
}
}
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) {
}
}
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);
}
}
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);
}
}
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);
}
}
public static SVG getSvg(@NonNull FileDescriptor descriptor)
throws SVGParseException, IOException {
try (InputStream is = new BufferedInputStream(new FileInputStream(descriptor))) {
return SVG.getFromInputStream(is);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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 FileDescriptor source,
int width,
int height,
@NonNull Options options
) throws SvgParseException {
try {
return SvgUtils.getSvg(source);
} catch (IOException | SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
public void setDataSource(@NonNull DataSource source) throws IOException {
try {
mSVG = SVG.getFromInputStream(source.toStream());
} catch (SVGParseException e) {
e.printStackTrace();
}
}
@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);
}
/**
* Loading the svg from the resources.
*
* @param context Context object to get the resources.
* @param svgResource int resource id of the svg.
*/
public void load(Context context, int svgResource) {
if (mSvg != null) return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
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);
}
}
/**
* Loading the svg from the resources.
*
* @param context Context object to get the resources.
* @param svgResource int resource id of the svg.
*/
public void load(Context context, int svgResource) {
if (mSvg != null)
return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
@Override
public CloseableImage decode(
EncodedImage encodedImage,
int length,
QualityInfo qualityInfo,
ImageDecodeOptions options) {
try {
SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
return new CloseableSvgImage(svg);
} catch (SVGParseException e) {
e.printStackTrace();
}
return null;
}
public void load(Context context, int svgResource) {
if (mSvg != null) return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
public void load(Context context, int svgResource) {
if (mSvg != null) return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
public void load(Context context, int svgResource) {
if (mSvg != null) return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
public void load(Context context, int svgResource) {
if (mSvg != null) return;
try {
mSvg = SVG.getFromResource(context, svgResource);
mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
} catch (SVGParseException e) {
Log.e(LOG_TAG, "Could not load specified SVG resource", e);
}
}
public static XulDrawable buildSVGDrawable(InputStream stream, String url, String imageKey, int width, int height) {
if (stream == null) {
return null;
}
XulSVGDrawable drawable = new XulSVGDrawable();
try {
drawable._svg = SVG.getFromInputStream(stream);
} catch (SVGParseException e) {
e.printStackTrace();
return null;
}
float offsetX = 0;
float offsetY = 0;
float xScalar = 1.0f;
float yScalar = 1.0f;
RectF documentViewBox = drawable._svg.getDocumentViewBox();
if (documentViewBox == null) {
float documentWidth = drawable._svg.getDocumentWidth();
float documentHeight = drawable._svg.getDocumentHeight();
if (documentWidth <= 0 && documentHeight <= 0) {
documentWidth = 256;
documentHeight = 256;
drawable._svg.setDocumentWidth(documentWidth);
drawable._svg.setDocumentHeight(documentHeight);
}
drawable._svg.setDocumentViewBox(0, 0, documentWidth, documentHeight);
documentViewBox = drawable._svg.getDocumentViewBox();
}
int docWidth = XulUtils.roundToInt(documentViewBox.width());
int docHeight = XulUtils.roundToInt(documentViewBox.height());
if (width == 0 && height == 0) {
width = docWidth;
height = docHeight;
} else if (width == 0) {
xScalar = yScalar = (float)(height)/docHeight;
width = XulUtils.roundToInt(docWidth*xScalar);
docWidth = width;
docHeight = height;
} else if (height == 0) {
xScalar = yScalar = (float)(width)/docWidth;
height = XulUtils.roundToInt(docHeight*xScalar);
docWidth = width;
docHeight = height;
} else {
float wScalar = (float) width / docWidth;
float hScalar = (float) height / docHeight;
if (wScalar > hScalar) {
docWidth *= wScalar;
docHeight *= wScalar;
xScalar = yScalar = wScalar;
} else {
docWidth *= hScalar;
docHeight *= hScalar;
xScalar = yScalar = hScalar;
}
offsetX = (docWidth - width) / 2.0f;
offsetY = (docHeight - height) / 2.0f;
}
XulUtils.ticketMarker ticketMarker = new XulUtils.ticketMarker("BENCH!!!", true);
ticketMarker.mark();
drawable._cachedBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(drawable._cachedBmp);
c.translate(-offsetX, -offsetY);
c.scale(xScalar, yScalar);
drawable._svg.renderToCanvas(c, documentViewBox);
ticketMarker.mark("svg");
Log.d("BENCH", ticketMarker.toString());
drawable._url = url;
drawable._key = imageKey;
return drawable;
}