BadCodersHub BadCodersHub - 2 months ago 6
Java Question

Bitmap not scaling to imageView

I do not fully understand where my problem lies, so I will provide as much information as I can, some of it may be irrelevant to finding the answer but some of it may help.

The problem

I have integrated a camera into my application, I am using a surfaceView to show a preview of the camera, when capturing the image I take the raw image bytes and send them through an ObjectOutput stream where I will show the image on that said activity. Here is a snippet of that code:

out = new ObjectOutputStream(new FileOutputStream(new File(getActivity().getCacheDir(),"")+"cacheFile.srl"));
out.writeObject(bytes);


In the other activity I read in the bytes and show the image:

bytes = (byte[]) in.readObject();
image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
myHolder.setImageBitmap(image);


Here is my XML file for the activity which shows the image:

<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"
tools:context=".fragments.Home">

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="@+id/ih"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>




Here is the problem, I need to scale the image to fit the whole activity, yet there is white lines on either side

If any other information is needed please let me know and ill update the post

Update 1

I changed my layout to this:

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:id="@+id/ih"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"/>


yet I still have the white lines either side

Answer

set android:scaleType="centerCrop" as attribute for the ImageView if you want the image to fit its bounds and cut of the rest

You can find more informations about scaletypes here: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Comments