下面列出了com.bumptech.glide.load.resource.SimpleResource#android.graphics.Picture 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates an overlay with a background color and a thumbnail for the cross profile apps
* animation.
*/
GraphicBuffer createCrossProfileAppsThumbnail(
@DrawableRes int thumbnailDrawableRes, Rect frame) {
final int width = frame.width();
final int height = frame.height();
final Picture picture = new Picture();
final Canvas canvas = picture.beginRecording(width, height);
canvas.drawColor(Color.argb(0.6f, 0, 0, 0));
final int thumbnailSize = mService.mContext.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.cross_profile_apps_thumbnail_size);
final Drawable drawable = mService.mContext.getDrawable(thumbnailDrawableRes);
drawable.setBounds(
(width - thumbnailSize) / 2,
(height - thumbnailSize) / 2,
(width + thumbnailSize) / 2,
(height + thumbnailSize) / 2);
drawable.setTint(mContext.getColor(android.R.color.white));
drawable.draw(canvas);
picture.endRecording();
return Bitmap.createBitmap(picture).createGraphicBufferHandle();
}
/**
* 截取所有网页内容到 Bitmap
*
* @return Bitmap
*/
Bitmap genCaptureBitmap() throws OutOfMemoryError {
// @todo Future versions of WebView may not support use on other threads.
try {
Picture picture = getWebView().capturePicture();
int height = picture.getHeight(), width = picture.getWidth();
if (height == 0 || width == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
} catch (NullPointerException e) {
return null;
}
}
/**
* 绘制坐标系
*
* @param coo 坐标系原点
* @param winSize 屏幕尺寸
* @param winSize 屏幕尺寸
*/
private static Picture getCoo(Point coo, Point winSize, boolean showText) {
Picture picture = new Picture();
Canvas recording = picture.beginRecording(winSize.x, winSize.y);
//初始化网格画笔
Paint paint = new Paint();
paint.setStrokeWidth(3);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
//设置虚线效果new float[]{可见长度, 不可见长度},偏移值
paint.setPathEffect(null);
//绘制直线
recording.drawPath(HelpPath.cooPath(coo, winSize), paint);
//左箭头
recording.drawLine(winSize.x, coo.y, winSize.x - 40, coo.y - 20, paint);
recording.drawLine(winSize.x, coo.y, winSize.x - 40, coo.y + 20, paint);
//下箭头
recording.drawLine(coo.x, winSize.y, coo.x - 20, winSize.y - 40, paint);
recording.drawLine(coo.x, winSize.y, coo.x + 20, winSize.y - 40, paint);
//为坐标系绘制文字
if (showText) {
drawText4Coo(recording, coo, winSize, paint);
}
return picture;
}
protected WebView.PictureListener getPictureListener() {
if (mPictureListener == null) {
mPictureListener = new WebView.PictureListener() {
@Override
public void onNewPicture(WebView webView, Picture picture) {
dispatchEvent(
webView,
new ContentSizeChangeEvent(
webView.getId(),
webView.getWidth(),
webView.getContentHeight()));
}
};
}
return mPictureListener;
}
protected WebView.PictureListener getPictureListener() {
if (mPictureListener == null) {
mPictureListener = new WebView.PictureListener() {
@Override
public void onNewPicture(WebView webView, Picture picture) {
dispatchEvent(
webView,
new ContentSizeChangeEvent(
webView.getId(),
webView.getWidth(),
webView.getContentHeight()));
}
};
}
return mPictureListener;
}
/**
* X5内核截取长图【暂时用不了】
* @param webView x5WebView的控件
* @return 返回bitmap对象
*
* 方法
* 1. 使用X5内核方法snapshotWholePage(Canvas, boolean, boolean);在X5内核中提供了一个截取整个
* WebView界面的方法snapshotWholePage(Canvas, boolean, boolean),但是这个方法有个缺点,
* 就是不以屏幕上WebView的宽高截图,只是以WebView的contentWidth和contentHeight为宽高截图,
* 所以截出来的图片会不怎么清晰,但作为缩略图效果还是不错了。
* 2. 使用capturePicture()截取清晰长图;如果想要在X5内核下截到清晰的长图,不能使用
* snapshotWholePage(),依然可以采用capturePicture()。X5内核下使用capturePicture()进行截图,
* 可以直接拿到WebView的清晰长图,但这是个Deprecated的方法,使用的时候要做好异常处理。
*/
@Deprecated
public static Bitmap captureX5Picture(WebView webView) {
if (webView == null) {
return null;
}
try {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
}
} catch (Exception e){
e.printStackTrace();
return null;
}
return null;
}
/**
* Enable the onNewPicture callback.
* @param enabled Flag to enable the callback.
* @param invalidationOnly Flag to call back only on invalidation without providing a picture.
*/
public void enableOnNewPicture(boolean enabled, boolean invalidationOnly) {
if (mNativeAwContents == 0) return;
if (invalidationOnly) {
mPictureListenerContentProvider = null;
} else if (enabled && mPictureListenerContentProvider == null) {
mPictureListenerContentProvider = new Callable<Picture>() {
@Override
public Picture call() {
return capturePicture();
}
};
}
mPictureListenerEnabled = enabled;
syncOnNewPictureStateToNative();
}
@SuppressWarnings({"WeakerAccess", "unused"})
public Picture renderToPicture(int widthInPixels, int heightInPixels, RenderOptions renderOptions)
{
Picture picture = new Picture();
Canvas canvas = picture.beginRecording(widthInPixels, heightInPixels);
if (renderOptions == null || renderOptions.viewPort == null) {
renderOptions = (renderOptions == null) ? new RenderOptions() : new RenderOptions(renderOptions);
renderOptions.viewPort(0f, 0f, (float) widthInPixels, (float) heightInPixels);
}
SVGAndroidRenderer renderer = new SVGAndroidRenderer(canvas, this.renderDPI);
renderer.renderDocument(this, renderOptions);
picture.endRecording();
return picture;
}
private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode,
int targetWidth, int targetHeight) throws SVGParseException {
// Util.debug("Parsing SVG...");
try {
long start = System.currentTimeMillis();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
final Picture picture = new Picture();
SVGHandler handler = new SVGHandler(picture, targetWidth, targetHeight);
handler.setColorSwap(searchColor, replaceColor);
handler.setWhiteMode(whiteMode);
xr.setContentHandler(handler);
xr.parse(new InputSource(in));
// Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis.");
SVG result = new SVG(picture, handler.bounds);
// Skip bounds if it was an empty pic
if (!Float.isInfinite(handler.limits.top)) {
result.setLimits(handler.limits);
}
return result;
} catch (Exception e) {
throw new SVGParseException(e);
}
}
private WebView.PictureListener getPictureListener() {
if (mPictureListener == null) {
mPictureListener = new WebView.PictureListener() {
@Override
public void onNewPicture(WebView webView, Picture picture) {
dispatchEvent(
webView,
new ContentSizeChangeEvent(
webView.getId(),
webView.getWidth(),
webView.getContentHeight()));
}
};
}
return mPictureListener;
}
public void setImage(Picture image) {
this.image = image;
if (mapView.getWidth() == 0) {
ViewTreeObserver vto = mapView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
if (!hasMeasured) {
initMapLayer();
hasMeasured = true;
}
return true;
}
});
} else {
initMapLayer();
}
}
private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode) throws SVGParseException {
// Util.debug("Parsing SVG...");
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
final Picture picture = new Picture();
SVGHandler handler = new SVGHandler(picture);
handler.setColorSwap(searchColor, replaceColor);
handler.setWhiteMode(whiteMode);
xr.setContentHandler(handler);
xr.parse(new InputSource(in));
// Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis.");
SVG result = new SVG(picture, handler.bounds);
// Skip bounds if it was an empty pic
if (!Float.isInfinite(handler.limits.top)) {
result.setLimits(handler.limits);
}
return result;
} catch (Exception e) {
throw new SVGParseException(e);
}
}
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);
}
}
@Override
public Bitmap onLoadBitmap(final Config pBitmapConfig) {
final Picture picture = this.mPicture;
if (picture == null) {
Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ".");
return null;
}
final Bitmap bitmap = Bitmap.createBitmap(this.mTextureWidth, this.mTextureHeight, pBitmapConfig);
final Canvas canvas = new Canvas(bitmap);
final float scaleX = (float)this.mTextureWidth / this.mPicture.getWidth();
final float scaleY = (float)this.mTextureHeight / this.mPicture.getHeight();
canvas.scale(scaleX, scaleY, 0, 0);
picture.draw(canvas);
return bitmap;
}
public boolean checkRenderView(WebView view)
{
if(state == null)
{
setBitmap(view);
return false;
}
else
{
Picture p = view.capturePicture();
Bitmap newState = Bitmap.createBitmap(p.getWidth(), p.getHeight(), Bitmap.Config.ARGB_8888);
boolean result = newState.equals(state);
newState.recycle();
return result;
}
}
/**
* Renders this SVG document to a Picture object using the specified view defined in the document.
* <p/>
* A View is an special element in a SVG document that describes a rectangular area in the document.
* Calling this method with a {@code viewId} will result in the specified view being positioned and scaled
* to the viewport. In other words, use {@link #renderToPicture()} to render the whole document, or use this
* method instead to render just a part of it.
*
* @param viewId the id of a view element in the document that defines which section of the document is to
* be visible.
* @param widthInPixels the width of the initial viewport
* @param heightInPixels the height of the initial viewport
* @return a Picture object suitable for later rendering using {@code Canvas.drawPicture()},
* or null if the viewId was not found.
*/
public Picture renderViewToPicture(String viewId, int widthInPixels, int heightInPixels) {
SvgObject obj = this.getElementById(viewId);
if (obj == null)
return null;
if (!(obj instanceof SVG.View))
return null;
SVG.View view = (SVG.View) obj;
if (view.viewBox == null) {
Log.w(TAG, "View element is missing a viewBox attribute.");
return null;
}
Picture picture = new Picture();
Canvas canvas = picture.beginRecording(widthInPixels, heightInPixels);
Box viewPort = new Box(0f, 0f, (float) widthInPixels, (float) heightInPixels);
SVGAndroidRenderer renderer = new SVGAndroidRenderer(canvas, viewPort, this.renderDPI);
renderer.renderDocument(this, view.viewBox, view.preserveAspectRatio, false);
picture.endRecording();
return picture;
}
public void applyFillToPath(Path path, Paint paint, Canvas context) {
context.save();
// clip to path
context.clipPath(path);
// do the gradient
Rect bounds = new Rect();
context.getClipBounds(bounds);
Picture image = imageForBounds(bounds);
// draw
if (image != null) {
// TODO - Blending mode? We may need to convert the Picture to a
// Bitmap and then call drawBitmap
context.drawPicture(image);
}
context.restore();
}
public boolean checkRenderView(AmazonWebView view)
{
if(state == null)
{
setBitmap(view);
return false;
}
else
{
Picture p = view.capturePicture();
Bitmap newState = Bitmap.createBitmap(p.getWidth(), p.getHeight(), Bitmap.Config.ARGB_8888);
boolean result = newState.equals(state);
newState.recycle();
return result;
}
}
@Override
public final void drawPicture(@NonNull Picture picture) {
picture.endRecording();
int restoreCount = save();
picture.draw(this);
restoreToCount(restoreCount);
}
public void postOnNewPicture(Callable<Picture> pictureProvider) {
if (mHasPendingOnNewPicture) return;
mHasPendingOnNewPicture = true;
long pictureTime = java.lang.Math.max(mLastPictureTime + ON_NEW_PICTURE_MIN_PERIOD_MILLIS,
SystemClock.uptimeMillis());
mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ON_NEW_PICTURE, pictureProvider),
pictureTime);
}
@Override
public final void drawPicture(@NonNull Picture picture, @NonNull RectF dst) {
save();
translate(dst.left, dst.top);
if (picture.getWidth() > 0 && picture.getHeight() > 0) {
scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight());
}
drawPicture(picture);
restore();
}
/**
* Get a copy of bitmap of given drawable, return null if intrinsic size is zero
*/
public static Bitmap createDrawableBitmap(Drawable drawable, View hostView) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width <= 0 || height <= 0) {
return null;
}
float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (width * height));
if (drawable instanceof BitmapDrawable && scale == 1f) {
// return same bitmap if scale down not needed
return ((BitmapDrawable) drawable).getBitmap();
}
int bitmapWidth = (int) (width * scale);
int bitmapHeight = (int) (height * scale);
final Picture picture = new Picture();
final Canvas canvas = picture.beginRecording(width, height);
// Do stuff with the canvas
Rect existingBounds = drawable.getBounds();
int left = existingBounds.left;
int top = existingBounds.top;
int right = existingBounds.right;
int bottom = existingBounds.bottom;
drawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
drawable.draw(canvas);
drawable.setBounds(left, top, right, bottom);
picture.endRecording();
return Bitmap.createBitmap(picture);
}
/**
* 对WebView进行截图
*
* @param webView
* @return
*/
private Bitmap captureWebView1(WebView webView) {
Picture snapShot = webView.capturePicture();
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
snapShot.draw(canvas);
return bmp;
}
@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);
}
private Bitmap captureWebView(WebView webView) {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
}
return null;
}
private static Bitmap createType1(WebView webView, FileHelper.OnSavedToGalleryListener listener) {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
Bitmap bitmap = null;
if (width > 0 && height > 0) {
bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
}
return bitmap;
}
/**
* 绘制picture
*
* @param canvas
* @param pictures
*/
public static void draw(Canvas canvas, Picture... pictures) {
if (!isDraw) {
return;
}
for (Picture picture : pictures) {
picture.draw(canvas);
}
}
/**
* 绘制网格
*
* @param winSize 屏幕尺寸
*/
private static Picture getGrid(Point winSize) {
Picture picture = new Picture();
Canvas recording = picture.beginRecording(winSize.x, winSize.y);
Paint paint = getHelpPint();
recording.drawPath(HelpPath.gridPath(50, winSize), paint);
return picture;
}
@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);
}
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;
}