根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段

 /* *      

 * 根据缩略的宽高比例和设定的最大宽高设置图片的宽高 长图会截取中间一段

  */
public  static  synchronized  Bitmap readBitmap(String key,  int  width,
                 int  height) {
         File bitmapFile =  null ;
         Bitmap bitmap =  null ;
         boolean  toLongOrWidth =  false ;
         boolean  widthbigger =  false ;
         try  {
                 bitmapFile = getFile(key);
         catch  (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
         }
         if  (bitmapFile !=  null ) {
 
                 BitmapFactory.Options options =  new  Options();
                 options.inJustDecodeBounds =  true ;
 
                 try  {
                         BitmapFactory.decodeStream( new  FileInputStream(bitmapFile),
                                         null , options);
                         int  outheight = options.outHeight;
                         int  outwidth = options.outWidth;
                         float  rate;
                         if  (outheight > outwidth) {
                                 rate = ( float ) outheight / ( float ) outwidth;
                                 if  (rate >  2.0 ) {
                                         toLongOrWidth =  true ;
                                 }
                                 height = ( int ) (width * rate);
                                 widthbigger =  false ;
                         else  {
                                 rate = ( float ) outwidth / ( float ) outheight;
                                 if  (rate >  2.0 ) {
                                         toLongOrWidth =  true ;
                                 }
                                 width = ( int ) (height * rate);
                                 widthbigger =  true ;
                         }
 
                         int  size = calculateInSampleSize(options, width, height);
                         options.inSampleSize = size;
                         options.inJustDecodeBounds =  false ;
                         bitmap = BitmapFactory.decodeStream( new  FileInputStream(
                                         bitmapFile),  null , options);
                         Bitmap dst = Bitmap.createScaledBitmap(bitmap, width, height,
                                         false );
                         if  (toLongOrWidth) {
                                 if  (widthbigger) {
                                         int  oldwidth = width;
                                         width = ( int ) (( float ) height * ( float ) 1.77 );
                                         dst = Bitmap.createBitmap(dst, (oldwidth - width) /  2 ,
                                                         0 , width, height);
 
                                 else  {
                                         int  oldheight = height;
                                         height = ( int ) (( float ) width * ( float ) 1.77 );
                                         dst = Bitmap.createBitmap(dst,  0 ,
                                                         (oldheight - height) /  2 , width, height);
                                 }
                                 Bitmap newb = Bitmap.createBitmap(dst.getWidth(),
                                                 dst.getHeight(), Config.ARGB_8888);
                                 Canvas canvasTmp =  new  Canvas(newb);
                                 canvasTmp.drawColor(Color.TRANSPARENT);
                                 Paint p =  new  Paint();
                                 Typeface font = Typeface.create( "宋体" , Typeface.BOLD);
                                 p.setAntiAlias( true );  // 设置画笔为无锯齿
                                 p.setColor(Color.WHITE);
                                 p.setTypeface(font);
                                 p.setTextSize(ImageUtils.dp2px(HoHoApplication.mAppContext,
                                                 ( float 12 ));
                                 canvasTmp.drawBitmap(dst,  0 0 , p);
                                 Paint p2 =  new  Paint();
                                 p2.setColor(Color.parseColor( "#77000000" ));
                                 Rect r =  new  Rect( 0 , dst.getHeight()
                                                 - ImageUtils.dp2px(HoHoApplication.mAppContext,
                                                                 ( float 24 ), dst.getWidth(),
                                                 dst.getHeight());
                                 canvasTmp.drawRect(r, p2);
                                 canvasTmp.drawText(
                                                 "长图" ,
                                                 dst.getWidth()
                                                                 2
                                                                 - ImageUtils.dp2px(
                                                                                 HoHoApplication.mAppContext,
                                                                                 ( float 12 ),
                                                 dst.getHeight()
                                                                 - ImageUtils.dp2px(
                                                                                 HoHoApplication.mAppContext,
                                                                                 ( float 6 ), p);
                                 canvasTmp.save(Canvas.ALL_SAVE_FLAG);
                                 canvasTmp.restore();
                                 dst = newb;
                         }
                         if  (bitmap != dst) {  // 如果没有缩放,那么不回收
                                 bitmap.recycle();  // 释放Bitmap的native像素数组
                         }
                         return  dst;
 
                 catch  (FileNotFoundException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                 }
 
         }
         return  bitmap;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值