下面列出了android.graphics.Bitmap.Config#HARDWARE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Loads a task from the disk.
* <p>
* Do not hold the window manager lock when calling this method, as we directly read data from
* disk here, which might be slow.
*
* @param taskId The id of the task to load.
* @param userId The id of the user the task belonged to.
* @param reducedResolution Whether to load a reduced resolution version of the snapshot.
* @return The loaded {@link TaskSnapshot} or {@code null} if it couldn't be loaded.
*/
TaskSnapshot loadTask(int taskId, int userId, boolean reducedResolution) {
final File protoFile = mPersister.getProtoFile(taskId, userId);
final File bitmapFile = reducedResolution
? mPersister.getReducedResolutionBitmapFile(taskId, userId)
: mPersister.getBitmapFile(taskId, userId);
if (bitmapFile == null || !protoFile.exists() || !bitmapFile.exists()) {
return null;
}
try {
final byte[] bytes = Files.readAllBytes(protoFile.toPath());
final TaskSnapshotProto proto = TaskSnapshotProto.parseFrom(bytes);
final Options options = new Options();
options.inPreferredConfig = Config.HARDWARE;
final Bitmap bitmap = BitmapFactory.decodeFile(bitmapFile.getPath(), options);
if (bitmap == null) {
Slog.w(TAG, "Failed to load bitmap: " + bitmapFile.getPath());
return null;
}
final GraphicBuffer buffer = bitmap.createGraphicBufferHandle();
if (buffer == null) {
Slog.w(TAG, "Failed to retrieve gralloc buffer for bitmap: "
+ bitmapFile.getPath());
return null;
}
return new TaskSnapshot(buffer, proto.orientation,
new Rect(proto.insetLeft, proto.insetTop, proto.insetRight, proto.insetBottom),
reducedResolution, reducedResolution ? REDUCED_SCALE : 1f,
proto.isRealSnapshot, proto.windowingMode, proto.systemUiVisibility,
proto.isTranslucent);
} catch (IOException e) {
Slog.w(TAG, "Unable to load task snapshot data for taskId=" + taskId);
return null;
}
}
private Bitmap drawOnSFCanvas(Bitmap bitmap) {
if (VERSION.SDK_INT < 26 || bitmap.getConfig() != Config.HARDWARE) {
return bitmap;
}
Bitmap sfBitmap = bitmap.copy(Config.ARGB_8888, false);
this.bitmapWeakSet.add(sfBitmap);
return sfBitmap;
}
private Paint replaceBitmapShader(Paint paint) {
if (paint == null) {
return null;
}
if (VERSION.SDK_INT >= 26 && (paint.getShader() instanceof BitmapShader)) {
Paint saPaint = new Paint(paint);
BitmapShader userBitmapShader = (BitmapShader) saPaint.getShader();
try {
Field mBitmap = BitmapShader.class.getField("mBitmap");
mBitmap.setAccessible(true);
if (((Bitmap) mBitmap.get(userBitmapShader)).getConfig() == Config.HARDWARE) {
Field mTileX = BitmapShader.class.getDeclaredField("mTileX");
Field mTileY = BitmapShader.class.getDeclaredField("mTileY");
mTileX.setAccessible(true);
mTileY.setAccessible(true);
Bitmap sfBitmap = ((Bitmap) mBitmap.get(userBitmapShader)).copy(Config.ARGB_8888, false);
this.bitmapWeakSet.add(sfBitmap);
Constructor<BitmapShader> constructor = BitmapShader.class.getDeclaredConstructor(new Class[]{Bitmap.class, Integer.TYPE, Integer.TYPE});
constructor.setAccessible(true);
BitmapShader bitmapShaderShader = (BitmapShader) constructor.newInstance(new Object[]{sfBitmap, mTileX.get(userBitmapShader), mTileY.get(userBitmapShader)});
Matrix matrix = new Matrix();
paint.getShader().getLocalMatrix(matrix);
bitmapShaderShader.setLocalMatrix(matrix);
saPaint.setShader(bitmapShaderShader);
return saPaint;
}
} catch (Exception e) {
SALog.i(TAG, e.toString());
}
}
return paint;
}