2014-03-25 72 views
1

幫助!安卓圖片位置錯誤

我有一個奇怪的問題。我正在編寫一個Android應用程序,允許用戶選擇要在下一頁使用的圖像。這一切都很好,但是當我做了

imageView.setImageBitmap(bitmap); 

顯示的新形象,但基本上垂直居中在頁面上,躲在我放置在頁面上在設計時按鈕。佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/jeepfront" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="46dp" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/button1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="43dp" 
     android:text="Picture resized/cropped with any buttons" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

如果我不叫setBitmap,那麼默認的圖片(@繪製/ jeepfront)顯示就好了,在頁面的頂部,我可以看到按鈕。

任何想法?我在Android上是一個新手,但從1.0開始使用Java。

謝謝, 大衛

+0

由於您沒有提供任何有關位圖的信息,我只能假設它的寬度和高度(縮放版本)比吉普的更大,導致隱藏按鈕以及屏幕背後的文本視圖,重新放置在圖像下方。 – Onik

回答

1

您可以通過實現下面的代碼實現這一點。

/** 
* Scales image bitmap to fit in correctly in the view. The 
* {@link LayoutParams} for the view in which the bitmap to be rendered is 
* set here 
* 
* @param bitmap 
*   the {@link Bitmap} object to be scaled 
* @param view 
*   the view in which the bitmap will be rendered 
* @param context 
*   the context being used for invoking the method dpToPx(int, 
*   Context) 
* @return a scaled {@link Bitmap} object for the {@link View} obejct 
*   provided 
*/ 
public static Bitmap scaleAssetImage(Bitmap bitmap, View view, 
     Context context) { 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    int bounding = dpToPx(250, context); 

    float xScale = ((float) bounding)/width; 
    float yScale = ((float) bounding)/height; 
    float scale = (xScale <= yScale) ? xScale : yScale; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scale, scale); 
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, 
      matrix, true); 
    width = scaledBitmap.getWidth(); 
    height = scaledBitmap.getHeight(); 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view 
      .getLayoutParams(); 
    params.width = width; 
    params.height = height; 
    view.setLayoutParams(params); 
    return scaledBitmap; 
} 

/** 
* Converts dp to pixel 
* 
* @param dpValue 
*   the integer value to be scaled into pixels 
* @param context 
*   the context being used for accessing resources 
* @return the converted value 
*/ 
private static int dpToPx(int dpValue, Context context) { 
    float density = context.getResources().getDisplayMetrics().density; 
    return Math.round((float) dpValue * density); 
} 

public static Bitmap scaleAssetImage(Bitmap bitmap, View view, Context context)方法會接受你bitmap連同您的ImageView並基於bitmap大小將設置LayoutParams

+0

我不知道如何上傳和文件在這裏,所以我不能上傳位圖。但是,jeepfront是633x480,我上傳的圖片是1280x800(Truck Raw.png)。我不認爲這個問題是大小問題,因爲當我選擇設備上的Truck Raw.png時,它不會與屏幕的頂部對齊,如果我沒有加載它,那麼在那裏會出現吉普車的位置。這是令人困惑的部分。感謝scaleAssetImage,我也需要這樣做。 –