使用Picasso和Glide显示占位符图像,但不显示实际图像-Android

时间:2019-04-22 12:38:32

标签: android google-maps google-maps-api-3 picasso android-glide

我想在Android的Google Map中创建一个自定义标记。自定义标记的图片将来自网址。为了从URL加载图像,我尝试了Picasso和Glide。

问题:从URL加载图片时,它始终显示占位符图片。毕加索和Glide也有同样的问题。

出于测试目的,我使用了两个不同的ImageViews。一种用于自定义标记,另一种仅用于测试。奇怪的是,它适用于测试ImageView,但不适用于自定义标记ImageView。我尝试添加侦听器,但没有随时获得成功方法的回调。

我的代码如下。

MapsActivity.java

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private ArrayList<Marker> markerArrayList = new ArrayList<>();
    private ArrayList<MarkerData> markersDataArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        if (mapFragment != null) {
            mapFragment.getMapAsync(this);
        }

        setGoogleMapData();

        final ImageView iv_sample = findViewById(R.id.iv_sample);
        Picasso.get()
                //.load("https://farm6.staticflickr.com/5294/5460063960_1ef2d5c216_o.jpg")
                .load("http://i.imgur.com/DvpvklR.png")
                .error(R.drawable.ic_error)
                .placeholder(R.drawable.progress_image)
                .into(iv_sample);
    }

    private void setGoogleMapData() {

        MarkerData markerData_1 = new MarkerData();
        markerData_1.setTitle("Current Location");
        markerData_1.setSnippet("JobSquare");
        markerData_1.setLatLng(new LatLng(23.007809, 72.508872)); // Current Location
        markerData_1.setProfilePicUrl("https://imgur.com/t/profile_picture/JfImhgO");
//        markerData_1.setProfilePicUrl("http://i.imgur.com/DvpvklR.png");
        markerData_1.setPersonName("Ishit Jethwa");
        markersDataArrayList.add(markerData_1);

        MarkerData markerData_2 = new MarkerData();
        markerData_2.setTitle("Manek Chowk");
        markerData_2.setSnippet("The eatery");
        markerData_2.setLatLng(new LatLng(23.023323, 72.589652)); // Manek Chowk
        markerData_2.setProfilePicUrl("https://imgur.com/t/profile_picture/jdl5S");
//        markerData_2.setProfilePicUrl("http://i.imgur.com/DvpvklR.png");
        markerData_2.setPersonName("Maulik Dodia");
        markersDataArrayList.add(markerData_2);

        MarkerData markerData_3 = new MarkerData();
        markerData_3.setTitle("Science City");
        markerData_3.setSnippet("Science home");
        markerData_3.setLatLng(new LatLng(23.080292, 72.493599)); // Science City
        markerData_3.setProfilePicUrl("https://imgur.com/t/profile_picture/G1O3ns0");
//        markerData_3.setProfilePicUrl("http://i.imgur.com/DvpvklR.png");
        markerData_3.setPersonName("Piyush Gupta");
        markersDataArrayList.add(markerData_3);

        MarkerData markerData_4 = new MarkerData();
        markerData_4.setTitle("Gujarat High Court");
        markerData_4.setSnippet("Court");
        markerData_4.setLatLng(new LatLng(23.080483, 72.524387)); // Gujarat High Court
        markerData_4.setProfilePicUrl("https://imgur.com/t/profile_picture/rpLiwQv");
//        markerData_4.setProfilePicUrl("http://i.imgur.com/DvpvklR.png");
        markerData_4.setPersonName("MS Dhoni");
        markersDataArrayList.add(markerData_4);

        MarkerData markerData_5 = new MarkerData();
        markerData_5.setTitle("Neelkanth Patang");
        markerData_5.setSnippet("The revolving restaurant");
        markerData_5.setLatLng(new LatLng(23.026840, 72.571986)); // Neelkanth Patang
        markerData_5.setProfilePicUrl("https://imgur.com/t/profile_picture/yu3gjqw");
//        markerData_5.setProfilePicUrl("http://i.imgur.com/DvpvklR.png");
        markerData_5.setPersonName("Rahul Dravid");
        markersDataArrayList.add(markerData_5);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        for (int i = 0; i < markersDataArrayList.size(); i++) {

            Marker marker = createMarker(markersDataArrayList.get(i).getLatLng(),
                    markersDataArrayList.get(i).getTitle(),
                    markersDataArrayList.get(i).getSnippet(),
                    markersDataArrayList.get(i).getProfilePicUrl(),
                    markersDataArrayList.get(i).getPersonName());

            markerArrayList.add(marker);

            builder.include(marker.getPosition());
        }

        LatLngBounds bounds = builder.build();

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        int padding = (int) (width * 0.20); // offset from edges of the map 10% of screen

        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

        mMap.animateCamera(cu);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected Marker createMarker(LatLng latLng, String title, String snippet, String profilePicUrl, String personName) {

        return mMap.addMarker(new MarkerOptions()
                .position(latLng)
                .anchor(0.5f, 0.5f)
                .title(title)
                .snippet(snippet)
                .icon(BitmapDescriptorFactory.fromBitmap(
                        getMarkerBitmapFromView(profilePicUrl, personName))));
    }

    private Bitmap getMarkerBitmapFromView(String profilePicUrl, String personName) {

        View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.layout_map_custom_icon, null);

        TextView nameTextView = customMarkerView.findViewById(R.id.tv_name);
        CircleImageView markerImageView = customMarkerView.findViewById(R.id.profile_image1);
        /*final ImageView markerImageView = customMarkerView.findViewById(R.id.profile_image);*/

        Picasso.get()
                //.load("https://farm6.staticflickr.com/5294/5460063960_1ef2d5c216_o.jpg")
                .load("http://i.imgur.com/DvpvklR.png")
                //.load(R.drawable.my_photo)
                .error(R.drawable.ic_error)
                .resize(100, 100)
                .placeholder(R.drawable.progress_image)
                .into(markerImageView);

        nameTextView.setText(personName);

        customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
        customMarkerView.buildDrawingCache();

        Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(),
                customMarkerView.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(returnedBitmap);
        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);

        Drawable drawable = customMarkerView.getBackground();
        if (drawable != null)
            drawable.draw(canvas);

        customMarkerView.draw(canvas);

        return returnedBitmap;
    }
}

