/**
* 截屏方法
* @param view
* @param path
* @param fileName
*/
public static boolean takeScreenShot(View view ,String path){
boolean isSucc=false;
/**
* 我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,
* 然后再调用getDrawingCache方法就可以获得view的cache图片了。
* buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,
* 若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。
* 若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,
* 才能建立新的。
*/
view.setDrawingCacheEnabled(true);//开启获取缓存
view.buildDrawingCache();
Bitmap bitmap=view.getDrawingCache();//得到View的cache
Canvas canvas=new Canvas(bitmap);
int w=bitmap.getWidth();
int h=bitmap.getHeight();
Paint paint=new Paint();
paint.setColor(Color.YELLOW);
SimpleDateFormat simple=new SimpleDateFormat("yyyyMMddhhmmss");
String time=simple.format(new Date());
//canvas.drawText(time, w-w/2, h-h/10, paint);
canvas.save();
canvas.restore();
FileOutputStream fos=null;
try{
File sddir=new File(path);
if(!sddir.exists()){
sddir.mkdir();
}
File file=new File(path+time + ".jpg");
fos=new FileOutputStream(file);
if(fos!=null){
bitmap.compress(Bitmap.CompressFormat.JPEG,100, fos);
fos.close();
isSucc=true;
}
}catch(Exception e){
e.printStackTrace();
}
return isSucc;
}
android截屏方法
最新推荐文章于 2023-03-28 10:54:45 发布