我有一个大尺寸的图像.在运行时,我想从存储中读取图像并对其进行缩放,以便减轻其重量和大小,并将其用作缩略图.当用户点击缩略图时,我想显示完整尺寸的图像.
Dax*_*Dax 130
试试这个
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
Run Code Online (Sandbox Code Playgroud)
此实用程序可从API_LEVEl 8获得.[来源]
kak*_*ppa 46
我的解决方案
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}
Run Code Online (Sandbox Code Playgroud)
Mas*_*imo 14
我找到的最佳解决方案如下.与其他解决方案相比,这个解决方案不需要加载完整的图像来创建缩略图,因此效率更高! 它的限制是你不能有一个具有确切宽度和高度的缩略图,但解决方案尽可能接近.
File file = ...; // the image file
Options bitmapOptions = new Options();
bitmapOptions.inJustDecodeBounds = true; // obtain the size of the image, without loading it in memory
BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
// find the best scaling factor for the desired dimensions
int desiredWidth = 400;
int desiredHeight = 300;
float widthScale = (float)bitmapOptions.outWidth/desiredWidth;
float heightScale = (float)bitmapOptions.outHeight/desiredHeight;
float scale = Math.min(widthScale, heightScale);
int sampleSize = 1;
while (sampleSize < scale) {
sampleSize *= 2;
}
bitmapOptions.inSampleSize = sampleSize; // this value must be a power of 2,
// this is why you can not have an image scaled as you would like
bitmapOptions.inJustDecodeBounds = false; // now we want to load the image
// Let's load just the part of the image necessary for creating the thumbnail, not the whole image
Bitmap thumbnail = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
// Save the thumbnail
File thumbnailFile = ...;
FileOutputStream fos = new FileOutputStream(thumbnailFile);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
// Use the thumbail on an ImageView or recycle it!
thumbnail.recycle();
Run Code Online (Sandbox Code Playgroud)
这是一个将Bitmap缩小为缩略图大小的更完整的解决方案.它通过保持图像的纵横比并将它们填充到相同的宽度来扩展Bitmap.createScaledBitmap解决方案,以便它们在ListView中看起来很好.
此外,最好进行一次缩放,并将生成的Bitmap作为blob存储在Sqlite数据库中.我已经包含了一个关于如何将Bitmap转换为字节数组的代码片段.
public static final int THUMBNAIL_HEIGHT = 48;
public static final int THUMBNAIL_WIDTH = 66;
imageBitmap = BitmapFactory.decodeByteArray(mImageData, 0, mImageData.length);
Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);
int padding = (THUMBNAIL_WIDTH - imageBitmap.getWidth())/2;
imageView.setPadding(padding, 0, padding, 0);
imageView.setImageBitmap(imageBitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();
Run Code Online (Sandbox Code Playgroud)
使用BitmapFactory.decodeFile(...)
让您的Bitmap
对象,并将它设置为ImageView
同ImageView.setImageBitmap()
.
在ImageView
布局尺寸设置为小的,例如:
android:layout_width="66dip" android:layout_height="48dip"
Run Code Online (Sandbox Code Playgroud)
一个添加onClickListener
到ImageView
并推出新的活动,在那里你在全尺寸显示图像
android:layout_width="wrap_content" android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
或指定一些更大的尺寸.