Android实现长截屏功能
本文实例为大家分享了Android实现长截屏功能的具体代码,供大家参考,具体内容如下
1、MainActivity
publicclassMainActivityextendsAppCompatActivity{
ScrollViewscrollView;
StringsdRoot=Environment.getExternalStorageDirectory().getPath();
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView=(ScrollView)findViewById(R.id.scroll);
//权限
intpermission_write=ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
intpermission_read=ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if(permission_write!=PackageManager.PERMISSION_GRANTED
||permission_read!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"正在请求权限",Toast.LENGTH_SHORT).show();
//申请权限,特征码自定义为1,可在回调时进行相关判断
ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
}
@Override
publicvoidonWindowFocusChanged(booleanhasFocus){
shootScrollView(scrollView,"sdRoot");
//takeScreenshot();
super.onWindowFocusChanged(hasFocus);
}
@Override
protectedvoidonResume(){
super.onResume();
}
/**
*截取scrollview的屏幕
***/
publicstaticBitmapgetScrollViewBitmap(ScrollViewscrollView,Stringpicpath){
inth=0;
Bitmapbitmap;
//获取listView实际高度
for(inti=0;i
2.ScreenShot
publicclassScreenShot{
//获取指定Activity的截屏,保存到png文件
publicstaticBitmaptakeScreenShot(Activityactivity){
//View是你需要截图的View
Viewview=activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmapb1=view.getDrawingCache();
//获取状态栏高度
Rectframe=newRect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
intstatusBarHeight=frame.top;
System.out.println(statusBarHeight);
//获取屏幕长和高
intwidth=activity.getWindowManager().getDefaultDisplay().getWidth();
intheight=activity.getWindowManager().getDefaultDisplay()
.getHeight();
//去掉标题栏
//Bitmapb=Bitmap.createBitmap(b1,0,25,320,455);
Bitmapb=Bitmap.createBitmap(b1,0,statusBarHeight,width,height
-statusBarHeight);
view.destroyDrawingCache();
returnb;
}
//保存到sdcard
publicstaticvoidsavePic(Bitmapb,StringstrFileName){
FileOutputStreamfos=null;
Log.d("TAG","savePic()returned:");
try{
fos=newFileOutputStream(strFileName);
if(null!=fos){
b.compress(Bitmap.CompressFormat.PNG,90,fos);
Log.d("TAG","savePic()returned:"+b.getHeight());
fos.flush();
fos.close();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
//程序入口截取当前屏幕
publicstaticvoidshootLoacleView(Activitya,Stringpicpath){
ScreenShot.savePic(ScreenShot.takeScreenShot(a),picpath);
}
/**
*保存bitmap到SD卡
*@parambitName保存的名字
*@parammBitmap图片对像
*return生成压缩图片后的图片路径
*/
@SuppressLint("SdCardPath")
publicstaticStringsaveMyBitmap(BitmapmBitmap,StringbitName){
Stringpath=Environment.getExternalStorageDirectory()+"/"+bitName+".png";
Filef=newFile(path);
try{
f.createNewFile();
}catch(IOExceptione){
System.out.println("在保存图片时出错:"+e.toString());
}
FileOutputStreamfOut=null;
try{
fOut=newFileOutputStream(f);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
try{
mBitmap.compress(Bitmap.CompressFormat.PNG,100,fOut);
}catch(Exceptione){
return"create_bitmap_error";
}
try{
fOut.flush();
}catch(IOExceptione){
e.printStackTrace();
}
try{
fOut.close();
}catch(IOExceptione){
e.printStackTrace();
}
//Filefile=newFile(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
//FileOutputStreamout=null;
//try{
//out=newFileOutputStream(file);
//mBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
//System.out.println("___________保存的__sd___下_______________________");
//}catch(FileNotFoundExceptione){
//e.printStackTrace();
//}
//try{
//out.flush();
//out.close();
//}catch(IOExceptione){
//e.printStackTrace();
//}
returnpath;
//Toast.makeText(HahItemActivity.this,"保存已经至"+Environment.getExternalStorageDirectory()+"下",Toast.LENGTH_SHORT).show();
}
}
3.xml
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。