Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Bug: Invalidate View Problem in Galaxy S3 #5

Open
cagkanciloglu opened this issue Nov 5, 2012 · 43 comments
Open

Bug: Invalidate View Problem in Galaxy S3 #5

cagkanciloglu opened this issue Nov 5, 2012 · 43 comments
Assignees
Labels

Comments

@cagkanciloglu
Copy link

Hello. First thank you for library, its really great. While I'm developing an application I came across a bug in Galaxy S3 phones. While flipping the view invalidates itself and causes black screen. This bug can be seen at the demo also. I uploaded a video on you ube about this bug. You can check it. Thank you in advance. http://www.youtube.com/watch?v=rv0pLLCn-8w

@AravindRaj56060
Copy link

Hi. Library is superb. But i am also facing the same view invalidating issue as @cagkanciloglu . I tested in galaxy note and galaxy note 2. Could anyone please help how to solve this. Thank you in advance..

@ghost ghost assigned openaphid Nov 6, 2012
@openaphid
Copy link
Owner

I'll try to get a Samsung device to reproduce the issue.

Could you tell me the OS version on your devices?

Thanks

@AravindRaj56060
Copy link

Hi guys, I guess the problem is while taking screenshot in grabIt.java file in higher resolution mobiles previous screenshot is released to free memory. So i think we need to reduce the size of bitmap created or compress bitmap for high res mobiles alone. Please correct me if i am wrong. Thank you.

@AravindRaj56060
Copy link

Note is android 4.0.4 and Note II is android 4.1.1. But I guess this problem may occur for most of the high resolution devices.

Thanks

@cagkanciloglu
Copy link
Author

Aravind's theory may be correct. I also tested it on Galaxy S2 its working alright. Btw OS version on my S3 is 4.1.1

@cagkanciloglu
Copy link
Author

Flip Complex Layouts demo working alright on S3. Fyi

@openaphid
Copy link
Owner

I tested on emulator running OS 4.1 with high resolution settings (720 x 1280), but I failed to reproduce it.

It doesn't sound like a memory issue as far as I can tell from the video. But I could be wrong on this.

I'll ask my friends for a Galaxy Note for further testing.

By the way, could you try to pause and resume the activity to see what happens then, by pressing the Home button?

@cagkanciloglu
Copy link
Author

Nothing happened after resuming the activity. Issue remained.

@AravindRaj56060
Copy link

Nothing changes while pausing and resuming activity. I will attach my log data printed for a single flip while running in galaxy Note

