Imageview does not respect the dimensions on a cell phone

0

Good afternoon

The problem I have is that the image does not respect the correct size on a device, this must be adapted for each type of resolution

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<Button
    android:id="@+id/btnTomaFoto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/btn_style"
    android:drawableLeft="@drawable/camera"
    android:text="@string/strTomarFoto" />

<ImageView
    android:id="@+id/imgView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="fitXY"
    >
</ImageView>

<LinearLayout
    android:id="@+id/ly1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >

    <Button
        android:id="@+id/btnMuestraFoto"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/btn_style"
        android:drawableLeft="@drawable/image"
        android:text="@string/strMuestraFoto" />

    <Button
        android:id="@+id/btnEnviar"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/btn_style"
        android:drawableLeft="@drawable/check"
        android:text="@string/strEnviar" />
</LinearLayout>

And when I take a picture, the image enlarges it too much, as in the following image:

The image must be adapted to different equipment resolutions; In the cell phone that I am testing, I have the following results based on the following code lines:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Log.d("debug", "width: " + width); // 480
    Log.d("debug", "height: " + height); // 800

    Log.d("debug", "screen: " + getResources().getConfiguration().screenLayout); // 98
    Log.d("debug", "mask: " + Configuration.SCREENLAYOUT_SIZE_MASK); // 15

    Log.d("debug", "large: " + Configuration.SCREENLAYOUT_SIZE_LARGE); // 3
    Log.d("debug", "normal: " + Configuration.SCREENLAYOUT_SIZE_NORMAL); // 2
    Log.d("debug", "small: " + Configuration.SCREENLAYOUT_SIZE_SMALL); // 1

And the exit:

  

width: 480   height: 800   screen: 98   mask: 15

And finally this is how I show the image in the imageview:

int bitmapHeight = bMap.getHeight();
int bitmapWidth = bMap.getWidth();
Matrix matrix = new Matrix();
matrix.postRotate(rotacion);
Bitmap rotatedBitmap = Bitmap.createBitmap(bMap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);

Bitmap tamanio = Bitmap.createScaledBitmap(rotatedBitmap, bitmapHeight / 6, bitmapWidth / 6, false);

img.setImageBitmap(tamanio);
    
asked by Kenny 15.01.2018 в 18:29
source

1 answer

1

One of the biggest problems when developing apps on android is the great diversity of screen sizes that you have. What you try to do, as I understand it, is re-dimensioning the image in a programmatic way does not seem like the most optimal solution because it looks like too much work and too much code that does not contribute much. The solution I propose is to change the scaleType property of the image view as follows.

<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
>

With this property, what you achieve is that the image that is displayed in the imageView is resized in such a way that it shows as much image as possible without distorting it. This is the best practice and the solution to your problem in my opinion.

    
answered by 16.01.2018 / 14:38
source