android - image在不同的手机上有不同的大小

时间:2016-02-04 16:39:12

标签: android image bitmap google-maps-markers picasso

我有一个可以使用Facebook SDK和谷歌地图API的应用程序。

HomeActivity上,我会获取用户个人资料照片并将其作为标记在Google地图上绘制。

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));
}

我的问题是 - 当我在不同的设备上运行应用程序时(例如 OnePlusOne samsung gt-18190 samsung gt-18190 设备上的图片更大(奇怪的是,更小)。

我检查了picUrl方法中的loadProfilePicture,它们在chrome上的大小相同,但在不同的设备上,大小不同。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

三星拥有OnePlusOne的一半像素密度(~233ppi vs~401ppi),所以如果你在任一款手机上加载的位图分辨率相同,那么它在三星上会显得更大。

您可以做的是从Picasso返回的位图,并使用Bitmap.createScaledBitmap函数和具有一些密度无关维度的资源文件创建缩放版本。例如:

在资源dimen.xml文件中。 (使用适用于您的应用程序的任何dp值)

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

在您的家庭活动中。

    @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));
    }