11-06 14:45:53.329: D/OpenAphid(30218): flippedToView: 4
11-06 14:45:53.329: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@41c99f18, android.widget.RelativeLayout@419ee2a0, android.widget.RelativeLayout@41c64270], index: 1
11-06 14:45:53.369: D/dalvikvm(30218): GC_FOR_ALLOC freed 8011K, 34% free 38951K/58311K, paused 21ms
11-06 14:45:53.374: I/dalvikvm-heap(30218): Grow heap (frag case) to 41.049MB for 2457616-byte allocation
11-06 14:45:53.399: D/dalvikvm(30218): GC_FOR_ALLOC freed <1K, 30% free 41350K/58311K, paused 14ms
11-06 14:45:53.439: D/dalvikvm(30218): GC_CONCURRENT freed 0K, 30% free 41351K/58311K, paused 1ms+2ms
11-06 14:45:53.459: I/OpenAphid(30218): Update visible views, index -1, buffered: 3
11-06 14:45:53.459: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@41948060, android.widget.RelativeLayout@41c99f18, android.widget.RelativeLayout@419ee2a0], index: 1
11-06 14:45:53.469: I/OpenAphid(30218): Update visible views, index -1, buffered: 3
11-06 14:45:53.469: D/OpenAphid(30218): flippedToView: 4
11-06 14:45:53.469: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@41948060, android.widget.RelativeLayout@41c99f18, android.widget.RelativeLayout@419ee2a0], index: 1
11-06 14:45:53.489: I/OpenAphid(30218): Update visible views, index -1, buffered: 3
11-06 14:45:53.489: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@4199a880, android.widget.RelativeLayout@421aeed8, android.widget.RelativeLayout@419ee2a0], index: 1
11-06 14:45:53.489: D/OpenAphid(30218): flippedToView: 4
11-06 14:45:53.489: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@4199a880, android.widget.RelativeLayout@421aeed8, android.widget.RelativeLayout@419ee2a0], index: 1
11-06 14:45:53.504: I/OpenAphid(30218): Update visible views, index -1, buffered: 3
11-06 14:45:53.504: D/OpenAphid(30218): bufferedViews: [android.widget.RelativeLayout@4215ede8, android.widget.RelativeLayout@419ad6c8, android.widget.RelativeLayout@419ee2a0], index: 1
11-06 14:45:53.509: D/OpenAphid(30218): onLayout: 0, 0, 800, 1280; child 3
11-06 14:45:53.529: D/dalvikvm(30218): GC_FOR_ALLOC freed 634K, 30% free 40873K/58311K, paused 17ms
11-06 14:45:53.529: I/dalvikvm-heap(30218): Grow heap (frag case) to 44.490MB for 4096016-byte allocation
11-06 14:45:53.564: D/dalvikvm(30218): GC_CONCURRENT freed 2489K, 28% free 42384K/58311K, paused 1ms+3ms
11-06 14:45:53.609: D/OpenAphid(30218): create bitmap 800x1280
11-06 14:45:53.609: D/OpenAphid(30218): reloading texture: android.widget.RelativeLayout@419ad6c8 and android.widget.RelativeLayout@419ee2a0; old views: android.widget.RelativeLayout@419ad6c8, android.widget.RelativeLayout@419ee2a0, front changed true, back changed false
11-06 14:45:53.609: D/OpenAphid(30218): Destroy texture: 2
11-06 14:45:53.609: D/OpenAphid(30218): createTexture: 800, 1280; POT: 1024, 2048
11-06 14:45:54.119: I/OpenAphid(30218): Update visible views, index 1, buffered: 3 

But for galaxy S2 my logacat data are

11-06 15:02:52.193: I/OpenAphid(17658): Update visible views, index -1, buffered: 2
11-06 15:02:53.213: D/OpenAphid(17658): flippedToView: 1
11-06 15:02:53.213: D/OpenAphid(17658): bufferedViews: [android.widget.RelativeLayout@2fdac120, android.widget.RelativeLayout@2fd78728], index: 0
11-06 15:02:53.733: I/OpenAphid(17658): Update visible views, index -1, buffered: 3
11-06 15:02:53.733: D/OpenAphid(17658): bufferedViews: [android.widget.RelativeLayout@2fdac120, android.widget.RelativeLayout@2fd78728, android.widget.RelativeLayout@2fdd5050], index: 1
11-06 15:02:53.733: I/OpenAphid(17658): Update visible views, index 1, buffered: 3
11-06 15:02:53.733: D/OpenAphid(17658): onLayout: 0, 0, 480, 800; child 3
11-06 15:02:53.813: D/OpenAphid(17658): create bitmap 480x800
11-06 15:02:53.813: D/OpenAphid(17658): reloading texture: android.widget.RelativeLayout@2fd78728 and android.widget.RelativeLayout@2fdd5050; old views: android.widget.RelativeLayout@2fd78728, android.widget.RelativeLayout@2fdd5050, front changed false, back changed true
11-06 15:02:53.813: D/OpenAphid(17658): Destroy texture: 1
11-06 15:02:53.813: D/OpenAphid(17658): createTexture: 480, 800; POT: 512, 1024

You can notice that memory is freed before creating second bitmap for Galaxy note where as it is not in S II. So i guessed that issue is with the size of bitmap. I am sorry if i am wrong.

@openaphid
Copy link
Owner

Thanks for the detailed report. The GC logs are fine for this scenario.

I guess the bug could be caused by some incorrect OpenGL renderer settings for the high resolution. I'll dig into it for more clues

@AravindRaj56060
Copy link

Thanks. I am eager for your solution....

@cagkanciloglu
Copy link
Author

No luck?

@openaphid
Copy link
Owner

I'm still struggling to reproduce the bug. I went to a Samsung store and tried a GS3 and a Galaxy Note II with the demo app, but found nothing abnormal.

