问题
The main issue is saving the whole scroll view as a bitmap image rather than just what appears on the screen. Is there a way to save whole scroll view, and if so how?
回答1:
Make a RelativeLayout or LinearLayout in your ScrollView to get Bitmap from Layout.
Organize this way:
public class ActivityA extends Activity {
LinearLayout PP_Ll;
Button btn_capture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PP_Ll = (RelativeLayout) findViewById(R.id.PP_Ll);
Button btn_capture= (Button) findViewById(R.id.btn_capture);
btn_capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
public Bitmap takeScreenshot() {
View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/Folder");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-" + n + ".jpg";
File file = new File(newDir, fotoname);
if (file.exists())
file.delete();
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(),
"Saved in folder: 'Folder'", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
}}
Your RelativeLayout will be saved in certain directory as a BitMap. Good luck.
Note: You have to insert your imageviews, textviews etc. into your RelativeLayout in .xml file that all can go into screenshot.
回答2:
try below code:-
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
see below link
Take a screenshot of a whole View
回答3:
Possible solution, you can create a emulator of larger screen size maybe 10inch, and run your app on it, If your listview fits into the screen, you can take a screen shot of it.
来源:https://stackoverflow.com/questions/23987199/capture-whole-scrollview-bigger-than-screen