我需要截取整个列表视图的屏幕截图,而不管屏幕上可见什么,但最后我得到的结果是Image,其中包含仅在显示屏中可见的细节。请帮我解决这个问题。
这是我的参考代码:
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TAG = getClass().getName();
screenShot = (Button) findViewById(R.id.screenshot_button);
listView = (ListView) findViewById(R.id.listView);
screenRoot = (LinearLayout) findViewById(R.id.screenRoot);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.layout(0, 0, totalWidth, totalHeight);
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = screenRoot.getMeasuredWidth();
int height = screenRoot.getMeasuredHeight();
//totalHeight = listView.getChildAt(0).getHeight();
//totalWidth = listView.getChildAt(0).getWidth();
listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// You only need this method to be called once
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Store the height in a class variable
int width = listView.getMeasuredWidth();
int height = listView.getMeasuredHeight();
totalHeight = height;
totalWidth = width;
}
});
screenShot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Inside on click",
Toast.LENGTH_SHORT).show();
//Bitmap bitmap = takeScreenshot();
//saveBitmap(bitmap);
Log.i("The Total Height is ",""+totalHeight);
Log.i("The Total Width is ",""+totalWidth);
createImage(totalHeight, totalWidth, listView,"screenshot_new");
}
});
}
public Bitmap takeScreenshot() {
Log.d(TAG, "Inside takeScreenShot method");
listView.setDrawingCacheEnabled(true);
listView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(listView.getDrawingCache());
// View rootView = findViewById(android.R.id.content).getRootView();
// rootView.setDrawingCacheEnabled(true);
return listView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
public File createImage(int height, int width, View view, String fileName) {
Bitmap bitmapCategory = getBitmapFromView(view, height, width);
return createFile(bitmapCategory, fileName);
}
public File createFile(Bitmap bitmap, String fileName) {
File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
// File reportImageFile = new File(sdcardPath + fileName + ".jpg");
File reportImageFile = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
try {
if (reportImageFile.isFile()) {
reportImageFile.delete();
}
if (reportImageFile.createNewFile()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = new FileOutputStream(reportImageFile);
fo.write(bytes.toByteArray());
bytes.close();
fo.close();
return reportImageFile;
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Unable to create Image.Try again", Toast.LENGTH_SHORT).show();
}
return null;
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
totalWidth = getTotalWidthofListView(listView);
// totalHeight = getTotalHeightofListView(listView);
//totalWidth = 2160;
totalHeight = 2160;
view.measure(MeasureSpec.makeMeasureSpec(totalWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
view.layout(0, 0, totalWidth, totalHeight);
view.draw(canvas);
return returnedBitmap;
}
public static int getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
return totalHeight;
}
public static int getTotalWidthofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalWidth = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalWidth += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalWidth));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalWidth
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
return totalWidth;
}