One user suggested that the problem is easy to reproduce for certain layout hierarchy. If anyone knows a layout pattern for this issue, please share it to us. Thanks for advance.

@cagkanciloglu
Copy link
Author

The layout below is similar to the layout in the video. I hope this helps.

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <ImageView
        android:id="@+id/gallery_flip_item_background_imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/galeritestimage" />



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:background="@color/transparentBlack"
        android:padding="5dp" >


        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/feed_taggeduser_image" />


        <TextView
            android:id="@+id/gallery_flip_item_place_name_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical"
            android:maxLines="2"
            android:text="Merve B (Gezgin): &quot;Big Chefs&apos;te Big Burger yedim ve gerçekten adını hak edecek kadar büyüktu :) Merve B (Gezgin): &quot;Big Chefs&apos;te Big Burger yedim ve gerçekten adını hak edecek kadar büyüktu :)"
            android:textColor="@color/white"
            android:textSize="12sp" />

    </LinearLayout>



    <LinearLayout
        android:id="@+id/gallery_flip_item_place_linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/transparentBlack"
        android:orientation="horizontal"
        android:padding="5dp" >


        <ImageView
            android:id="@+id/gallery_flip_item_place_icon_imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="3dp"
            android:scaleType="fitXY"
            android:src="@drawable/img1" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical" >


            <TextView
                android:id="@+id/gallery_flip_item_place_name_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Rumeli Hısarı İskele Restaurant"
                android:textColor="@color/white"
                android:textSize="19sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="3dp" >


                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/feed_checkin_location" />



                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:orientation="vertical" >


                    <TextView
                        android:id="@+id/gallery_flip_item_place_district_textview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Mercan"
                        android:textColor="@color/white"
                        android:textSize="11sp" />


                    <TextView
                        android:id="@+id/gallery_flip_item_place_city_textview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="İstanbul"
                        android:textColor="@color/white"
                        android:textSize="11sp" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

openaphid added a commit that referenced this issue Dec 9, 2012
@openaphid
Copy link
Owner

@cagkanciloglu
I setup an activity to implement the layout that is supposed to cause issues. Unfortunately, it still runs fine on my new Nexus 7, old Defy+ and emulator.

@cagkanciloglu
Copy link
Author

@openaphid
I will implement the problematic layout and share its apk and source as soon as possible.

@openaphid
Copy link
Owner

@cagkanciloglu Thanks. It would be the best way for me to fix the issue.

@cagkanciloglu
Copy link
Author

https://github.com/cagkanciloglu/FlipViewProblematicLayout

Here is the link for the problematic layout. I shot a new video about the problem too, you can find it in "video" folder.

I guess I got the problem: it seems it happens when application downloads image for background. After the download when I begin the gesture for flipping, the background image is being released immediately. Maybe you can add a listener so when the gesture ends you release the image. That should do the trick.

@openaphid
Copy link
Owner

Sorry for my late reply.

It seems that FlipView doesn't work well with async content. I'll try to figure out a way to improve it in develop branch first

@KyungHo
Copy link

KyungHo commented Dec 28, 2012

The same phenomenon occurs in my program has been.
My device Galaxy Notes 2 and OS version is 4.1.2.
the Galaxy S3 is the same phenomenon occurs.

Until now, the results of testing the devices except the device Galaxy Notes 2 and Galaxy S3,
the problem did not occur.

Ironically Demo apk 2 and Galaxy Note Galaxy S3 has been working well.

Please help me.

@KyungHo
Copy link

KyungHo commented Dec 28, 2012

The layout is the same symptoms that occurred.
Download images asynchronously using a thread.

<RelativeLayout
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center|left"
    android:background="@color/white"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4" >
    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="홍보관"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/backlay"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:gravity="center|left" >

        <Button
            android:id="@+id/backbtn"
            android:layout_width="20dp"
            android:layout_height="25dp"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_marginLeft="10dp"
            android:background="@drawable/backbtnpress" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/spnlay"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:gravity="center|right" >

        <Button
            android:id="@+id/spnbtn"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/spnbtnpress" />
    </LinearLayout>

    <ImageView
        android:id="@+id/bgline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/bg_line" />
