2012-04-06 7 views
2

私の携帯電話から画像を取得し、配列に格納することができます。その後、私はそれらを画面に表示しています。しかし、彼らはすべて異なった形と大きさである。私はそれらをすべて同じサイズと形で表示したい。何か案が?Androidでプログラムで画像を拡大する

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);} 

答えて

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); 
+0

完全に機能しました!私は受け入れるために待つ必要があります。ありがとうMac – user182192

+1

実際に私はこれを使用すると例外が発生します: 'IllegalArgumentException:ビットマップのサイズが32ビットを超えています.'では、私の答えを確認してください。 –

+0

あなたは 'Bitmap size exceeds'を得られないので、[this](http://stackoverflow.com/a/16248911/1289716) – MAC

0

あなただけのアンドロイドによる「既知」であり、JPG、画像や画像フォーマットを表示したい場合は、単にMediaStore.Imagesを使用してサムネイルを取得することができ、これは高速で、少ないメモリを必要とします画像はすでにすべて同じフォーマット(幅+高さ)でなければなりません。

http://developer.android.com/reference/android/provider/MediaStore.Images.html

1

私が使用しています:

Bitmap bitmap = //Your source 

int newWidth = //compute new width 
int newHeight = //compute new height 

bitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true); 

最後booleanfilterで、あなたのイメージがスムーズになりますました。

上記のMACによって示された解決策は、私にIllegalArgumentException: bitmap size exceeds 32bitsを与えました。

これはビットマップのサイズを変更するだけで、ビットマップは回転させません。

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; 
}