4

我在考虑如何实现一个显示图像的ImageView,当我用新图像刷新它的内容时,它会显示一个“正在加载...”文本,右侧有一个圆形ProgressBar ,所以我已经编写了代码附在下面..这是实现我想要的正确方法吗?当其可见性设置为 GONE 时,带有TextViewProgressBar的LinearLayout是否消耗零资源?进度条是否当自身或其父布局的可见性设置为 GONE 时,自身消耗零资源(我正在考虑进度循环圈动画)?如果我将其设置为 INVISIBLE,由于布局管理,它应该会消耗一些资源,但它仍然不应该消耗资源来为进度圈设置动画,对吧?

编辑:当我在上面说“它是否消耗资源”时,我的意思是 CPU 资源,因为它显然会消耗一些内存资源,因为当我只是将其可见性设置为GONE时我没有释放视图。我在第一条评论和第一个答案之后添加了这条评论。

我希望下面的代码是正确的,以防像我这样的其他新手想知道如何实现相同的东西。

遵循代码和在模拟器上显示结果的图像:

在此处输入图像描述

主要的.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>

可绘制/填充矩形.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);
        }
    }
}
4

1 回答 1

12

不。就屏幕上显示的内容而言,它已经消失了,但它仍然在 R.java 中具有内存映射,并且它可用于视图。

如果您创建了一个包含数十亿 GONE 子项的视图,它可能会由于内存耗尽而崩溃。

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

于 2012-05-25T23:29:39.107 回答