2017-03-14 4 views
0

Mache ich hier etwas falsch? Ich möchte dieses Grau als Platzhalter laden, es wird jedoch nicht geladen. Es scheint nur dann zu passieren, wenn bereits ein anderes Bild gesetzt oder geladen wurde.ColorDrawable-Platzhalter wird nicht in Glide geladen

Glide.with(getContext()) 
    .load(url) 
    .placeholder(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray))) 
    .error(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray))) 
    .dontAnimate() 
    .into(new SimpleTarget<GlideDrawable>() { 
     @Override 
     public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { 
     image.setImageDrawable(resource); 
     } 

     @Override 
     public void onLoadFailed(Exception e, Drawable errorDrawable) { 
     resetImageView(); 
     } 
    }); 

Antwort

2

Versuchen Sie, den folgenden Code:

Glide Version: kompilieren 'com.github.bumptech.glide: glide: 3.6.1'

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="100dp" 
     android:layout_height="100dp"></ImageView> 
</RelativeLayout> 



ImageView imageView = (ImageView) findViewById(R.id.imageView); 
     Glide.with(this) 
       .load("http://matplotlib.org/_images/color_demo.png") 
       .placeholder(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray))) 
       .error(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray))) 
       .dontAnimate() 
       .into(imageView); 

<color name="placeholder_gray">#A9A9A9</color> 

Versuchen Sie eine gültige URL und eine ungültige URL.Für eine gültige URL wird vor dem Laden des Bildes eine graue Farbe angezeigt und bei einem ungültigen URL wird ein grauer Platzhalter angezeigt.