哈哈 在项目中看了些别人的图片缩放类,放出代码 以后用

/* * Copyright (C) 2010 Aspire *  * CommonUtil.java Version 1.0 * */package com.mid.market.util;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Random;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.drawable.Drawable;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.shapes.PathShape;import android.graphics.drawable.shapes.Shape;/** * @author x_liyajun * * 2010-10-24 ����10:40:46 *   * ImageUtil * */public class ImageUtil {    private static final float PHOTO_BORDER_WIDTH = 3.0f;    private static final int PHOTO_BORDER_COLOR = 0xffffffff;    private static final float ROTATION_ANGLE_MIN = 2.5f;    private static final float ROTATION_ANGLE_EXTRA = 5.5f;    public static final String  SDCARD_DIR="/sdcard/mid/";    private static final Random sRandom = new Random();    private static final Paint sPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);    private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);    static {        sStrokePaint.setStrokeWidth(PHOTO_BORDER_WIDTH);        sStrokePaint.setStyle(Paint.Style.STROKE);        sStrokePaint.setColor(PHOTO_BORDER_COLOR);    }    /**     * 图片缩放     * @param bitmap     * @param width     * @param height     * @return     */   public  static Bitmap scaleAndFrame(Bitmap bitmap, int width, int height) {        final int bitmapWidth = bitmap.getWidth();        final int bitmapHeight = bitmap.getHeight();        final float scale = Math.min((float) width / (float) bitmapWidth,                 (float) height / (float) bitmapHeight);        final int scaledWidth = (int) (bitmapWidth * scale);        final int scaledHeight = (int) (bitmapHeight * scale);        final Bitmap decored = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);        final Canvas canvas = new Canvas(decored);        final int offset = (int) (PHOTO_BORDER_WIDTH / 2);        sStrokePaint.setAntiAlias(false);        canvas.drawRect(offset, offset, scaledWidth - offset - 1,                scaledHeight - offset - 1, sStrokePaint);        sStrokePaint.setAntiAlias(true);        return decored;    }    /**     * 图片旋转     * @param bitmap     * @return     */   public static Bitmap rotateAndFrame(Bitmap bitmap) {        final boolean positive = sRandom.nextFloat() >= 0.5f;        final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * ROTATION_ANGLE_EXTRA) *                (positive ? 1.0f : -1.0f);        final double radAngle = Math.toRadians(angle);        final int bitmapWidth = bitmap.getWidth();        final int bitmapHeight = bitmap.getHeight();        final double cosAngle = Math.abs(Math.cos(radAngle));        final double sinAngle = Math.abs(Math.sin(radAngle));        final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);        final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);        final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);        final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);        final float x = (width - bitmapWidth) / 2.0f;        final float y = (height - bitmapHeight) / 2.0f;        final Bitmap decored = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);        final Canvas canvas = new Canvas(decored);        canvas.rotate(angle, width / 2.0f, height / 2.0f);        canvas.drawBitmap(bitmap, x, y, sPaint);        canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);        return decored;    }   /**    * 从图片文件中读取图片,并返回缩略图    * @param fileName    * @param width 缩放图片的宽度    * @param height  高度    * @return    */   public static Bitmap getSamlBitmapFromFill(String fileName,int width, int height){   if(fileName.split("/").length<=1){   fileName =SDCARD_DIR+fileName;   }   Bitmap srcBitmap = BitmapFactory.decodeFile(fileName);   if(srcBitmap==null){   return null;   }   Bitmap destBitmap = scaleAndFrame(srcBitmap,width,height);   return destBitmap;   }   public static Bitmap genStar(int count){      return null;      }         public static Drawable getDrawable(String name) {       Drawable d = null;       FileInputStream fis = null;       String fileName = FileUtils.newFileName(name, "png");       if (!FileUtils.isExists(fileName)) {           return null;       }       try {           fis = new FileInputStream(fileName);           d = Drawable.createFromStream(fis, name);       } catch (FileNotFoundException e) {           // TODO Auto-generated catch block           e.printStackTrace();       } finally {           if (fis != null) {               try {                   fis.close();               } catch (IOException e) {                   // TODO Auto-generated catch block                   e.printStackTrace();               }               fis = null;           }       }       return d;   }//   public static Shape createPentacle(double sx,double sy,double radius,double theta){//       final float arc = (float) (Math.PI/5);//       final float rad = (float) (Math.sin(Math.PI/10)/Math.sin(3*Math.PI/10));//       Path path = new Path();//       path.moveTo(1,0);//       for(int idx = 0;idx < 5;idx ++){//           path.lineTo((float)(rad*Math.cos((1+2*idx)*arc)),(float)(rad*Math.sin((1+2*idx)*arc)));//           //           path.lineTo((float)(Math.cos(2*(idx+1)*arc)),(float)(Math.sin(2*(idx+1)*arc)));//       }//       path.close();//       ShapeDrawable mDrawable = new ShapeDrawable(new PathShape(path, 250, 250));//       mDrawable.getPaint().setColor(Color.YELLOW);//       return atf.createTransformedShape(path);//   } }

更多相关文章

  1. Android(安卓)matrix 控制图片的旋转、缩放、移动
  2. android拍照与读取相册
  3. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  4. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  5. [Android]在App中使用相机
  6. android WebView 图片缩放功能小结
  7. Android(安卓)主流图片库Picasso Glide Fresco对比分析
  8. android背景选择器selector用法汇总
  9. ImageView的scaletype属性

随机推荐

  1. 【从源码看Android】03Android(安卓)Mess
  2. android root权限相关bin程序rageagainst
  3. Android TextView文字链接4中方法
  4. Android 导入Flutter模块
  5. android ARM 汇编学习 —— hello world
  6. android去掉系统默认标题栏
  7. Android 性能优化系列视频(五)
  8. [2011.02.22] Android SDK离线安装
  9. Android(安卓)SDK,ADT,API 版本的对应关
  10. 【游戏语音技术开发互联网公司】android