试试这个。
CardView card = (CardView) findViewById(R.id.card);
现在只需将卡片传递给captureScreenShot()即可。它返回位图并保存该位图saveImage()。
您可以传递任何视图像RelativeLayout,LinearLayout等任何视图都可以传递给captureScreenShot()。
// Function which capture Screenshot
public Bitmap captureScreenShot(View view) {
/*
* Creating a Bitmap of view with ARGB_4444.
* */
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
Drawable backgroundDrawable = view.getBackground();
if (backgroundDrawable != null) {
backgroundDrawable.draw(canvas);
} else {
canvas.drawColor(Color.parseColor("#80000000"));
}
view.draw(canvas);
return bitmap;
}
// Function which Save image.
private void saveImage(Bitmap bitmap) {
File file = // Your Storage directory name + your filename
if (file == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
终于像这样调用这个函数。
saveImage(captureScreenShot(card));
现在设置你的形象像这样。
File file = new File(“yourImageFilePath”);
if(file.exists())
{
yourImageView.setImageURI(Uri.fromFile(file));
}
注意:如果setImageURI()不起作用,那么您可以使用下面的代码。
File file = new File(“yourImageFilePath”);
if(file.exists())
{
Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
yourImageView.setImageBitmap(bitmap);
}