2016-11-02 7 views
0

私は自分のアプリ画面のスクリーンショットをプログラム的に取り入れようとしています。レイアウトファイルには、いくつかの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); 

     } 

    } 

すべてのヘルプスクリーンショット取るためのコードスニペットとレイアウトファイルです感謝する。ありがとう:)

+1

'button.setVisibility(View.GONE)'はスクリーンショットの直前ですか?そして、あなたが完了したときにそれらを表示する –

+1

@MichałKのように、スクリーンショットの前にボタンを隠し、スクリーンショットの後にボタンを表示します。しかし、私は 'button.setVisibility(View.INVISIBLE)'を 'View.INVISIBLE'として提案すると、ボタンに依存しているかもしれない他のビューを歪ませません。 –

+0

Thanks MichalとAtif –

答えて

1

スクリーンショットを撮る直前に画像ボタンを非表示にしてから、表示させることができます。

private void captureScreen() { 
    //make unwanted views invisible 
    findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE); 
    findViewById(R.id.shareButton).setVisibility(View.INVISIBLE); 
    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); 

    } 
    //make those views visible again 
    findViewById(R.id.refreshButton).setVisibility(View.VISIBLE); 
    findViewById(R.id.shareButton).setVisibility(View.VISIBLE); 
} 
+0

ありがとうrana_sadam、これは完全に働いた:) –

+0

いつでも@AshishBanerjee :) –

3

私はそれがフルスクリーンであるように見えるだけのテキストビューであれば。

View view = findViewById(R.id.text1); // instead of R.id.activity_main 
    view.setDrawingCacheEnabled(true); 

基本的にポイントを使用してみてください すると、画面ショットしたいし、その特定のコンテナの描画可能を取得別の容器でこれらのビューを維持しています。

+0

ありがとうございますIshan、これを試してみました。実際には相対レイアウトには背景があります。 activity_mainの代わりにtext1を取得すると、スクリーンショットにTextが付いていますが、背景は黒です。 –

+0

テキストビューに背景をフルスクリーンで追加し、それをXMLの最初の子として保存すると、インクルードする前に書き込むことを意味します。 – Ishan

関連する問題