0

I have Relative Layout containing layers of different Relative layouts and Views. In one child Relative layout I have added images whose size are 2400*480, 1600*480 and 920*480. Now I want these images to maintain there original size but they got shrink to fit width and height of screen when I add them to Relative layout and they also keep aspect ratio when they got shrink. Below is my XML and code for adding ImageViews.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        /* Some other layouts */
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"                      
        android:id="@+id/parallaxLayers"        
        android:visibility="gone">      
    </RelativeLayout>
    /* Viewgroup */    
</RelativeLayout>

RelativeLayout parallaxLayout = (RelativeLayout)findViewById(R.id.parallaxLayers);

private void addParallaxLayers() {
        // TODO Auto-generated method stub
        InputStream s1 = getResources().openRawResource(R.drawable.parallax_layer1);
        InputStream s2 = getResources().openRawResource(R.drawable.parallax_layer2);
        InputStream s3 = getResources().openRawResource(R.drawable.parallax_layer3);
        InputStream s4 = getResources().openRawResource(R.drawable.parallax_layer4);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        System.gc();
        bitmap = bitmap(s1);
        layer1Back = new ImageView(this);       
        layer1Back.setImageBitmap(bitmap);
        parallaxLayout.addView(layer1Back, 0, lp);
        try {
            s1.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bitmap = null;
        s1 = null;
        System.gc();

        bitmap = bitmap(s2);
        layer2Back = new ImageView(this);
        layer2Back.setImageBitmap(bitmap);
        parallaxLayout.addView(layer2Back, 1, lp);
        try {
            s2.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bitmap = null;
        s2 = null;
        System.gc();

        bitmap = bitmap(s3);
        layer3Back = new ImageView(this);
        layer3Back.setImageBitmap(bitmap);
        parallaxLayout.addView(layer3Back, 2, lp);
        try {
            s3.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bitmap = null;
        s3 = null;
        System.gc();

        bitmap = bitmap(s4);
        layer4Back = new ImageView(this);
        layer4Back.setImageBitmap(bitmap);
        parallaxLayout.addView(layer4Back, 3, lp);
        try {
            s4.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bitmap = null;
        s4 = null;
        System.gc();
    }

How to make those images fit their original size even if they have to resize Relative Layouts?

Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
  • 2
    first: thats 72 meg of bitmap data you load at once. I think that alone is going to be a problem on many devices. Second: look at this [question](http://stackoverflow.com/questions/3813049/how-to-create-a-view-that-is-bigger-than-the-screen) on how to have a view which is larger than the screen. – Renard Apr 18 '12 at 09:29

3 Answers3

0

Try This line for each ImageView:

 layer2Back.setScaleType(ScaleType.FIT_XY);

I am not sure.

Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
Himanshu Mittal
  • 225
  • 1
  • 5
0

I think is more like that :

layer2Back.setScaleType(ScaleType.FIT_CENTER);
Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
FUBUs
  • 617
  • 5
  • 9
0

I ended up using HorizontalScrollView with framelayout to position imageviews over each other.

<HorizontalScrollView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fadingEdge="none">
            <FrameLayout android:layout_width="wrap_content"
                android:layout_height="fill_parent"                   
                android:id="@+id/parallaxLayers"        
                android:visibility="gone">      
            </FrameLayout>
        </HorizontalScrollView>
Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68