首页 > 代码库 > Android--将布局保存成图像

Android--将布局保存成图像

		View view = findViewById(R.id.photoselectbg_iv_rl11); //获取布局控件属性
		final Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_8888);
		view.draw(new Canvas(bmp));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = dateFormat.format(new Date());
		
		File dir = new File(GloableParams.IMG_FILE_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        
		final String photoUrl = GloableParams.IMG_FILE_PATH + time + ".png";//换成自己的图片保存路径
		final File file = new File(photoUrl);
		
		new Thread() {
			@Override
			public void run() {
				try {
					boolean bitMapOk = bmp.compress(CompressFormat.PNG, 100, new FileOutputStream(file));
					if(bitMapOk) {
						myApp.photoFile = file;
						myApp.photoUrl = photoUrl;					
				} catch (FileNotFoundException e) {				
					e.printStackTrace();
				}
			}			
		}.start();


核心就是view.draw();

Android--将布局保存成图像