12

I have a recycle view to show all photo thumbnail items. When click on item, I use transition for imageview in this item to Detail activity. The problem is that image source is gotten from internet by UIL. And sometime (not always) the images not reload correct size like this:

transition error

 // on view holder item click
  final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, false,
                    new Pair<>(((ItemViewHolder) viewHolder).thumbnail, getString(R.string.TransitionName_Profile_Image)),
                    new Pair<>(((ItemViewHolder) viewHolder).tvName, getString(R.string.TransitionName_Profile_Name)));

  ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pairs);
  startActivityForResult(intent, requestCode, transitionActivityOptions.toBundle());

Detail activity

// try to post pone transition until UIL load finish
ActivityCompat.postponeEnterTransition(this);
getSupportFragmentManager().beginTransaction().replace(R.id.layoutContent, new DetailFragment()).commit();

Fragment Detail

ImageLoader.getInstance().displayImage(url, imageViewDetail, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    finishAnimation();
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                    finishAnimation();
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    finishAnimation();
                }
            });


 private void finishAnimation(){
     ActivityCompat.startPostponedEnterTransition(getActivity());
     imageViewDetail.invalidate();
 }

fragment_detail.xml

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

     <ImageView
        android:transitionName="@string/TransitionName.Profile.Image"
        android:id="@+id/imageViewDetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>
</FrameLayout>

I even wait views are laid out before load image but still not work:

imageViewDetail.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                // code load image from UIL
                return false;
            }
        });

Is there any way to avoid this issue?

ductran
  • 10,043
  • 19
  • 82
  • 165
  • You are use centerCrop maybe that's a issue try with different scale type . – h_patel Sep 05 '16 at 09:28
  • @Himani well, I have to use because the photo must fit screen and keep ratio. But do you have any clue about the scaletype error? It **does work on many cases**, but sometime look like the image doesn't ready – ductran Sep 05 '16 at 09:33
  • then use FITXY to scaletype – h_patel Sep 05 '16 at 09:34
  • @Himani First, fitXY doesn't keep ratio aspect. Second, still not work. – ductran Sep 05 '16 at 09:42
  • @R4j, do you know the aspect ratio of the source images in advance? I had a similar problem with Images. But in my case I knew the aspect ratio of my images in advance, i.e 2:3 . So I had an Image view in which I was changing the imageview dimensions dynamically at runtime based on device screensize. If this is your requirement, I could post the code for doing so. – Adithya Upadhya Sep 13 '16 at 15:46
  • @oathkeeper well, my problem isn't image ratio. It's the transition issue. If I turn off transition, it displays correctly. Anyway, thanks for your feedback – ductran Sep 14 '16 at 02:43
  • did you find the solution? – kingston Apr 27 '17 at 22:14
  • @kingston no, I quit and remove the transition – ductran Apr 28 '17 at 04:57
  • facing same issue, but no answer at your question made me sad. – Usman Rana Oct 29 '18 at 08:10

1 Answers1

0

this xml can handle diferent image size

 <ScrollView
        android:id="@+id/vScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="51dp"
        android:scrollbars="none" >

        <HorizontalScrollView
            android:id="@+id/hScroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none" 
            android:layout_gravity="center">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/imgFullscreen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY" />
            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>

in java

fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
selectedPhoto = (FeedItem) i.getSerializableExtra(TAG_SEL_IMAGE);
        zoom = 1;

        if (selectedPhoto != null) {
            fetchFullResolutionImage();
        } else {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.msg_unknown_error), Toast.LENGTH_SHORT)
                    .show();
        }



private void fetchFullResolutionImage() {
    try {
        final URL url = new URL(selectedPhoto.getImge());       
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Bitmap bmp = BitmapFactory.decodeStream(url
                            .openStream());
                    if (bmp != null)
                        setImage(bmp);
                    else {
                        showToast("Error fetching image!");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
private void setImage(final Bitmap bmp) {
        runOnUiThread(new Runnable() {
            public void run() {
                fullImageView.setImageBitmap(bmp);
                adjustImageAspect(selectedPhoto.getWidth(),
                        selectedPhoto.getHeight());
            }
        });
    }
private void adjustImageAspect(int bWidth, int bHeight) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        if (bWidth == 0 || bHeight == 0)
            return;
        int swidth;

        if (android.os.Build.VERSION.SDK_INT >= 13) {
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            swidth = size.x;
        } else {
            Display display = getWindowManager().getDefaultDisplay();
            swidth = display.getWidth();
        }

        int new_height = 0;
        new_height = swidth * bHeight / bWidth;
        params.width = swidth;
        params.height = new_height;
        saveW = swidth;
        saveH = new_height;
        fullImageView.setLayoutParams(params);
    }
Muklas
  • 557
  • 7
  • 11
  • My problem is transition issue, not image scaling. Your code simply scale image to fit screen height. – ductran Sep 14 '16 at 02:47