收集整理的getDrawingCache的方法

时间:2022-07-21 17:58:15


参考:

View的getDrawingCache为空,解决办法

http://blog.csdn.net/huangbiao86/article/details/9053429

Android中View转换为Bitmap及getDrawingCache=null的解决方法

http://www.cnblogs.com/devinzhang/archive/2012/06/05/2536848.html



	public static Bitmap convertViewToBitmap(View view){
			view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
	        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
	        view.buildDrawingCache();
	        Bitmap bitmap = view.getDrawingCache();

	        return bitmap;
	}
	
	public static Bitmap loadBitmapFromView(View v ) {
		if (v == null) {
			return null;
		}
		Bitmap screenshot;
		screenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
		Canvas c = new Canvas(screenshot);
		c.translate(-v.getScrollX(), -v.getScrollY());
		v.draw(c);
		return screenshot;
	}