Skip to content

Instantly share code, notes, and snippets.

@MagicMicky
Last active June 16, 2021 22:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagicMicky/3868f9ea762e4ad95884 to your computer and use it in GitHub Desktop.
Save MagicMicky/3868f9ea762e4ad95884 to your computer and use it in GitHub Desktop.
Simple Android program to test performance gain when inflating temporary view programmatically or setting it to GONE and modify to VISIBLE when needed. Used for a blog article http://magicmicky.github.io/android_development/benchmark-using-traceview/
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/F_frame_view"
android:layout_width="match_parent"
android:layout_height="@dimen/triple_row"
android:scrollbars="none"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/default_padding"
android:fillViewport="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RL_inner_view"
android:visibility="gone"
android:padding="@dimen/default_padding">
<ImageView
android:id="@+id/IMG_image"
android:layout_width="@dimen/double_row"
android:layout_height="@dimen/triple_row"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/TV_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_padding"
android:text="@string/hello_world"
android:layout_toRightOf="@id/IMG_image"
/>
<View
android:id="@+id/V_separator"
android:layout_below="@id/TV_text"
android:layout_toRightOf="@id/IMG_image"
android:layout_width="match_parent"
android:layout_height="@dimen/separator_height"
android:layout_marginRight="@dimen/default_padding"
android:layout_marginLeft="@dimen/default_padding"
android:background="@color/separator"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/V_separator"
android:layout_alignParentRight="true"
android:paddingRight="@dimen/double_padding"
android:text="@string/ok"
android:padding="0dp"/>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/F_frame_view"
android:layout_width="match_parent"
android:layout_height="@dimen/triple_row"
android:scrollbars="none"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/default_padding"
android:fillViewport="true"/>
</LinearLayout>
package com.magicmicky.testperfviews;
import android.app.Activity;
import android.graphics.LinearGradient;
import android.os.Debug;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeActivity extends ActionBarActivity {
private static final boolean INFLATE_PROGRAMMATICALY=true;
private static final boolean SHOULD_FYI_VIEW_BE_SHOWN=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
StringBuilder name = new StringBuilder();
name.append("ctm_test_perf");
if(INFLATE_PROGRAMMATICALY)
name.append("_prgm");
else
name.append("_gone");
if(SHOULD_FYI_VIEW_BE_SHOWN)
name.append("_view_shown");
else
name.append("_view_hidden");
Debug.startMethodTracing(name.toString());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout root;
if(INFLATE_PROGRAMMATICALY) {
root = (LinearLayout) inflater.inflate(R.layout.fragment_test_prgmcly, container, false);
if(SHOULD_FYI_VIEW_BE_SHOWN) {
View tutoView = inflater.inflate(R.layout.inner_view, root, false);
root.addView(tutoView);
}
} else {
root = (LinearLayout) inflater.inflate(R.layout.fragment_test_gone,container,false);
if(SHOULD_FYI_VIEW_BE_SHOWN) {
View tutoView = root.findViewById(R.id.RL_inner_view);
tutoView.setVisibility(View.VISIBLE);
}
}
return root;
}
}
@Override
public void onResume() {
super.onResume();
Debug.stopMethodTracing();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/default_padding">
<ImageView
android:id="@+id/IMG_image"
android:layout_width="@dimen/double_row"
android:layout_height="@dimen/triple_row"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/TV_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_padding"
android:text="@string/hello_world"
android:layout_toRightOf="@id/IMG_image"
/>
<View
android:id="@+id/V_separator"
android:layout_below="@id/TV_text"
android:layout_toRightOf="@id/IMG_image"
android:layout_width="match_parent"
android:layout_height="@dimen/separator_height"
android:layout_marginRight="@dimen/default_padding"
android:layout_marginLeft="@dimen/default_padding"
android:background="@color/separator"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/V_separator"
android:layout_alignParentRight="true"
android:paddingRight="@dimen/double_padding"
android:text="@string/ok"
android:padding="0dp"/>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment