2010-12-10 6 views
2
에 매트릭스를 사용하여 이미지를 확장 할 때

내가 가지고있는 활동 확장하는 클래스의에서 onCreate 방법의 코드 조각 다음 : 나는 그것을 실행하면 이미지가 화면에 나타납니다문제 이미지 뷰

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

제대로 맞게 조정 . 내 문제는 올바른 것으로하기 위해 1.5로 척도를 조정해야한다는 것입니다.

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

내 직감이 장치의 표시 밀도를 함께 할 수 있다는 것입니다하지만 내 인생, 내가이 작업을 수행해야하는 이유 주위에 내 머리를 정리하고 수 없습니다. 여기

는 레이아웃 XML입니다 :

<?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> 

답변

4

좋아, 여기에 내 자신의 질문에 대답. 내가 보았던 문제는 이미지 자산을 어디에 두 었는지와 관련이 있습니다. 나는 res/drawable-mdpi 폴더에서만 그것을 가지고 있었고 내가 테스트하고 있던 장치는 고밀도였다. 따라서 안드로이드는 자동으로 이미지를 확대합니다. 상황을 바로 잡고 픽셀을 직접 처리하기 위해 이미지를 res/drawable-nodpi 폴더에 저장했습니다.

이 동작은 "Android가 여러 화면을 지원하는 방법"에서 http://developer.android.com/guide/practices/screens_support.html의이 페이지에 설명되어 있습니다.