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);
    
  • 相关阅读:
    windows下Docker的安装
    Javascript基础系列之(六)循环语句(for循环)
    Javascript基础系列之(六)循环语句(do while循环)
    Javascript基础系列之(六)循环语句(while语句)
    Javascript基础系列之(五)条件语句(switch语句)
    Javascript基础系列之(五)条件语句(if条件语句)
    Javascript基础系列之(五)条件语句(逻辑操作符)
    Javascript基础系列之(五)条件语句(比较操作符)
    Javascript基础系列之(五)关键字和保留字 (keyword)
    Javascript基础系列之(四)数据类型 (数组 array)
  • 原文地址:https://www.cnblogs.com/jiayonghua/p/3300346.html
Copyright © 2011-2022 走看看