LinearLayout设置为GONE状态时是否不消耗资源?

5
我在思考如何实现一个ImageView,它展示一张图片,同时在刷新其内容时,右侧显示一个"正在加载..."文本以及一个圆形的ProgressBar。所以我写了下面的代码,是否正确呢?当LinearLayout中的TextView和ProgressBar的可见性设置为GONE时,它们消耗零资源吗?ProgressBar本身消耗零资源(我指的是进度循环动画)当它自己或其父布局的可见性被设置为GONE时?如果将它设置为INVISIBLE,那么由于布局管理,它应该会消耗一些资源,但它仍然不应该消耗动画进度圆的资源,对吗?
编辑:当我上面说"它是否消耗资源"时,我指的是CPU资源,因为显然它消耗了一些内存资源,由于我仅将其可见性设置为GONE而没有释放视图。我在第一条评论和第一篇回答后添加了这个注释。
我希望下面的代码是正确的,以防像我这样的新手想要实现相同的东西。
以下是代码和在模拟器上显示结果的图像:
main.xml
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/image_view1"
            android:src="@drawable/fish"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:contentDescription="image view 1" />


       <LinearLayout
           android:id="@+id/layout_progress"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_gravity="center_vertical"
           android:background="@drawable/filled_rectangle"
           android:gravity="center_vertical|center_horizontal"
           android:orientation="horizontal"
           android:visibility="gone" >

           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="fill_horizontal"
               android:text="Loading..."  
               android:textSize="30sp" 
               android:layout_gravity="center_vertical"/>

               <!-- style="@android:style/Widget.ProgressBar.Small" --> 
           <ProgressBar
               android:id="@+id/progress_bar1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:indeterminateOnly="true"/>

       </LinearLayout>
   </FrameLayout>

   <Button
       android:id="@+id/refresh_image"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
       android:onClick="onClick" 
       android:text="@string/refresh_image"/>

</LinearLayout>

drawables/filled_rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#80000000"/>
</shape>

TestFrameLayoutActivity.java

public class TestFrameLayoutActivity extends Activity {
    private int progressVisible;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_progress);
        progressVisible = (layout.getVisibility() == View.VISIBLE)?1:0;
    }

    public void onClick (View view) {
        progressVisible = 1 - progressVisible;
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_progress);
        ImageView img = (ImageView) findViewById(R.id.image_view1);

        if (progressVisible == 1) {
            layout.setVisibility(ProgressBar.VISIBLE);
        } else {
            layout.setVisibility(ProgressBar.GONE);
        }
    }
}

如果您不再需要视图,则只需将其删除。 - K-ballo
@K-ballo 我并不想完全删除它,因为当用户选择刷新图像时,我可能很快就需要它。如果它只占用内存而不消耗 CPU,那么最好(在我看来)将它留在内存中,随时准备下一次使用,而不是在需要时销毁和重新创建它。也许这取决于我预期会有多少次图像刷新。 - Gianni Costanzi
不要删除它,只需从布局中移除。当您再次需要它时,可以将其重新添加到布局中。没有任何阻止您在内存中保留不属于布局的视图。 - K-ballo
@K-ballo 告诉我是否我理解正确:我仍然可以在main.xml文件中定义ProgressView,然后通过保持对它的引用,在编程时从布局中删除它,并在需要时将其添加回布局中,对吗?但是,相对于将其可见性设置为GONE,有什么优势呢?如果我理解它的工作原理,两种情况下都不应该消耗CPU并且占用相同的内存。 - Gianni Costanzi
2
确实,这就是你可以做的。我只会将可见性设置为 GONE,但是通过从布局中删除视图,你可以让其更接近 _消耗零资源_。 - K-ballo
1个回答

13

不会。虽然在屏幕上不再显示,但它仍在R.java中有一个内存映射,并且可以供View使用。

如果您创建了一个包含数十亿个GONE子元素的View,由于内存耗尽而导致崩溃的可能性很大。

话虽如此,它确实会消耗更少的资源,因为它不需要调用onMeasure()onDraw()


好的,所以它仍然会消耗内存,但不需要绘制动画和/或消耗CPU资源。我不希望它完全释放,因为下次用户想要刷新ImageView的内容时我还需要它,这种情况下我会再次将其可见性设置为VISIBLE。 - Gianni Costanzi
确实。如果这回答了你的问题,请确保将其标记为正确。 :) - Codeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接