Android 如何像whatsapp中那样对图像进行子采样/调整大小
通过whatsapp发送图像时,您可以在imageview中看到正在发送的图像,并且缩放得非常好 例如,我给我的朋友发了两张照片Android 如何像whatsapp中那样对图像进行子采样/调整大小,android,android-imageview,android-image,android-bitmap,Android,Android Imageview,Android Image,Android Bitmap,通过whatsapp发送图像时,您可以在imageview中看到正在发送的图像,并且缩放得非常好 例如,我给我的朋友发了两张照片 第一幅图像的大小:1296像素X2304像素 第二幅图像的大小:1920像素X1080像素 此图像太大,因此whatsapp必须先对其进行缩放,然后才能在imageview中将其显示给我 按whatapp333像素缩放后的第一幅图像的大小X339像素 按whatsapp缩放后的第二幅图像大小333像素X187像素 正如你所看到的,宽度是一样的,只是高度不同。我
- 第一幅图像的大小:1296像素X2304像素
- 第二幅图像的大小:1920像素X1080像素
- 按whatapp333像素缩放后的第一幅图像的大小X339像素
- 按whatsapp缩放后的第二幅图像大小333像素X187像素
private void resizeImage(){
Uri selectedImage = imageReturnedIntent.getData();
d("image url is " + selectedImage);
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
BitmapFactory.decodeStream(imageStream,null, options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(imageStream,null, options);
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth , int reqHeght){
int height = options.outHeight;
int width = options . outWidth;
d("width is "+ width + " height is "+ height);
d("required width is "+ reqWidth + " required height "+ reqHeght);
int inSampleSize = 1;
if(height > reqHeght || width > reqWidth){
int heightRatio = Math.round((float)height/(float)reqHeght);
d("height ratio is "+ heightRatio);
int widthRatio = Math.round((float)width / (float) reqWidth);
d("width ratio is "+widthRatio);
inSampleSize = (heightRatio > widthRatio)? heightRatio :widthRatio;
}
d(" insample size is "+ inSampleSize);
return inSampleSize;
}
使用上述方法输出第一张图像:宽度较小(<333),高度很大(>339,是579!!)
第二种方法
private Bitmap scaleToFitWidth(Bitmap b, int width){
float factor = width/b.getWidth(); // width is 333
return Bitmap.createScaledBitmap(b, width,(int)(b.getHeight() * factor),true)
}
第二种方法中第一幅图像的输出:图像高度非常大
问题是否有人知道如何在所有设备上像whatapp一样缩放图像
编辑:whatsapp图像
我的华威尔手机
三星平板电脑
您可以签出此代码,它的工作原理与您想要的相同。此代码已根据我的使用情况进行了修改。 使用此代码将为您提供:
- 小于或等于100kb的低尺寸图像,无需播放图像质量
- 高像素图像将缩小到maxWidth和maxHeight,而不会失去其原始质量李>
ImageView
类
如何像whatsapp中那样对图像进行子采样/调整大小
- 您只需使用以下命令即可完成此操作。替换
值500500,具有所需的图像质量
}Glide.with(getApplicationContext()) .load(uri) .asBitmap() .into(new SimpleTarget<Bitmap>(500, 500) { @Override public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { bitmap = resource;//this is the required bitmap imageView.setImageBitmap(resource); } });
- 要将Glide包括在项目中,请将以下内容添加到gradle中 文件
sent
文件夹中查看每个图像的大小和尺寸。当我在huawel手机中发送图像时,图像的大小调整得非常好。我要发送给的人正在使用三星平板电脑。他的手机中图像(或imageview)的大小与我手机中图像(或imageview)的大小不同。我已经编辑了我的问题。请参见照片如果使用相同的maxWidth和maxHeight,它们如何计算出将在imageview中显示给用户的大小?。
private File savebitmap(String filename) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(filename + ".png");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".png");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
// make a new bitmap from your file
Bitmap bitmap = BitmapFactory.decodeFile(file.getName());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}