Ofek Agmon
Ofek Agmon

Reputation: 5198

android - image has different sizes on different phones

I have an app which work with the Facebook SDK and google map API.

On the HomeActivity, I get the user profile picture and draw it on the google map as a marker.

public void loadProfilePicture(String picUrl, final Location location) {
    Picasso.with(getActivity()).load(picUrl).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            drawCanvas(location, bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    });
}

private void drawCanvas(Location location, Bitmap bitmap) {
    photoMarker = getMap().addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .anchor(0.5f, 1));
}

My problem is - when I run the app on different devices (OnePlusOne and samsung gt-18190, for example), it draws the image bigger on the samsung gt-18190 device (which is oddly, smaller).

I checked the picUrl in the loadProfilePicture method, and they both look the same size on chrome, but on different devices, different size.

what am I doing wrong?

Upvotes: 0

Views: 469

Answers (1)

Joopkins
Joopkins

Reputation: 1644

The Samsung has half of the pixel density of the OnePlusOne (~233ppi vs ~401ppi) so if the bitmap you are loading on either phone is the same resolution it will appear larger on the Samsung.

What you can do is take the bitmap returned from Picasso and create a scaled version with the Bitmap.createScaledBitmap function and a resource file with some density independent dimensions. For example:

In your resource dimen.xml file. (Use whatever dp values work for your application)

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="profile_width">200dp</dimen>
        <dimen name="profile_height">200dp</dimen>
    </resources>

Inside your home activity.

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        int width = (int) getResources().getDimension(R.dimen.profile_width);
        int height = (int) getResources().getDimension(R.dimen.profile_height);
        drawCanvas(location, Bitmap.createScaledBitmap(bitmap, width, height, false));
    }

Upvotes: 2

Related Questions