zoukankan      html  css  js  c++  java
  • 该View转换成Bitmap方法

    方法一:

    /**
    	* 该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();





    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    根本解决:由于没有远程桌面授权服务器可以提供许可证,远程会话被中断。
    通过SQLServer的数据库邮件来发送邮件
    SQLFullbackup
    vs2015 iis express启动不了及安装DotNetCore.1.0.0-VS2015Tools.Preview2失败的解决方法
    Django1.7+JQuery+Ajax集成小例子
    编译安装带ssl 模块指定版本Python
    细说IIS异常日志 — 你必须知道的功能
    7 MySQL存储过程和函数
    6 MySQL视图
    5 MySQL索引
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4625360.html
Copyright © 2011-2022 走看看