【Android 图片缩放】少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述Android 图片缩放相关的知识,希望能为你提供帮助。
通过计算图片缩放值进行图片缩放
/** * 根据路径获得突破并压缩返回bitmap用于显示 * * @param filePath * @return */ public static Bitmap getSmallBitmap(String filePath, int width, int height) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, width, height); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); }
计算图片的缩放值
/** * 计算图片的缩放值 * * @param options * @param reqWidth * @param reqHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) {// Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } HcLog.D("HcUtil #calculateInSampleSize = " + inSampleSize); return inSampleSize; }
推荐阅读
- android-ListView空间的使用
- Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)
- Adb connect监听指定的主机和端口/Adb监听Visual Studio Emulator for Android模拟器
- Android应用性能优化之使用SparseArray替代HashMap
- Android 设置图片 Bitmap任意透明度
- Android 手机卫士--选中SettingItemView条目状态切换
- Android开发实践android.view.InflateException: Binary XML file line #12: Error inflating class fragment
- Android的数据的存储方式
- sevice__属性介绍: android:exported