画面のスナップショットを撮るときに問題が発生します。ビットマップは、ビューの下部以外のすべてをペイントします。レイアウトのコードと、スナップショット呼び出しからのコードを添付します。
このアクティビティには、TabListenersを含むアクション バーもあります。そのため、アクション バーには 3 つのタブといくつかのボタンがあります (スナップショットに表示されます。スナップショットでそれらを削除できればいいのですが、それらはまったく必要ないからです ^^U しかし、それはそうではありません重要なこと)。
スナップショット:
private void snapShot() {
Log.d("On snapShot","At beggining");
Bitmap bitmap;
getWindow().getDecorView().setDrawingCacheEnabled(true);
try{
bitmap = Bitmap.createBitmap(getWindow().getDecorView().getDrawingCache());
}catch (IllegalStateException e){
bitmap = null;
}
try{
Log.d("On snapShot", "inside Try");
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root,"pic.png");
FileOutputStream out = new FileOutputStream(pic);
bitmap.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
}catch(IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
getWindow().getDecorView().setDrawingCacheEnabled(false);
}
レイアウト:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="20"
>
<com.example.daemon.views.CaptionView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/legendView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.daemon.views.CaptionView>
<View
android:id="@+id/lineSeparation"
android:background="#F000"
android:layout_marginTop="1dp"
android:layout_height="1dp"
android:layout_width="match_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.example.daemon.views.ChartView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewChart"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.example.daemon.views.ChartView>
</LinearLayout>
</LinearLayout>
アクション バーとタブを作成するコードをもう少し追加します。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // Set the application works only on Landscape orientation.
actionBar = getActionBar(); // Creates the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Log.i(TAG, "On create");
String label1 = "one day";
Tab tab = actionBar.newTab(); // Creates a tab
tab.setText(label1);
// Links the tab created with an existing Fragment
TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);
tab.setTabListener(tl); // Makes the tab respond the touch events.
actionBar.addTab(tab); // Add the tab to the action bar.
スナップショットをプッシュするときの PNG:
それがどのように見えるべきか、私のカメラからの写真:
問題 (私が思うに) は、ビットマップが作成されたときの最大の高さが 732 (ビューと同じ) であるが、ビューを上からキャプチャし始める (ビューにないアクション バーを含む) ことです。最初の 732 行をペイントします。ポイントは、アクションバーの後に開始するように言った場合、「y + 高さは > bitmap.height にすることはできません」というエラーが表示されるので、どうすればよいでしょうか?
本当にありがとうございました!!!