2013-10-16 3 views
0

내 안드로이드 응용 프로그램에는 ImageViews 및 textViews로 채워지는 목록이 있습니다. imageViews는 sdCard에서 동적으로로드됩니다. 내 문제는 내가 더 높은 해상도의 이미지를 가지고 있으며, 목록의 목적을 위해 엄지 손톱과 같이 크게 작게 만들어야한다는 것이다. 그렇다면 어떻게 이미지를 이미지 뷰로 스케일 할 수 있습니까?imageViews의 이미지 크기 조정

답변

2

나는 다음이 코드는 여기

public static Bitmap resizeImage(Bitmap orignal, int new_width, 
     int new_height) { 
    // load the origial BitMap 
    int width = orignal.getWidth(); 
    int height = orignal.getHeight(); 

    float scaleWidth = ((float) new_width)/width; 
    float scaleHeight = ((float) new_height)/height; 
    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 
    Bitmap resizedBitmap = Bitmap.createBitmap(orignal, 0, 0, width, 
      height, matrix, true); 
    return resizedBitmap; 
} 
+0

thx 메이트! 그것은 내가 찾고 있었던 바로 그 것이다. – Libathos

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

     int scale=1; 
     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=250; // here provide your max size required 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     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; 
     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     return bmp; 
    } catch (FileNotFoundException e) {} 
    return null; 
} 
0

이 코드는 당신의 이미지 뷰에 따라 이미지의 크기를 조절하는 데 도움이

private void scaleImage() 
{ 
    // Get the ImageView and its bitmap 
    ImageView view = (ImageView) findViewById(R.id.image_box); 
    Drawable drawing = view.getDrawable(); 
    if (drawing == null) { 
     return; // Checking for null & return, as suggested in comments 
    } 
    Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

    // Get current dimensions AND the desired bounding box 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    int bounding = dpToPx(250); 
    Log.i("Test", "original width = " + Integer.toString(width)); 
    Log.i("Test", "original height = " + Integer.toString(height)); 
    Log.i("Test", "bounding = " + Integer.toString(bounding)); 

    // Determine how much to scale: the dimension requiring less scaling is 
    // closer to the its side. This way the image always stays inside your 
    // bounding box AND either x/y axis touches it. 
    float xScale = ((float) bounding)/width; 
    float yScale = ((float) bounding)/height; 
    float scale = (xScale <= yScale) ? xScale : yScale; 
    Log.i("Test", "xScale = " + Float.toString(xScale)); 
    Log.i("Test", "yScale = " + Float.toString(yScale)); 
    Log.i("Test", "scale = " + Float.toString(scale)); 

    // Create a matrix for the scaling and add the scaling data 
    Matrix matrix = new Matrix(); 
    matrix.postScale(scale, scale); 

    // Create a new bitmap and convert it to a format understood by the ImageView 
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
    width = scaledBitmap.getWidth(); // re-use 
    height = scaledBitmap.getHeight(); // re-use 
    BitmapDrawable result = new BitmapDrawable(scaledBitmap); 
    Log.i("Test", "scaled width = " + Integer.toString(width)); 
    Log.i("Test", "scaled height = " + Integer.toString(height)); 

    // Apply the scaled bitmap 
    view.setImageDrawable(result); 

    // Now change ImageView's dimensions to match the scaled image 
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
    params.width = width; 
    params.height = height; 
    view.setLayoutParams(params); 

    Log.i("Test", "done"); 
} 

private int dpToPx(int dp) 
{ 
    float density = getApplicationContext().getResources().getDisplayMetrics().density; 
    return Math.round((float)dp * density); 
} 

ImageView에 대한 xml 코드는

입니다.
<ImageView a:id="@+id/image_box" 
    a:background="#ff0000" 
    a:src="@drawable/star" 
    a:layout_width="wrap_content" 
    a:layout_height="wrap_content" 
    a:layout_marginTop="20dp" 
    a:layout_gravity="center_horizontal"/> 

씨 : http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

0

또한 이미지보기에서 setMax를 사용할 수 있습니다

imageView.setMaxHeight(myMaxHeight); 
imageView.setMaxWidth(myMaxWidth); 
imageView.setAdjustViewBounds(true); 
imageView.setScaleType(ScaleType.FIT_CENTER); 

당신은 또한 XML은 그

이 방법을 사용하는 문제를 속성을 사용할 수 있습니다 최대 크기를 설정 했으므로 작은 이미지가 이미지보기를 채우지 않습니다.