gpt4 book ai didi

android - 将 Activity/Fragment 的一部分保存为图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:58 25 4
gpt4 key购买 nike

我试图保存我的部分 Activity ,没有工具栏和状态栏。我现在拥有的代码可以保存整个屏幕。请引用下图。

enter image description here

我现在拥有的代码:

   llIDCardRootView = (LinearLayout) view.findViewById(R.id.ll_id_card_root_view);
llIDCardContainer = (LinearLayout) llIDCardRootView.findViewById(R.id.ll_id_card_view);

private void createBitmap() {

Log.d(Const.DEBUG, "Creating Bitmap");

Bitmap bmp;
//View v = llIDCardContainer.getRootView();
//View v = activity.getWindow().getDecorView().findViewById(android.R.id.content);
//View v = activity.findViewById(R.id.ll_id_card_root_view);
ViewGroup v = (ViewGroup) ((ViewGroup) activity
.findViewById(android.R.id.content)).getChildAt(0);

v.setDrawingCacheEnabled(true);
// v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
// v.buildDrawingCache(true);

bmp = Bitmap.createBitmap(v.getDrawingCache());

File directory = new File(Environment.getExternalStorageDirectory()
+ File.separator);
File file = new File(directory, FILE_NAME);

try {
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}

v.destroyDrawingCache();
v.setDrawingCacheEnabled(false);
}

正在保存的图像..

enter image description here

我怎样才能只从 fragment 中保存我需要的部分?

最佳答案

使用下面的函数将任何 View 保存到图像文件。如果需要保存在Fragment中,在fragment中调用下面的函数。

public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}

关于android - 将 Activity/Fragment 的一部分保存为图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com