私は自分のアプリ画面のスクリーンショットをプログラム的に取り入れようとしています。レイアウトファイルには、いくつかのImageButtonとTextviewフィールドがあります。キャプチャ画面が発生すると、結果の画像にもボタンが含まれます。画像ボタンなしでスクリーンショットを撮る方法はありますか?以下は Imagebuttonsなしでスクリーンショットを取る必要があります
は意志activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.abc.def.MainActivity">
<include
layout="@layout/buttons_layout"
android:id="@+id/includedLayout"
android:visibility="visible"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:id="@+id/text1"
android:gravity="center"
android:textSize="32dp"
android:shadowColor="#edeff2"
android:textStyle="bold"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="25"
/>
</RelativeLayout>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<ImageButton
android:id="@+id/refreshButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="@drawable/refresh"
/>
<ImageButton
android:id="@+id/shareButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="@drawable/share"
/>
</LinearLayout>
</LinearLayout>
コードが
private void captureScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
すべてのヘルプスクリーンショット取るためのコードスニペットとレイアウトファイルです感謝する。ありがとう:)
'button.setVisibility(View.GONE)'はスクリーンショットの直前ですか?そして、あなたが完了したときにそれらを表示する –
@MichałKのように、スクリーンショットの前にボタンを隠し、スクリーンショットの後にボタンを表示します。しかし、私は 'button.setVisibility(View.INVISIBLE)'を 'View.INVISIBLE'として提案すると、ボタンに依存しているかもしれない他のビューを歪ませません。 –
Thanks MichalとAtif –