0
我正在xperia设备上进行测试,其中底部有一个触摸导航按钮。 我的代码捕获当前活动的截图,我不希望包括导航按钮下方的话,我隐藏它使用活动的屏幕截图左下角代替导航按钮
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
现在,当我按下“抓图”我可以看到NavButton隐藏和活动发生音量全屏显示,但保存的屏幕截图留空。可能是什么问题?下面是输出图像!
以上黑色空间是左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();
}
}
此代码只是隐藏像查看导航按钮。 INVISIBLE,我想要像VIew.GONE这样的导航按钮,以便内容下面或它不占用任何空间。 –
还有一个问题,我发现它只隐藏了第一次单击任何视图/触摸屏后,它仍然可见。 –