zoukankan      html  css  js  c++  java
  • 图片的等比缩放

    	    public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight) {  
    
    			   Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight);  
    
    			  Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight);  
    
    			   Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);  
    
    			   Canvas canvas = new Canvas(scaledBitmap);  
    
    			   canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));return scaledBitmap;  
    
    			   } 
    
    
    		
    		
    		
    		private static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
    			    final float srcAspect = (float)((float)srcWidth / (float)srcHeight);
    			    final float dstAspect = (float)((float)dstWidth / (float)dstHeight);
    			    if (srcAspect > dstAspect) {
    			      final int srcRectWidth = (int)(srcHeight * dstAspect);
    			      final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
    			      return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
    			    } else {
    			      final int srcRectHeight = (int)(srcWidth / dstAspect);
    			      final int scrRectTop = (int)(srcHeight - srcRectHeight) / 2;
    			      return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
    			    }
    			}
    			private static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
    			    final float srcAspect = (float)srcWidth / (float)srcHeight;
    			    final float dstAspect = (float)dstWidth / (float)dstHeight;
    			    if (srcAspect > dstAspect) {
    			      return new Rect(0, 0, dstWidth, (int)(dstWidth / srcAspect));
    			    } else {
    			      return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight);
    			    }
    			}
    
    			
    			private static Bitmap decodeFile(String pathName, int dstWidth, int dstHeight) {
    				 Bitmap unscaledBitmap = null;
    				if(dstWidth > 0){
    					Options options = new Options();
    					options.inJustDecodeBounds = true;
    					BitmapFactory.decodeFile(pathName, options);
    					options.inJustDecodeBounds = false;
    					options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight);
    					unscaledBitmap = BitmapFactory.decodeFile(pathName, options);
    				}else{
    					unscaledBitmap = BitmapFactory.decodeFile(pathName);
    				}
    				  return unscaledBitmap;
    				}
    				public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
    				    final float srcAspect = (float)srcWidth / (float)srcHeight;
    				    final float dstAspect = (float)dstWidth / (float)dstHeight;
    				    if (srcAspect > dstAspect) {
    				      return srcWidth / dstWidth;
    				    } else {
    				      return srcHeight / dstHeight;
    				    }
    				}
    

    调用如下:

    Bitmap baseBitmap = decodeFile(picName,width,height);
    Bitmap bitmap = createScaledBitmap(bitmap,width,height);
    
  • 相关阅读:
    Mybatis 自动从数据库生成entity,mapping,dao接口
    ActiveMQ主从配置
    ajax跨域请求,页面和java服务端的写法
    mysql查看连接数和状态,设置连接数和超时时间
    tomcat错误:@HandlesTypes annotation of one or more ServletContentInitializers
    自定义对话框 提示:Unable to add window token null is not for an application
    使用Provider时提示:Unable to get provider...
    使用JFinal-weixin配置微信开发
    redis提示Could not get a resource from the pool(jedis连接池配置)
    mysql配置文件生效顺序
  • 原文地址:https://www.cnblogs.com/jiayonghua/p/3300346.html
Copyright © 2011-2022 走看看