</RelativeLayout>

<ImageView
    android:id="@+id/photop"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:layout_weight="10"
    android:clickable="true"
    android:scaleType="centerCrop"
    android:src="@drawable/loading_img_big" />

<ImageView
    android:id="@+id/bgline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/bg_line" />

<TextView
    android:id="@+id/titlep"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center|left"
    android:layout_marginLeft="10dp"
    android:gravity="center|left"
    android:singleLine="true"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/bgline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/bg_line" />

<TextView
    android:id="@+id/descriptionp"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="10dp"
    android:textColor="@android:color/black" />

@openaphid
Copy link
Owner

@KyungHo if you met the same problem as @cagkanciloglu showed in http://www.youtube.com/watch?v=rv0pLLCn-8w . I doubt it could be caused by the OpenGL lighting setup in FlipView. But it's hard for me to debug it as it can't be reproduced on my devices (Defy+ and Nexus 7)

@cagkanciloglu the issue you found in your followup video. It's actually a new issue, which will be resolved by fixing #37 and #28

@openaphid
Copy link
Owner

@cagkanciloglu I migrated your project into the demo app as "Issue #5". I found that its async loading logic is not 100% correct. Please refer to the comments in my commit for more details: https://github.com/openaphid/android-flip/blob/bef17096d3eef18f412916b3c928ca81438c498f/FlipView/Demo/src/com/aphidmobile/flip/demo/issue5/GalleryFlipItem.java

You can also download the updated APK: https://github.com/openaphid/android-flip/blob/develop/FlipView/Demo/APK/Aphid-FlipView-Demo.apk

BTW: I also found the layout hierarchy is not optimal, you can try to improve it if it's used in your real app.

@laxman223
Copy link

i am download your project and importing into my project and it sow errors "R.styleable.FlipViewController_orientation"
R.styleable.FlipViewController_animationBitmapFormat is cannt resloved

@ZoserLock
Copy link

@openaphid We solved the black screen render error on Samsung Galaxy S III device, changing the gluPerspective far plane to a higher number. In FlipRenderer.java,
onSurfaceChange method:

float fovy = 20f;
float eyeZ = height / 2f / (float) Math.tan(TextureUtils.d2r(fovy / 2));
GLU.gluPerspective(gl, fovy, (float) width / (float) height, 0.5f, 5000); //We changed Math.max(2500.0f, eyeZ) to 5000

@openaphid
Copy link
Owner

@ZoserLock Thanks a lot. I will merge it into master branch later.

openaphid added a commit that referenced this issue Jan 17, 2013
@openaphid
Copy link
Owner

I've pushed a fix into develop branch.

Could you guys test it on a Galaxy S3 for me please? APK: https://github.com/openaphid/android-flip/tree/develop/FlipView/Demo/APK

But I still don't get the real reason of the issue. If eyeZ is larger than 2500, zFar should be correctly set to eyeZ by Math.max, which is the case on my Nexus 7. Maybe it's caused by a math rounding issue on certain OpenGL drivers.

@cagkanciloglu
Copy link
Author

Yep. Setting the value to 5000 solved that issue. But after I have implemented the solution I noticed another bug on S3. While I was sliding through the pages I saw some pages coming full white at first then the actual layout. I don't think It's caused by async content because the page comes with complete white other than this I have high speed internet connection. I shot a video about the bug. It's rather short video but you can see the bug. And sorry for the quality.

http://www.youtube.com/watch?v=v5XTKueIIKY

@cagkanciloglu
Copy link
Author

And the layout is almost similar to one you looked early. I just took your relative layout advice and changed view and added cache code for the content image downloader. I can send the layout xml and the Flip Item class too if you want.

@openaphid
Copy link
Owner

The full white issue seems not related to async content. Please share your code that can reproduce the problem.

Is it reproducible on emulator? And what happens if you hold the page when it's full white?

@openaphid openaphid reopened this Jan 17, 2013
@cagkanciloglu
Copy link
Author

