I was thinking about how to implement an ImageView that shows an image and while I'm refreshing its content with a new image it shows a "Loading..." text with a circular ProgressBar on the right, so I've written the code attached below.. Is this the correct way to implement what I want? Does the LinearLayout with TextView and ProgressBar consume zero resources when it's visibility is set to GONE? Does the ProgressBar itself consume zero resources (I'm thinking about the progress looping circle animation) when itself or it's parent layout's visibility is set to GONE? If I'd set it to INVISIBLE it should consume a bit of resources due to Layout management, but it still should not consume resources for animating the progress circle, right?
EDIT: when I've said "does it consumes resources" above, I meant CPU resources, since it obviously consumes a bit of memory resources since I do not release the view when I simply set its visibility to GONE. I've added this comment after the first comment and the first answer.
I hope the code below is correct, in case some other newbie like me is wondering how to implement the same thing.
Follows the code and an image showing the result on an emulator:
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);
}
}
}
No. It is GONE in terms of what is shown on the screen, but it still has a memory mapping in R.java, and it is available to the View.
If you created a View with several billion GONE children, it would likely crash as a result of memory running out.
That said, It does consume less resources because it does not need to call onMeasure()
or onDraw()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With