我正在開發一個android應用程序,並試圖使下面的代碼有效。我想要做的是:截取整個活動的截圖,包括未顯示的文字(必須向上或向下滾動)。 這是截圖方法:如何在android studio中傳遞整個活動
public static Bitmap takeScreenshot(Activity activity){
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
Bitmap bmap = view.getDrawingCache();
Rect statusBar = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);
view.setDrawingCacheEnabled(false);
return snapshot;
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.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);
}
}
我想打電話給takeScreenshot以下功能,但我不知道如何在takeScreenshot的參數傳遞的活動。我試圖複製活動的名稱,但它不起作用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed__info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
id = intent.getStringExtra(Urls.MARKER_ID);
year = intent.getStringExtra(Urls.MARKER_Year);
info = intent.getStringExtra(Urls.MARKER_Info);
editTextName = (TextView) findViewById(R.id.markerYear);
editTextDesg = (TextView) findViewById(R.id.detailedInfo);
editTextName.setText(year);
editTextDesg.setText(info);
//Saving into picture
findViewById(R.id.download).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot(); // here's where i have to pass the activity
saveBitmap(bitmap);
}
});
}
'this'將代表沒有活動在這種情況下,因爲是這樣'onClick' – Pragnani
在這種情況下,這將引用您的活動。 takeScreenshot(Activity.this)將活動替換爲您的活動名稱。 –
我不是OP,我已經糾正你的答案..我試圖糾正你,而不是給downvote – Pragnani