XML layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkgrey"
        android:gravity="center"
        android:orientation="vertical" >




        <ImageView
            android:id="@+id/gallery_flip_item_background_imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            android:visibility="visible" />

        <ProgressBar
            android:id="@+id/gallery_flip_item_background_progressbar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:indeterminateBehavior="cycle" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/transparentBlack"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/feed_taggeduser_image" />

        <TextView
            android:id="@+id/gallery_flip_item_place_name_ellipsizingtextview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical"
            android:maxLines="2"
            android:text="Merve B (Gezgin): &quot;Big Chefs&apos;te Big Burger yedim ve gerçekten adını hak edecek kadar büyüktu :) Merve B (Gezgin): &quot;Big Chefs&apos;te Big Burger yedim ve gerçekten adını hak edecek kadar büyüktu :)"
            android:textColor="@color/white"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/gallery_flip_item_place_linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/transparentBlack"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ProgressBar
            android:id="@+id/gallery_flip_item_icon_progressbar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:indeterminate="true"
            android:indeterminateBehavior="cycle" />

        <ImageView
            android:id="@+id/gallery_flip_item_place_icon_imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="3dp"
            android:scaleType="fitXY"
            android:visibility="gone" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/gallery_flip_item_place_name_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Rumeli Hısarı İskele Restaurant"
                android:textColor="@color/white"
                android:textSize="19sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="3dp" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="12dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_place_checkin" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/gallery_flip_item_place_district_textview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Mercan"
                        android:textColor="@color/white"
                        android:textSize="11sp" />

                    <TextView
                        android:id="@+id/gallery_flip_item_place_city_textview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="İstanbul"
                        android:textColor="@color/white"
                        android:textSize="11sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

@cagkanciloglu
Copy link
Author

public class GalleryFlipItem extends LinearLayout
{
    /**
     * Borrowed from the official BitmapFun tutorial: http://developer.android.com/training/displaying-bitmaps/index.html
     */
    private static final class AsyncDrawable extends BitmapDrawable
    {
        private final WeakReference<ContentPhotosDownloader> taskRef;

        public AsyncDrawable(Resources res, Bitmap bitmap, ContentPhotosDownloader task)
        {
            super(res, bitmap);
            this.taskRef = new WeakReference<ContentPhotosDownloader>(task);
        }

        public static ContentPhotosDownloader getTask(ImageView imageView)
        {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof AsyncDrawable)
                return ((AsyncDrawable) drawable).taskRef.get();

