lundi 29 juin 2015

Android picasso resize image to screen width


I am using picasso to resize my background image to fill the screen width (not adjust the image height).

The problem is, that even though I set the image width to the same as the screen width, the image does not fill to the horizontal edges, there is a gap. At 800dp the gap is 35dp.

here is my code

ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.morn_blur_bg);
WindowManager wm = (WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Point size = new Point();
display.getSize(size);
int displayWidth = size.x;
int height = size.y;
int width = 0;

//Log.d("CP", "Screen width="+displayWidth);
//Log.d("CP", "Image width=" + bMap.getWidth());

if (bMap.getWidth() < displayWidth) {
    width = displayWidth;
} else {
    width = bMap.getWidth();
}

Log.d("CP", "New width=" + width);

Picasso.with(v.getContext())
        .load(R.drawable.morn_blur_bg)
        .resize(width, height)
        .into(imgBackground);

here is my xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    android:id="@+id/layRoot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

     <!--Background image-->
    <ImageView
        android:id="@+id/imgBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

If I hardcode the width to 835dp the image fills the display, but of course this will not work across devices. :-)

cheers


Aucun commentaire:

Enregistrer un commentaire