我可以从手机中检索图片并将其存储在一个阵列中。之后我在屏幕上显示它们。但它们都有不同的形状和大小。我想以相同的尺寸和形状显示它们。有什么想法吗?
photoPaths = new ArrayList<String>();
getAllPhotos(Environment.getExternalStorageDirectory(), photoPaths);
images = new Bitmap[photoPaths.size()];
apa = (AnimationPhotoView)findViewById(R.id.animation_view);
for(int i=0;i<photoPaths.size();i++)
{
File imgFile = new File(photoPaths.get(0));
if(imgFile.exists())
{
images[0] = decodeFile(imgFile);}
答案 0 :(得分:7)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
// this will create image with new size
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
iv.setScaleType(ScaleType.CENTER);
iv.setImageBitmap(resizedBitmap);
答案 1 :(得分:1)
我正在使用:
Bitmap bitmap = //Your source
int newWidth = //compute new width
int newHeight = //compute new height
bitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true);
最后boolean
为filter
,可让您的图片更流畅。
上面MAC提出的解决方案给了我一个IllegalArgumentException: bitmap size exceeds 32bits
。
这只会缩放位图,但不会旋转它。
答案 2 :(得分:0)
如果您只想显示由android“知道”的jpg图像或图像格式,您可以简单地使用MediaStore.Images并检索缩略图,这样更快且需要更少的内存,图像应该已经全部相同的格式(宽度+高度)。
http://developer.android.com/reference/android/provider/MediaStore.Images.html
答案 3 :(得分:0)
我想你也可以尝试一下。
private Bitmap decodeFile(File f)
{
try
{
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=200;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true)
{
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}