            return null;
        }
    }

    // Change to a static inner class from a normal inner class, otherwise it may holds a strong reference to GalleryFlipItem, which is not a good practise for AsyncTask
    private static class ContentPhotosDownloader extends AsyncTask<Void, Void, Bitmap>
    {
        private final String url;

        private final WeakReference<FlipViewController> controllerRef;
        // Use WeakReference
        private final WeakReference<ImageView> imgViewRef;
        private WeakReference<ProgressBar> progressBarRef;
        private final int pageIndex;

        public ContentPhotosDownloader(String url, ImageView imgView, ProgressBar progressBar, FlipViewController controller, int pageIndex)
        {
            if (url == null || imgView == null || controller == null)
                ContentPhotosDownloader.this.cancel(true);

            // Assert.assertNotNull(url);
            // Assert.assertNotNull(imgView);
            // Assert.assertNotNull(controller);
            // progressBar can be null

            this.url = url;
            this.imgViewRef = new WeakReference<ImageView>(imgView);
            this.controllerRef = new WeakReference<FlipViewController>(controller);
            this.pageIndex = pageIndex;
            if (progressBar != null)
                progressBarRef = new WeakReference<ProgressBar>(progressBar);
        }

        public String getUrl()
        {
            return url;
        }

        public int getPageIndex()
        {
            return pageIndex;
        }

        private ProgressBar getProgressBar()
        {
            return progressBarRef != null ? progressBarRef.get() : null;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

            ProgressBar bar = getProgressBar();
            if (bar != null)
            {
                bar.setVisibility(View.VISIBLE);
                ImageView iv = imgViewRef.get();
                if (iv != null)
                    iv.setVisibility(GONE);
            }
        }

        @Override
        protected Bitmap doInBackground(Void... params)
        {
            // Caching code right here
            String filename = String.valueOf(url.hashCode());
            File f = new File(ImageDownloader.getCacheDirectory(imgViewRef.get().getContext()), filename);

            Bitmap bitmap = null;


            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            options.inDither = false; // Disable Dithering mode
            options.inPurgeable = true; // Tell to gc that whether it needs free memory, the Bitmap can be cleared
            options.inInputShareable = true; // Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            options.inTempStorage = new byte[32 * 1024];
            bitmap = BitmapFactory.decodeFile(f.getPath(), options);

            if (bitmap == null)
            {
                InputStream is = null;
                try
                {
                    URL aURL = new URL(url);
                    URLConnection conn = aURL.openConnection();
                    conn.setConnectTimeout(1000);

                    conn.connect();

                    is = new BufferedInputStream(conn.getInputStream());

                    bitmap = BitmapFactory.decodeStream(is);

                    // cache the image

                    String filenameToWrite = String.valueOf(url.hashCode());
                    File fw = new File(ImageDownloader.getCacheDirectory(imgViewRef.get().getContext()), filenameToWrite);

                    ImageDownloader.writeFile(bitmap, fw);

                    return bitmap;
                }
                catch (IOException e)
                {
                    AphidLog.e(e, "Failed to load bitmap from url: " + url);
                }
                finally
                {
                    IO.close(is);
                }
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result)
        {
            if (isCancelled())
                return;

            ImageView imgView = imgViewRef.get();

            if (imgView != null && AsyncDrawable.getTask(imgView) == this)
            { // make sure the ImageView instance has not been reused for another page
                if (result != null)
                {
                    imgView.setImageBitmap(result);
                    imgView.setVisibility(View.VISIBLE);
                }

                ProgressBar bar = getProgressBar();
                if (bar != null)
                    bar.setVisibility(View.GONE);

                FlipViewController controller = controllerRef.get();
                if (controller != null)
                    controller.refreshPage(pageIndex);
            }
        }
    }

    public GalleryPage mGalleryPage;

    private ImageView imgBackground;
    private final ProgressBar pd;
    private final ProgressBar pdIcon;
    private final LinearLayout lnlPlace;

    private ImageView imgIcon;
    private final TextView txtName;
    private final TextView txtDistrict;
    private final TextView txtCity;
    private final TextView txtBody;

    private final Context mContext;

    public void refreshView(GalleryPage mGalleryPage, FlipViewController controller, int pageIndex)
    {
        this.mGalleryPage = mGalleryPage;

        txtName.setText(this.mGalleryPage.getTargetPlace().name);
        txtDistrict.setText(this.mGalleryPage.getTargetPlace().location.district.name);
        txtCity.setText(this.mGalleryPage.getTargetPlace().location.city.name);
        txtBody.setText(this.mGalleryPage.getBody());
        txtBody.setMaxLines(5);

        lnlPlace.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                new MekanDetailActivity.InitForMekanDetailTask(mContext, GalleryFlipItem.this.mGalleryPage.getTargetPlace().id).execute();
            }
        });

        if (shouldStartAsyncLoad(imgIcon, this.mGalleryPage.getTargetPlace().photo.small, pageIndex))
        {
            ContentPhotosDownloader downloader = new ContentPhotosDownloader(this.mGalleryPage.getTargetPlace().photo.small, imgIcon, pdIcon, controller, pageIndex);
            imgIcon.setImageDrawable(new AsyncDrawable(getResources(), null, downloader));
            downloader.execute();
        }

        if (shouldStartAsyncLoad(imgBackground, this.mGalleryPage.getImageURL(), pageIndex))
        {
            ContentPhotosDownloader downloader = new ContentPhotosDownloader(this.mGalleryPage.getImageURL(), imgBackground, pd, controller, pageIndex);
            imgBackground.setImageDrawable(new AsyncDrawable(getResources(), null, downloader));
            downloader.execute();
        }
    }

    private boolean shouldStartAsyncLoad(ImageView imageView, String url, int pageIndex)
    {
        ContentPhotosDownloader downloader = AsyncDrawable.getTask(imageView);
        boolean shouldStart = true;
        if (downloader != null)
        {
            if (downloader.getPageIndex() == pageIndex && url.equals(downloader.getUrl()))
                shouldStart = false;
            else
                downloader.cancel(true);
        }
        return shouldStart;
    }

    @SuppressWarnings("deprecation")
    public GalleryFlipItem(Context context, GalleryPage mGalleryPage, FlipViewController controller, int pageIndex)
    {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.gallery_flip_item_layout, this);

        pd = (ProgressBar) findViewById(R.id.gallery_flip_item_background_progressbar);
        pdIcon = (ProgressBar) findViewById(R.id.gallery_flip_item_icon_progressbar);
        imgBackground = (ImageView) findViewById(R.id.gallery_flip_item_background_imageview);

        lnlPlace = (LinearLayout) findViewById(R.id.gallery_flip_item_place_linearlayout);

        imgIcon = (ImageView) findViewById(R.id.gallery_flip_item_place_icon_imageview);
        txtName = (TextView) findViewById(R.id.gallery_flip_item_place_name_textview);
        txtDistrict = (TextView) findViewById(R.id.gallery_flip_item_place_district_textview);
        txtCity = (TextView) findViewById(R.id.gallery_flip_item_place_city_textview);
        txtBody = (TextView) findViewById(R.id.gallery_flip_item_place_name_ellipsizingtextview);

        // int Measuredwidth = 0;
        // int Measuredheight = 0;
        // Point size = new Point();
        // WindowManager w = ((Activity) context).getWindowManager();
        //
        // Display d = w.getDefaultDisplay();
        // Measuredwidth = d.getWidth();
        // Measuredheight = d.getHeight();
        //
        // // notes: it's not the right approach to make the background fill its parent, using a RelativeLayout could be much better
        // imgBackground.setLayoutParams(new LayoutParams(Measuredwidth, Measuredheight));

        refreshView(mGalleryPage, controller, pageIndex);
    }
}

