2017-03-23 76 views
0

我正在xperia設備上進行測試,其中底部有一個觸摸導航按鈕。 我的代碼捕獲當前活動的截圖,我不希望包括導航按鈕下方的話,我隱藏它使用活動的屏幕截圖左下角代替導航按鈕

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 

現在,當我按下「抓圖」我可以看到NavButton隱藏和活動發生音量全屏顯示,但保存的屏幕截圖留空。可能是什麼問題?下面是輸出圖像!

enter image description here

以上黑色空間是左space.I不希望它被拋

下面是代碼。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fullscreen); 
    takeScreenshot= (Button) findViewById(R.id.takeScreenshot); 
    Drawable icon= getResources().getDrawable(R.drawable.ic_photo_camera); 
    takeScreenshot.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 
    takeScreenshot.setOnClickListener(this); 
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 46323); 
    settings= (ImageButton) findViewById(R.id.settings); 
    settings.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    int click=v.getId(); 
    if (click==R.id.takeScreenshot){ 
     isStoragePermissionGranted(); 
     // View rootView = getWindow().getDecorView().findViewById(android.R.id.content); 
    //  RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout); 
     View rootView=findViewById(R.id.relativeLayout); 
     takeScreenshot.setVisibility(View.VISIBLE); 
     settings.setVisibility(View.VISIBLE); 
     try{ 
      getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 
     }catch (Exception ex){ 
      Log.d(TAG,"Non Navigation button"); 
     } 
     Bitmap bmp=getScreenShot(rootView); 
     store(bmp); 
     takeScreenshot.setVisibility(View.VISIBLE); 
     settings.setVisibility(View.VISIBLE); 

     Log.d(TAG, Environment.getDataDirectory().toString()); 
    } 
    if (click==R.id.settings){ 

    } 
} 
public static Bitmap getScreenShot(View view) { 
    View screenView = view.getRootView(); 
    screenView.setDrawingCacheEnabled(true); 
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); 
    screenView.setDrawingCacheEnabled(false); 
    return bitmap; 
} 
public void store(Bitmap bm){ 
    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/iScreenShot"; 
    File dir = new File(Environment.getExternalStorageDirectory().toString()+"/iScreenShot"); 
    if(!dir.exists()) 
     dir.mkdirs(); 
    String tempFileName="shot"; 
    String extension=".png"; 
    int num=0; 
    File file=new File(dirPath,tempFileName+num+extension); 
    while (file.exists()){ 
     num++; 
     file=new File(dirPath,tempFileName+num+extension); 
    } 
    try { 
     FileOutputStream fOut = new FileOutputStream(file); 
     // bm.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
     bm.compress(Bitmap.CompressFormat.PNG,100,fOut); 
     fOut.flush(); 
     fOut.close(); 
     Toast.makeText(this, "Saved in gallery", Toast.LENGTH_SHORT).show(); 
     try { 
      MediaPlayer mMediaPlayer = new MediaPlayer(); 
      mMediaPlayer = MediaPlayer.create(this, R.raw.shutter); 
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      mMediaPlayer.setLooping(false); 
      mMediaPlayer.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString()+"/iScreenShot/"+tempFileName+num+extension }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i("ExternalStorage", "Scanned " + path + ":"); 
         Log.i("ExternalStorage", "-> uri=" + uri); 
        } 
       }); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

回答

0

使用此方法來隱藏狀態欄以及導航欄前setContentView

public void setFullScreenView() {//Hiding status/navigation bar 
     if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { 
      View v = this.getWindow().getDecorView(); 
      v.setSystemUiVisibility(View.GONE); 
     } else if (Build.VERSION.SDK_INT >= 19) { 
      View decorView = getWindow().getDecorView(); 
      decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE 
        | View.SYSTEM_UI_FLAG_FULLSCREEN 
        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 
     } 
     setContentView(R.layout.main_activity); 
    } 

然後打電話給你的方法截圖

+0

此代碼只是隱藏像查看導航按鈕。 INVISIBLE,我想要像VIew.GONE這樣的導航按鈕,以便內容下面或它不佔用任何空間。 –

+0

還有一個問題,我發現它只隱藏了第一次單擊任何視圖/觸摸屏後,它仍然可見。 –