具有可见性GONE的LinearLayout是否消耗零资源?

Gia*_*nzi 3 android visibility drawable android-linearlayout progress-bar

我正在考虑如何实现一个显示图像的ImageView,当我用新图像刷新其内容时,它会在右侧显示一个带有圆形ProgressBar的"正在加载..."文本,所以我编写了代码附在下面..这是实现我想要的正确方法吗?具有TextViewProgressBarLinearLayout在其可见性设置为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>
Run Code Online (Sandbox Code Playgroud)

可绘制/ 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>
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*man 11

不是.它在屏幕上显示的内容方面是GONE,但它仍然在R.java中有一个内存映射,它可供View使用.

如果你创建了一个拥有数十亿GONE子节点的View,它可能会因内存耗尽而崩溃.

也就是说,它确实消耗更少的资源,因为它不需要调用onMeasure()onDraw().