2014-01-06 183 views
1

我是Android新手,來自IOS並遇到一個小問題。 我有一個很長的圖像,因此在平板電腦上圖像跨越整個屏幕而不縮放圖像。Android圖像縮放問題

但是,當我打開手機上的應用程序圖像被壓縮高度以適應屏幕。 我如何解決這個問題?

Problem

的圖像被設定爲在的LinearLayout背景:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/blur"/> 

我想:

android:scaleType="fitStart" 
android:scaleType="center" 
android:scaleType="fitcenter 

回答

3

的Android始終隨背景,以適應其查看scaleType只適用於ImageViews,您需要執行一個ImageView,帶有android:src引用您的圖像。只有這樣你才能開始玩android:scaleType

+0

使用imageview並嘗試所有的比例類型。關閉的那個是矩陣標度類型。 – Msmit1993

0

嘗試將XML文件轉換到

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/blur"/> 

如果你的工作就完成了,然後它的好別人按你必須使用imageview @ernir。

0

LinearLayout沒有android:scaleType屬性定義。您應該使用ImageView並將資源設置爲android:src。將其尺寸/位置擴大至LinearLayout。然後你可以使用的ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent">  

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/blur" 
     android:scaleType="centerCrop" 
    /> 
</LinearLayout> 
1

android:scaleType="centerCrop"屬性如果你想以適應或縮放圖像到圖像視圖可以試試這個代碼。

public class ScalingUtilities { 
    /** 
* Utility function for decoding an image resource. The decoded bitmap will 
* be optimized for further scaling to the requested destination dimensions 
* and scaling logic. 
* @param res The resources object containing the image data 
* @param resId The resource id of the image data 
* @param dstWidth Width of destination area 
* @param dstHeight Height of destination area 
* @param scalingLogic Logic to use to avoid image stretching 
* @return Decoded bitmap 
*/ 
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight, 
     ScalingLogic scalingLogic) { 
    Options options = new Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
    options.inJustDecodeBounds = false; 
    options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, 
      dstHeight, scalingLogic); 
    Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options); 

    return unscaledBitmap; 
} 

/** 
* Utility function for creating a scaled version of an existing bitmap 
* 
* @param unscaledBitmap Bitmap to scale 
* @param dstWidth Wanted width of destination bitmap 
* @param dstHeight Wanted height of destination bitmap 
* @param scalingLogic Logic to use to avoid image stretching 
* @return New scaled bitmap object 
*/ 
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, 
     ScalingLogic scalingLogic) { 
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
      dstWidth, dstHeight, scalingLogic); 
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
      dstWidth, dstHeight, scalingLogic); 
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), 
      Config.ARGB_8888); 
    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); 

    return scaledBitmap; 
} 

/** 
* ScalingLogic defines how scaling should be carried out if source and 
* destination image has different aspect ratio. 
* 
* CROP: Scales the image the minimum amount while making sure that at least 
* one of the two dimensions fit inside the requested destination area. 
* Parts of the source image will be cropped to realize this. 
* 
* FIT: Scales the image the minimum amount while making sure both 
* dimensions fit inside the requested destination area. The resulting 
* destination dimensions might be adjusted to a smaller size than 
* requested. 
*/ 
public static enum ScalingLogic { 
    CROP, FIT 
} 

/** 
* Calculate optimal down-sampling factor given the dimensions of a source 
* image, the dimensions of a destination area and a scaling logic. 
* 
* @param srcWidth Width of source image 
* @param srcHeight Height of source image 
* @param dstWidth Width of destination area 
* @param dstHeight Height of destination area 
* @param scalingLogic Logic to use to avoid image stretching 
* @return Optimal down scaling sample size for decoding 
*/ 
public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
     ScalingLogic scalingLogic) { 
    if (scalingLogic == ScalingLogic.FIT) { 
     final float srcAspect = (float)srcWidth/(float)srcHeight; 
     final float dstAspect = (float)dstWidth/(float)dstHeight; 

     if (srcAspect > dstAspect) { 
      return srcWidth/dstWidth; 
     } else { 
      return srcHeight/dstHeight; 
     } 
    } else { 
     final float srcAspect = (float)srcWidth/(float)srcHeight; 
     final float dstAspect = (float)dstWidth/(float)dstHeight; 

     if (srcAspect > dstAspect) { 
      return srcHeight/dstHeight; 
     } else { 
      return srcWidth/dstWidth; 
     } 
    } 
} 

/** 
* Calculates source rectangle for scaling bitmap 
* 
* @param srcWidth Width of source image 
* @param srcHeight Height of source image 
* @param dstWidth Width of destination area 
* @param dstHeight Height of destination area 
* @param scalingLogic Logic to use to avoid image stretching 
* @return Optimal source rectangle 
*/ 
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
     ScalingLogic scalingLogic) { 
    if (scalingLogic == ScalingLogic.CROP) { 
     final float srcAspect = (float)srcWidth/(float)srcHeight; 
     final float dstAspect = (float)dstWidth/(float)dstHeight; 

     if (srcAspect > dstAspect) { 
      final int srcRectWidth = (int)(srcHeight * dstAspect); 
      final int srcRectLeft = (srcWidth - srcRectWidth)/2; 
      return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight); 
     } else { 
      final int srcRectHeight = (int)(srcWidth/dstAspect); 
      final int scrRectTop = (int)(srcHeight - srcRectHeight)/2; 
      return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight); 
     } 
    } else { 
     return new Rect(0, 0, srcWidth, srcHeight); 
    } 
} 

/** 
* Calculates destination rectangle for scaling bitmap 
* 
* @param srcWidth Width of source image 
* @param srcHeight Height of source image 
* @param dstWidth Width of destination area 
* @param dstHeight Height of destination area 
* @param scalingLogic Logic to use to avoid image stretching 
* @return Optimal destination rectangle 
*/ 
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
     ScalingLogic scalingLogic) { 
    if (scalingLogic == ScalingLogic.FIT) { 
     final float srcAspect = (float)srcWidth/(float)srcHeight; 
     final float dstAspect = (float)dstWidth/(float)dstHeight; 

     if (srcAspect > dstAspect) { 
      return new Rect(0, 0, dstWidth, (int)(dstWidth/srcAspect)); 
     } else { 
      return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight); 
     } 
    } else { 
     return new Rect(0, 0, dstWidth, dstHeight); 
    } 
}}