下面列出了怎么用android.graphics.drawable.PictureDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @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;
}
@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);
}
/**
* @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;
}
@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;
}
@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;
}
@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);
}
@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;
}
@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;
}
@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);
}
@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);
}
private void doRender()
{
if (svg == null)
return;
Picture picture = this.svg.renderToPicture(renderOptions);
setSoftwareLayerType();
setImageDrawable(new PictureDrawable(picture));
}
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;
}
/**
* 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;
// }
// }
// };
}
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;
}
/**
* 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;
// }
// }
// };
}
@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;
}
@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;
}
@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);
}
/**
* 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);
}
}
/**
* 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);
}
}
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);
}
}
/**
* 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;
// }
// }
// };
}
/**
* 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;
// }
// }
// };
}
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
mPicture = new Picture();
drawSomething(mPicture.beginRecording(200, 100));
mPicture.endRecording();
mDrawable = new PictureDrawable(mPicture);
}
/**
* 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;
// }
// }
// };
}
public static void justLoadImage(Activity activity, Uri uri, ImageView imageView){
GlideApp.with(activity)
.as(PictureDrawable.class)
.listener(new SvgSoftwareLayerSetter()).load(uri).into(imageView);
}
public RequestBuilder<PictureDrawable> getRequestBuilder() {
return requestBuilder;
}
private void createRequestBuilder(Context ctx){
requestBuilder = GlideApp.with(ctx)
.as(PictureDrawable.class)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.listener(new SvgSoftwareLayerSetter());
}
@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());
}
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);
}