2015-08-18 18 views
9

Mam problem z Android Glide. Próbuję szybko zamienić obraz, stają się one po prostu wielkością zastępczą, a mój obraz zastępczy jest bardzo mały. Co muszę zrobić?Android Glide rozmiar zastępczy

Może powinienem sprawdzić, czy jest załadowany, czy coś, ale nie wiem jak.

Glide.with(this) 
    .load(quest.get(id).getImage()) 
    .placeholder(R.drawable.load) 
    .fitCenter() 
    .crossFade() 
    .into(imageView); 

Odpowiedz

0

Jeśli poprawnie rozumiem Twój problem, musisz załadować obrazy, ale muszą one mieć szerokość i wysokość większą niż obraz zastępczy. Aby rozwiązać problem, najpierw należy przeczytać dokumentację dla Glide. Nie widzę twojej definicji ImageView w pliku xml, ale myślę, że używasz parametru wrap_content dla szerokości i wysokości. Jeśli mam rację, to masz wąskie gardło. Przed rozpoczęciem ładowania obrazu, Glide uzyskuje dokładną szerokość i wysokość obrazu. Następnie ładuje obraz z sieci i jednocześnie zmienia jego rozmiar zgodnie z jednym z atrybutów ImageView (szerokość lub wysokość). To dlatego po załadowaniu pojawiają się małe obrazy. Aby rozwiązać problem, po prostu ustaw szerokość lub wysokość ImageView na wartość, którą spodziewasz się zobaczyć. Druga strona obrazu zostanie automatycznie obliczona przez logikę Glide.

12

Według this numerze na stronie Glide GitHub można naprawić go, dodając następujący wiersz do prośbę Glide:

.dontAnimate() 

To by dokonać pełnej linii ładunkowej:

Glide.with(context) 
      .load("http://lorempixel.com/150/150") 
      .placeholder(R.drawable.no_image) 
      .override(100, 100) 
      .dontAnimate() 
      .into(imageView); 
+3

Jak linkujące wspomina również, .don'tAnimate() może również zastąpiony przez .animate (android.R.anim.fade_in) – Chris

+0

Awesome! Zadziałało. – Chandru

1

Co ja w tym celu użyto metody override() w celu wymuszenia rozmiaru obrazu. Jeśli masz GridView i trzeba mieć dwa obrazy w każdym rzędzie, to w jaki sposób można znaleźć rozmiar ekranu w pikselach, aby obliczyć prawidłową wysokość i szerokość obrazu:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    placeholderWidth = size.x/2 ; 
    placeholderHeight = size.x * 3/2 ; // based on the image's height to width ratio 

Po żądanej szerokości i wysokości za każdy obraz jest w ręku, jest łatwy w użyciu ręczne:

Glide.with(context) 
      .load(url) 
      .placeholder(R.drawable.loading) 
      .override(placeholderWidth,placeholderHeight)     
      .into(viewHolder.imageViewHolder); 
1

zrobić go jak wymienione poniżej:

Chodzi o to, aby ustawić typ skalę jako wymagane przez posiadacza miejsce początkowo & dołączać słuchacza aby zmienić typ skali agai n zgodnie z wymaganiami pobranego obrazu po pobraniu obrazu.

//ivBuilderLogo = Target ImageView 
//Set the scale type to as required by your place holder 
//ScaleType.CENTER_INSIDE will maintain aspect ration and fit the placeholder inside the image view 
holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

//AnimationDrawable is required when you are using transition drawables 
//You can directly send resource id to glide if your placeholder is static 
//However if you are using GIFs, it is better to create a transition drawable in xml 
//& use it as shown in this example 
AnimationDrawable animationDrawable; 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    animationDrawable=(AnimationDrawable)context.getDrawable(R.drawable.anim_image_placeholder); 
else 
    animationDrawable=(AnimationDrawable)context.getResources().getDrawable(R.drawable.anim_image_placeholder); 
animationDrawable.start(); 

Glide.with(context).load(logo_url) 
        .placeholder(animationDrawable) 
        .listener(new RequestListener<String, GlideDrawable>() { 
         @Override 
         public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) 
         { 
          return false; 
         } 

         //This is invoked when your image is downloaded and is ready 
         //to be loaded to the image view 
         @Override 
         public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) 
         { 
         //This is used to remove the placeholder image from your ImageView 
         //and load the downloaded image with desired scale-type(FIT_XY in this case) 
         //Changing the scale type from 'CENTER_INSIDE' to 'FIT_XY' 
         //will stretch the placeholder for a (very) short duration, 
         //till the downloaded image is loaded 
         //setImageResource(0) removes the placeholder from the image-view 
         //before setting the scale type to FIT_XY and ensures that the UX 
         //is not spoiled, even for a (very) short duration 
          holder.ivBuilderLogo.setImageResource(0); 
          holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.FIT_XY); 
          return false; 
         } 
        }) 
        .into(holder.ivBuilderLogo); 

Moje przejście odkształcalne (R.drawable.anim_image_placeholder):

(nie wymagane w przypadku korzystania z obrazu statycznego)

<?xml version="1.0" encoding="utf-8"?> 
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/loading_frame1" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame2" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame3" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame4" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame5" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame6" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame7" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame8" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame9" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame10" android:duration="100" />--> 
</animation-list> 
Powiązane problemy