【问题标题】:Picasso loads landscape image but doesn't load portrait into marker's info window毕加索加载横向图像但不将纵向加载到标记的信息窗口中
【发布时间】:2018-11-22 17:27:08
【问题描述】:

我有一个地图活动,我想为我的标记自定义信息窗口。 在那个窗口中,我想从一个文件中加载一张图片,并添加一些额外的文本(我稍后会添加)。

问题是,当我有一个横向模式的图片文件时,图片会加载并以正确的尺寸显示在标记的信息窗口中。 但是,如果文件处于纵向模式,则信息窗口会以正确的尺寸出现(实际上是纵向),但没有显示图片。 我检查了肖像图像文件是否存在,它具有正确的文件路径,但我无法找出肖像图片不显示的原因。

这是我的代码:

        // Initializing the custom info window as per info_window.xml
        if (mMap != null) {
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {

                    View view = getLayoutInflater().inflate(R.layout.info_window, null);
                    mImageView = view.findViewById(R.id.markerImageView);

                    for (Picture picture : mPictureList) {
                        if (picture.getMarker().equals(marker)) {
                            String path = picture.getPicturePath();
                            int width = 160;
                            int height = 90;
                            ExifInterface data = null;
                            try {
                                data = new ExifInterface(path);
                                String orientation = data.getAttribute(ExifInterface.TAG_ORIENTATION);
                                // 1 for landscape, 3 for landscape upside down
                                // 6 for portrait, 8 is portrait upside down
                                if (orientation.equalsIgnoreCase("6") || orientation.equalsIgnoreCase("8")) {
                                    width = 90;
                                    height = 160;
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            // setting the new dimensions in dp
                            mImageView.getLayoutParams().height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics());
                            mImageView.getLayoutParams().width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, getResources().getDisplayMetrics());
                            mImageView.requestLayout();

                            Picasso.with(MapsActivity.this)
                                    .load("file://" + path)
                                    .resize(width, height)
                                    .centerCrop()
                                    .into(mImageView);
                        } else {
                            Log.d(TAG, "getInfoContents: Didn't find any picture with that marker");
                        }
                    }
                    return view;
                }
            });
        }

还有 info_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<ImageView
    android:id="@+id/markerImageView"
    android:layout_width="160dp"
    android:layout_height="160dp"/>

</LinearLayout>

【问题讨论】:

  • @Jonas Lochmann,删除 centerCrop() 没有帮助。在纵向模式下仍然没有显示图像(信息窗口的尺寸设置为纵向)。顺便说一句,我认为 centerCrop() 很重要,以防照片不是以 16:9 比例拍摄(这是我为信息窗口设置的比例)

标签: android google-maps picasso marker


【解决方案1】:

我终于找到了解决这个问题的方法。 我使用的是最新版本的 picasso 2.3.2,它在显示图像时存在问题(根据 https://github.com/square/picasso/issues/530) 我切换到2.2.0版本,问题解决了。

附: 2.4.0 版应该可以解决上述问题。

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 2017-01-25
    • 1970-01-01
    • 2014-08-29
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多