【问题标题】:RelativeLayout width needs same width as biggest image inside itRelativeLayout 宽度需要与其中最大的图像相同的宽度
【发布时间】:2012-04-24 16:09:13
【问题描述】:

我有包含不同相对布局和视图层的相对布局。在一个子相对布局中,我添加了尺寸为 2400*480、1600*480 和 920*480 的图像。现在我希望这些图像保持原始大小,但是当我将它们添加到相对布局时它们会缩小以适应屏幕的宽度和高度,并且它们在缩小时也会保持纵横比。 下面是我的 XML 和添加 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();
    }

即使必须调整相对布局的大小,如何使这些图像适合其原始大小?

【问题讨论】:

  • 首先:您一次加载 72 兆位图数据。我认为仅此一项就会成为许多设备上的问题。第二:看看这个question如何拥有比屏幕更大的视图。

标签: android android-relativelayout


【解决方案1】:

为每个 ImageView 尝试这一行:

 layer2Back.setScaleType(ScaleType.FIT_XY);

我不确定。

【讨论】:

    【解决方案2】:

    我觉得更像这样:

    layer2Back.setScaleType(ScaleType.FIT_CENTER);
    

    【讨论】:

      【解决方案3】:

      我最终使用 Horizo​​ntalScrollView 和 framelayout 来定位图像视图。

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

      【讨论】:

        猜你喜欢
        • 2018-12-29
        • 1970-01-01
        • 1970-01-01
        • 2022-12-05
        • 2013-05-14
        • 1970-01-01
        • 1970-01-01
        • 2011-10-18
        • 2012-08-06
        相关资源
        最近更新 更多