0

画面のスナップショットを撮るときに問題が発生します。ビットマップは、ビューの下部以外のすべてをペイントします。レイアウトのコードと、スナップショット呼び出しからのコードを添付します。

このアクティビティには、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 にすることはできません」というエラーが表示されるので、どうすればよいでしょうか?

本当にありがとうございました!!!

4

2 に答える 2

1

解決策を見つけました。見つけるのは大変でしたが、とても簡単です。最初の LinearLayout (他のすべてを含むもの) に ID を追加するだけです。次に、ID でビューを検索するビットマップを作成できます。

私が追加するレイアウトで:android:id="@+id/mainLayout" そしてアクティビティで:

int id = R.id.mainLayout;
View view = findViewById(id);                   
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(getWindow().findViewById(id).getDrawingCache());    

サポートに感謝します。これが他の誰かに役立つことを願っています。

于 2012-11-06T10:31:47.800 に答える
0

BitMap のサイズを設定すると、問題が解決する場合があります。

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
bitmap = Bitmap.createBitmap(getWindow().getDecorView().getDrawingCache(), 0, 0, width, height);
于 2012-10-31T14:04:03.960 に答える