Mega Code Archive

 
Categories / Android / 2D Graphics
 

Fit Image No Margin

/*  * Copyright (C) 2008-2010 aki@akjava.com  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ //package com.akjava.lib.android.image; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.Rect; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory.Options; import android.util.Log; /**  *   * @author aki  * @version 1.0  * ?????????????  * ????????????????  *  */  class ImageUtils {   public static final String[] imageExtensions={"jpg","png","jpeg","gif"};      private ImageUtils(){}   /**    * ????????·????????????????????????????????    * ????????????????    *     * @param baseImage ???    * @param width ??    * @param height ??    * @return  ?????????????????    */   public static Bitmap fitImageNoMargin(Bitmap baseImage,int width,int height){          Point pt=calculateFitImage(baseImage,width,height,null);//TODO gc free     Bitmap resizedBitmap = Bitmap.createScaledBitmap(baseImage,         pt.x, pt.y, true);      return resizedBitmap;   }      /**    * ????????????????????????????    *     * @param baseImage    * @param width    * @param height    * @param receiver    * @return    */   public static Point calculateFitImage(Bitmap baseImage,int width,int height,Point receiver){     if(baseImage==null){       throw new RuntimeException("baseImage is null");     }     if(receiver==null){       receiver=new Point();     }     int dw=width;     int dh=height;               if(dw!=0 && dh!=0 ){       double waspect=(double)dw/baseImage.getWidth();       double haspect=(double)dh/baseImage.getHeight();       if(waspect>haspect){//fit h         dw=(int) (baseImage.getWidth()*haspect);                }else{         dh=(int)(baseImage.getHeight()*waspect);       }     }     receiver.x=dw;     receiver.y=dh;     return receiver;   } }