从code抓屏 [英] Screen capture from code
问题描述
我需要捕捉的屏幕截图present屏幕,所以我采用下面的code。在
I need to capture the screen shot of the present screen so I adopted the code below on the
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
LinearLayout v = (LinearLayout) findViewById(R.id.mainLayout);
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a
// dimension of 0,0 and the bitmap will
// be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.layout(0, 0, v.getWidth(), v.getHeight());
v.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); //
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
File file = new File(path, "screentest.jpg");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
Log.e("ImagePath", "Image Path : "
+ MediaStore.Images.Media.insertImage(
getContentResolver(), file.getAbsolutePath(),
file.getName(), file.getName()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
这code一直很好但对我来说,我需要在屏幕装载完成后立即捕捉到屏幕上。
This code has worked fine for me but I need to capture the screen just after the screen loading is finished.
我也曾尝试onPostCReate(),但一切都以徒劳的。
I have also tried the onPostCReate() but all goes to vain..
我也试着拨打code在的onPause()方法,但由于动画,屏幕triming有点从右侧和底部,所以,我不能,甚至去了...
I also tried to call the code in the onPause() method but due to animation, the screen is triming a bit from the right and the bottom portion... so I cant even go for it...
现在它轮到你来分享您的专业知识。
Now its your turn to share your expertise..
任何建议都大大AP preciated !!!!
Any suggestions are greatly appreciated!!!!
推荐答案
试试这个:
public void onCreate(Bundle savedInstanceState) {
LinearLayout v = (LinearLayout) findViewById(R.id.mainLayout);
v.setDrawingCacheEnabled(true);
v.post(new Runnable() {
public void run() {
// Code to take screenshot
});
}
}
这篇关于从code抓屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!