码迷,mamicode.com
首页 > 其他好文 > 详细

把View转化成Bitmap的方法

时间:2014-07-21 23:29:43      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:des   blog   java   color   width   2014   

方法一:

/**
	* 把View绘制到Bitmap上
	* @param view 需要绘制的View
	* @param width 该View的宽度
	* @param height 该View的高度
	* @return 返回Bitmap对象
	* add by csj 13-11-6
	*/
	public Bitmap getViewBitmap(View comBitmap, int width, int height) {
		Bitmap bitmap = null;
		if (comBitmap != null) {
			comBitmap.clearFocus();
			comBitmap.setPressed(false);

			boolean willNotCache = comBitmap.willNotCacheDrawing();
			comBitmap.setWillNotCacheDrawing(false);

			// Reset the drawing cache background color to fully transparent
			// for the duration of this operation
			int color = comBitmap.getDrawingCacheBackgroundColor();
			comBitmap.setDrawingCacheBackgroundColor(0);
			float alpha = comBitmap.getAlpha();
			comBitmap.setAlpha(1.0f);

			if (color != 0) {
				comBitmap.destroyDrawingCache();
			}
			
			int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
			int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
			comBitmap.measure(widthSpec, heightSpec);
			comBitmap.layout(0, 0, width, height);

			comBitmap.buildDrawingCache();
			Bitmap cacheBitmap = comBitmap.getDrawingCache();
			if (cacheBitmap == null) {
				Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + comBitmap + ")", 
						new RuntimeException());
				return null;
			}
			bitmap = Bitmap.createBitmap(cacheBitmap);
			// Restore the view
			comBitmap.setAlpha(alpha);
			comBitmap.destroyDrawingCache();
			comBitmap.setWillNotCacheDrawing(willNotCache);
			comBitmap.setDrawingCacheBackgroundColor(color);
		}
		return bitmap;
	}
	

方法二:

private Bitmap getViewBitmap(View v) {  
        v.clearFocus();  
        v.setPressed(false);  
  
        boolean willNotCache = v.willNotCacheDrawing();  
        v.setWillNotCacheDrawing(false);  
  
        // Reset the drawing cache background color to fully transparent  
        // for the duration of this operation  
        int color = v.getDrawingCacheBackgroundColor();  
        v.setDrawingCacheBackgroundColor(0);  
  
        if (color != 0) {  
            v.destroyDrawingCache();  
        }  
        v.buildDrawingCache();  
        Bitmap cacheBitmap = v.getDrawingCache();  
        if (cacheBitmap == null) {  
            Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());  
            return null;  
        }  
  
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  
  
        // Restore the view  
        v.destroyDrawingCache();  
        v.setWillNotCacheDrawing(willNotCache);  
        v.setDrawingCacheBackgroundColor(color);  
  
        return bitmap;  
    }

方法三:

View view = mLauncher.getDragLayer();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();





把View转化成Bitmap的方法,布布扣,bubuko.com

把View转化成Bitmap的方法

标签:des   blog   java   color   width   2014   

原文地址:http://blog.csdn.net/chenshijun0101/article/details/38022789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!