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);
    
  • 相关阅读:
    委托的另一种写法
    List集合基于某个字段排序
    js进阶
    DBlink与同义词
    iOS汤姆猫素材
    Objective-C 变量和基本的数据类型
    OC基础语法之方法
    16进制数
    kmp算法原理自我理解
    bfs广度遍历搜索模版
  • 原文地址:https://www.cnblogs.com/jiayonghua/p/3300346.html
Copyright © 2011-2022 走看看