2012-04-18 92 views
0

我有相對佈局,其中包含不同相對佈局和視圖的圖層。在一個孩子相對佈局我添加了圖像的大小是2400 * 480,1600 * 480和920 * 480。現在我希望這些圖像保持原始尺寸,但是當我將它們添加到相對佈局時,它們會縮小以適應屏幕的寬度和高度,並且它們在縮小時也會保持寬高比。 下面是我的XML和添加ImageViews的代碼。RelativeLayout寬度需要與其中最大的圖像相同的寬度

<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(); 
    } 

如何使這些圖像適合其原始大小,即使他們不得不調整相對佈局?

+2

第一:這就是你一次加載的72兆位圖數據。我認爲這在很多設備上都會成爲問題。第二:看看這個[問題](http://stackoverflow.com/questions/3813049/how-to-create-a-view-that-is-bigger-than-the-screen)如何有一個視圖比屏幕大。 – Renard 2012-04-18 09:29:49

回答

0

我結束了使用Horizo​​ntalScrollView用的FrameLayout在彼此定位imageviews。

<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> 
0

嘗試這條線爲每個ImageView的:

layer2Back.setScaleType(ScaleType.FIT_XY); 

我不知道。

+0

不工作的傢伙 – 2012-04-18 09:34:27

0

我覺得更像是:

layer2Back.setScaleType(ScaleType.FIT_CENTER); 
+0

不工作的傢伙 – 2012-04-18 09:34:38