图片处理

package com.example.testactivitylifecycle;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class ZoomInOrOut {

 public enum ScalingLogic {
  CROP, FIT
 }

 public static Bitmap createScaledBitmap(Bitmap unscaledBitmap,
   int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
  Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(),
    unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
  Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(),
    unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
  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, ScalingLogic scalingLogic) {
  if (scalingLogic == ScalingLogic.CROP) {
   final float srcAspect = (float) srcWidth / (float) srcHeight;
   final float dstAspect = (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);
   }
  } else {
   return new Rect(0, 0, srcWidth, srcHeight);
  }
 }

 private static Rect calculateDstRect(int srcWidth, int srcHeight,
   int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
  if (scalingLogic == ScalingLogic.FIT) {
   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);
   }
  } else {
   return new Rect(0, 0, dstWidth, dstHeight);
  }
 }

 public static Bitmap decodeFile(String pathName, int dstWidth,
   int dstHeight, ScalingLogic scalingLogic) {
  Options options = new Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(pathName, options);
  options.inJustDecodeBounds = false;
  options.inSampleSize = calculateSampleSize(options.outWidth,
    options.outHeight, dstWidth, dstHeight, scalingLogic);
  Bitmap unscaledBitmap = BitmapFactory.decodeFile(pathName, options);
  return unscaledBitmap;
 }

 public static Bitmap decodeResource(Resources res, int id, int dstWidth,
   int dstHeight, ScalingLogic scalingLogic) {

  Options options = new Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeResource(res, id, options);
  options.inJustDecodeBounds = false;
  options.inSampleSize = calculateSampleSize(options.outWidth,
    options.outHeight, dstWidth, dstHeight, scalingLogic);
  Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, id, options);
  return unscaledBitmap;

 }

 private static int calculateSampleSize(int srcWidth, int srcHeight,
   int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
  if (scalingLogic == ScalingLogic.FIT) {
   final float srcAspect = (float) srcWidth / (float) srcHeight;
   final float dstAspect = (float) dstWidth / (float) dstHeight;
   if (srcAspect > dstAspect) {
    return srcWidth / dstWidth;
   } else {
    return srcHeight / dstHeight;
   }
  } else {
   final float srcAspect = (float) srcWidth / (float) srcHeight;
   final float dstAspect = (float) dstWidth / (float) dstHeight;
   if (srcAspect > dstAspect) {
    return srcHeight / dstHeight;
   } else {
    return srcWidth / dstWidth;
   }
  }
 }

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值