2014-03-25 4 views
1

도움말!안드로이드 이미지가 잘못된 위치에 있습니다

이상한 문제가 있습니다. 나는 사용자가 다음 페이지에서 사용할 이미지를 선택할 수있는 Android 앱을 작성 중입니다. 그게 전부 좋은,하지만 난

imageView.setImageBitmap(bitmap); 

을 수행 할 때 새로운 이미지가 표시하지만, 기본적으로 내가 디자인 타임에 페이지에 배치 된 버튼을 숨기는 페이지에 수직으로 중앙에있다. 레이아웃은 다음과 같습니다 : 나는 (드로어 블/사진 jeepfront @)를 setBitmap, 디폴트 사진을 호출하지 않을 경우 페이지 상단에, 잘 보여주고 내가 버튼을 볼 수 있습니다

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

.

아이디어가 있으십니까? 나는 안드로이드에서 약간의 newby이지만 Java 1.0을 사용하고 있습니다.

덕분에, 데이비드

+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

업로드 방법과 파일을 모르기 때문에 비트 맵을 업로드 할 수 없습니다. 하지만 지프 프론트는 633x480이고 업로드하는 이미지는 1280x800 (Truck Raw.png)입니다. 문제가 크기라고 생각하지 않습니다. 기기에서 트럭 Raw.png를 선택하면 화면 상단에 정렬되지 않습니다. 화면 위에로드하지 않으면 지프 프론트가 표시됩니다. 그것은 혼란스러운 부분입니다. scaleAssetImage를 사용해 주셔서 감사합니다. –

관련 문제