背景
new一個有各種信息的View,不需要顯示給用戶,對這個View截圖。上傳分享等…
代碼實現
ViewUtils.layoutView(view, 290, 210);
需要放在所有子view都bind完成後再調用,否則界面會亂掉- 用ConstraintLayout佈局,call
ViewUtils.layoutView(view, 290, 210);
整個頁面也亂掉
public static void layoutView(View v, int width, int height) {
// validate view.width and view.height
v.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
// validate view.measurewidth and view.measureheight
v.measure(measuredWidth, measuredHeight);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
2.保存圖片到sdcard
public static Pair<Boolean, String> saveBitmapToSdCard(Context context, Bitmap srcBitmap, String name) {
boolean result = false;
// //創建位圖保存目錄
// String path = Environment.getExternalStorageDirectory() + "/temp/";
// File sd = new File(path);
// if (!sd.exists()) {
// sd.mkdir();
// }
// File file = new File(sd, name + ".jpg");
File cacheFile = context.getCacheDir();
File file = new File(cacheFile, name + ".jpg");
FileOutputStream fileOutputStream = null;
if (file.exists()) {
file.delete();
}
try {
fileOutputStream = new FileOutputStream(file);
srcBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
fileOutputStream.flush();
result = true;
} catch (Exception e) {
LOG.e("BitmapUtil", e.getMessage(), e);
} finally {
Util.closeIOQuietly(fileOutputStream);
}
return new Pair<>(result, file.getAbsolutePath());
}
final View view = View.inflate(context, R.layout.xxx, null);
ImageView icon = view.findViewById(R.id.iv);
GlideApp.with(icon)
.load(url)
.placeholder(resId)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(icon);
TextView tvName = view.findViewById(R.id.tv_name);
tvName.setText(mArgumentMap.get("name"));
TextView tvTitle = view.findViewById(R.id.tv_title);
tvTitle.setText(mArgumentMap.get("title"));
TextView tvDescription = view.findViewById(R.id.tv_description);
tvDescription.setText(mArgumentMap.get("description"));
ViewUtils.layoutView(view, 290, 210);
final boolean drawingCacheEnabled = true;
view.setDrawingCacheEnabled(drawingCacheEnabled);
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
view.buildDrawingCache(drawingCacheEnabled);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
view.setDrawingCacheEnabled(false);
} else {
bitmap = null;
}
if (bitmap != null) {
Pair<Boolean, String> stringPair = BitmapUtil.saveBitmapToSdCard(context, bitmap, "imo_zone");
if (stringPair != null && stringPair.first) {
//
}
} else {
IMOLOG.trace(TAG, "failed to save bitmap");
}
}
}, 200);