@cagkanciloglu
Copy link
Author

No luck?

@openaphid
Copy link
Owner

I'm afraid the code snippet you shared is not compilable on my side due to its dependencies.

May I ask why you mixed a RelativeLayout and a LinearLayout inside a FrameLayout? This seems incorrect to me as FrameLayout only displays a single child view.

@cagkanciloglu
Copy link
Author

Can you tell me the dependencies ? So I can provide them for you. Framelayout doesn't display only one view. It displays a relativelayout and two linearlayouts. I didn't understand your question.

@openaphid
Copy link
Owner

I only used FrameLayout to display one item at a time as suggested in its document. I didn't know that it can also be used to display multiple children.

The things failed to compile at my side:

  1. How do you setup ImageDownloader?
  2. There is no getTargetPlace() method for GalleryPage
  3. MekanDetailActivity is unresolvable

Apparently pasting codes in comments is not an efficient way to communicate, is it possible to make a minimal app or to add the case into the demos project?

@pandapo
Copy link

pandapo commented Apr 18, 2013

Hi,

I want flip a view that have many button, example one view have 4 button. How do I make it with your library?

Thanks,

Panda

@FoamyGuy
Copy link

I ran in to this issue when working with HTC One (4.1.2) and Galaxy S4 (4.2.2). Though on those devices the parts of the layouts turned white instead of black, it seems like the same issue. Hardcoding the 5000 inside of FlipRenderer did not have any effect. But switching to the dev branch of the library fixed the problem on both devices.

@cagkanciloglu
Copy link
Author

Yes, switching to the dev branch solved the problem.

@Devendra321
Copy link

@cagkanciloglu and @FoamyGuy
what is dev branch library where to switch please help.

@cagkanciloglu
Copy link
Author

Hi @Devendra321

1.Open https://github.com/openaphid/android-flip
2.On top of the screen you will see 4 tabs. (commits, branches, releases, contributors)
3.Click branches then click develop.

@Devendra321
Copy link

thank you @cagkanciloglu
i use colors x factor mobile of version 4.2.1 but problem remain unchaged, same problem occured.

@Devendra321
Copy link

some times black screen seen and some time smoothly load the same image.Generally first time loading appear the black screen. How to solve this problem please help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants