2012-04-06 5 views
2

Ich kann die Bilder von meinem Telefon abrufen und sie in einem Array speichern. Danach zeige ich sie auf dem Bildschirm an. Aber sie sind alle in verschiedenen Formen und Größen. Ich möchte sie alle in der gleichen Größe und Form anzeigen. Irgendeine Idee?Bilder im Programm Android vergrößern

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

Antwort

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

Perfekt gearbeitet !! Ich muss warten, um zu akzeptieren. Danke Mac – user182192

+1

Eigentlich bekomme ich eine Ausnahme, wenn Sie dies verwenden: 'IllegalArgumentException: Bitmap-Größe überschreitet 32 ​​Bit', überprüfen Sie stattdessen meine Antwort. –

+0

Verwenden Sie [this] (http://StackOverflow.com/a/16248911/1289716), damit Sie nicht die Bitmap-Größe überschreiten – MAC

1

Ich verwende:

Bitmap bitmap = //Your source 

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

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

Die letzte booleanfilter ist, was macht das Bild weicher.

Die oben von MAC präsentierte Lösung gab mir eine IllegalArgumentException: bitmap size exceeds 32bits.

Dies skaliert jedoch nur die Bitmap und dreht sie nicht.

0

Ich denke, Sie können dies auch versuchen.

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