2015-06-18 17 views
7

Szukam adoptować Glide library zamiast Universal Image Loader, ale mam problem z przejściami elementów współużytkowanych.Biblioteka systemu Android Glide nie działa z przejściami elementów współużytkowanych

W moim prostym Sandbox Utworzyłem następujące przejścia przy użyciu UIL: https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-113333.mp4

Całkiem proste, i to działa dobrze. Ale kiedy użyłem Glide, że nie wygląda tak miły: https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-114508.mp4

Oto kod używam

pierwszej czynności:

public class MainActivity extends BaseActivity { 

    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.main); 

     final ImageView iv = (ImageView) findViewById(R.id.main_image); 

     displayImageGlide(BaseActivity.IMAGE_URL, iv, true); 

     Button button = (Button) findViewById(R.id.main_button); 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), DifferentActivity.class); 
       startActivity(intent, iv); 
      } 

     }); 
    } 

} 

a drugi:

public class DifferentActivity extends BaseActivity { 

    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 

     setContentView(R.layout.different); 

     ImageView imageView = (ImageView) findViewById(R.id.diff_image); 

     displayImageGlide(BaseActivity.IMAGE_URL, imageView, false); 
    } 

} 

BaseActivity zawiera po prostu displayImageGlide

public void displayImageGlide(String url, ImageView imageView) { 
    Glide.with(this).load(url) 
      .asBitmap().transform(new CustomTransformation(this)) 
      .skipMemoryCache(true) 
      .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
      .into(imageView); 
} 

private static class CustomTransformation extends BitmapTransformation { 

    public CustomTransformation(Context context) { 
     super(context); 
    } 

    @Override 
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
     return bitmapChanger(toTransform, outWidth, outHeight); 
    } 

    @Override 
    public String getId() { 
     return "some_id_1"; 
    } 

} 

private static Bitmap bitmapChanger(Bitmap bitmap, int desiredWidth, int desiredHeight) { 
    float originalWidth = bitmap.getWidth(); 
    float originalHeight = bitmap.getHeight(); 

    float scaleX = desiredWidth/originalWidth; 
    float scaleY = desiredHeight/originalHeight; 

    //Use the larger of the two scales to maintain aspect ratio 
    float scale = Math.max(scaleX, scaleY); 

    Matrix matrix = new Matrix(); 

    matrix.setScale(scale, scale); 

    //If the scaleY is greater, we need to center the image 
    if(scaleX < scaleY) { 
     float tx = (scale * originalWidth - desiredWidth)/2f; 
     matrix.postTranslate(-tx, 0f); 
    } 

    Bitmap result = Bitmap.createBitmap(desiredWidth, desiredHeight, bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(result); 
    canvas.drawBitmap(bitmap, matrix, new Paint()); 
    return result; 
} 

Próbowałem usunąć linie pamięci podręcznej z budowniczego Glide (używane, aby zapobiec buforowaniu tego, co próbuję poprawić), ale to nie pomogło. Chciałbym przyjąć Glide, ale jest to rodzaj łamacza transakcji, jeśli to nie zadziała. Jeśli ktokolwiek ma jakiś wkład, to byłbym wdzięczny, dzięki!

+0

nie wykorzystali Glide ale myślę, transformacja przełącza obrazy w locie co powoduje zacinać podczas animacji. Czy ustawienie "android: scaleType =" centerCrop "' na ImageView nie rozwiąże problemu? –

+0

centerCrop nie jest typem skali, który chcę, jednak metoda CustomTransformation stosuje niestandardową macierz do mapy bitowej, która tworzy obraz, który ładnie pasuje do granic. – JMRboosties

Odpowiedz

6

Próbowałem swój kod i mam ten sam rezultat, następnie próbowałem kolejną realizację następującego tym: https://github.com/codepath/android_guides/wiki/Shared-Element-Activity-Transition

i dokonała pewnych zmian, które myślę, że są kluczem tutaj:

centerCrop powinny być ustawione na ImageView, jeśli ustawimy to za pomocą Glide, spowoduje to ten sam błąd. activity_main.xml

<ImageView 
     android:id="@+id/ImageView" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:layout_centerInParent="true" 
     android:transitionName="detail_image" 
     android:scaleType="centerCrop" 
    /> 

BaseActivity.java Na główną działalność dontTransform powinny być prawdziwe i na DifferentActivity fałszywe.

public void displayImageGlide(String url, ImageView imageView, Boolean dontTransform) { 
    if (dontTransform) { 
     Glide.with(this).load(url) 
       .skipMemoryCache(true) 
       .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
       .dontTransform() 
       .into(imageView); 
     return; 
    } 

     Glide.with(this).load(url) 
       .skipMemoryCache(true) 
       .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
       .into(imageView); 
    } 

Edit, zobaczyć jak to wygląda:https://www.dropbox.com/s/30s5l8awxogltls/device-2015-06-23-153153.mp4?dl=0

+1

Dam ci ten strzał, dziękuję za odpowiedź! – JMRboosties

+0

Problem z tą odpowiedzią polega na tym, że nie używa ona matrycy o najwyższej koncentracji, którą chciałem użyć dla swoich obrazów. – JMRboosties

+0

Mam również ten sam problem, czy możesz podać pełną implementację? Dziękuję Ci! – user1436497

Powiązane problemy