2010-12-10 59 views
2

je l'extrait suivant de code dans la méthode onCreate d'une classe qui étend l'activité:problème lorsque l'image mise à l'échelle en utilisant la matrice dans ImageView

ImageView view = (ImageView) findViewById(R.id.ImageView01); 

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
bmpFactoryOptions.inJustDecodeBounds = true; 
Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
R.drawable.sample_landscape, bmpFactoryOptions); 

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 

Log.v("IMAGEVIEWERMATRIX", "Display Width: " + display.getWidth()); 
Log.v("IMAGEVIEWERMATRIX", "Display Height: " + display.getHeight()); 

Log.v("IMAGEVIEWERMATRIX", "BMP Width: " + bmpFactoryOptions.outWidth); 
Log.v("IMAGEVIEWERMATRIX", "BMP Height: " + bmpFactoryOptions.outHeight); 

if (bmpFactoryOptions.outWidth > width || bmpFactoryOptions.outHeight > height) { 
    float heightRatio = (float) height/(float) bmpFactoryOptions.outHeight; 
    float widthRatio = (float) width/(float) bmpFactoryOptions.outWidth; 

    // WHY 
    heightRatio = heightRatio/1.5f; 
    widthRatio = widthRatio/1.5f; 

    Log.v("IMAGEVIEWERMATRIX", "heightRatio:" + heightRatio); 
    Log.v("IMAGEVIEWERMATRIX", "widthRatio: " + widthRatio); 

    float scale = widthRatio; 
    if (heightRatio < widthRatio) { 
     scale = heightRatio; 
    } 

    matrix.setScale(scale, scale); 
    Log.v("IMAGEVIEWERMATRIX", "Scale: " + scale); 
} else { 
    Log.v("IMAGEVIEWERMATRIX", "NOTNOTNOT"); 
    matrix.setTranslate(1f, 1f); 
} 

Bitmap realBmp = BitmapFactory.decodeResource(getResources(), 
R.drawable.sample_landscape); 
view.setImageBitmap(realBmp); 
view.setImageMatrix(matrix); 

Quand je lance, l'image apparaît à l'écran mis à l'échelle pour tenir correctement . Mon problème est que je dois ajuster l'échelle de 1,5 pour que ce soit correct.

heightRatio = heightRatio/1.5f; 
widthRatio = widthRatio/1.5f; 

Mon intuition est que cela a à voir avec la densité d'affichage du dispositif, mais pour la vie de moi, je ne peux pas envelopper ma tête pourquoi je devrais le faire.

Voici le XML de mise en page:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView android:id="@+id/ImageView01" android:scaleType="matrix"  android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 
</LinearLayout> 

Répondre

4

Ok, répondre à ma propre question ici. Le problème que je voyais a à voir avec l'endroit où j'ai placé l'élément d'image. Je l'avais dans le dossier res/drawable-mdpi seulement et l'appareil sur lequel je testais était de haute densité. Par conséquent, Android faisait évoluer l'image automatiquement pour moi. Pour rectifier la situation et traiter les pixels directement, j'ai placé l'image dans le dossier res/drawable-nodpi.

Ce comportement est décrit sur cette page: http://developer.android.com/guide/practices/screens_support.html sous "Comment Android prend en charge plusieurs écrans".