编辑日期:2019年4月24日

正如安迪(Andy)提出有关毕加索callback的一些解决方案一样,我试图以相同的方式实现,但仍然面临着同样的问题。我认为我无法正确放置代码。下面是我更新的代码。

在createMarker方法中,我更新了以下代码。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected Marker createMarker(LatLng latLng, String title, String snippet, String profilePicUrl, String personName) {

    Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .anchor(0.5f, 0.5f)
                    .title(title)
                    .snippet(snippet)
            /*.icon(BitmapDescriptorFactory.fromBitmap(
                    getMarkerBitmapFromView(profilePicUrl, personName)))*/);

    Bitmap bitmap = getMarkerBitmapFromView(profilePicUrl, personName, marker);

    marker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));

    return marker;
}

在getMarkerBitmapFromView方法中,我更新了以下代码。

Picasso.get()
            //.load("https://farm6.staticflickr.com/5294/5460063960_1ef2d5c216_o.jpg")
            .load(profilePicUrl)
            //.load(R.drawable.my_photo)
            .error(R.drawable.ic_error)
            .placeholder(R.drawable.progress_image)
            .into(markerImageView, new MarkerCallback(marker));

我创建了MarkerCallback

    public class MarkerCallback implements Callback {

    private Marker mMarker;

    MarkerCallback(Marker marker) {
        this.mMarker = marker;
    }

    @Override
    public void onSuccess() {
        if (mMarker != null && mMarker.isInfoWindowShown()) {
            mMarker.hideInfoWindow();
            mMarker.showInfoWindow();
        }
    }

    @Override
    public void onError(Exception e) {
        Log.e("mk", "onError: " + e.getMessage());
    }
}

0 个答案:

没有答案