The following code shows how to block draw measure layout thread.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:text="hello" /> <com.java2s.SlowMeasureView android:id="@+id/slow_measure" android:layout_width="fill_parent" android:layout_height="30dp" android:background="#00ff00" android:text="slow measure" /> <com.java2s.SlowLayoutView android:id="@+id/slow_layout" android:layout_width="fill_parent" android:layout_height="30dp" android:background="#0000ff" android:text="slow layout" /> <com.java2s.SlowDrawView android:id="@+id/slow_draw" android:layout_width="fill_parent" android:layout_height="30dp" android:background="#ff0000" android:text="slow draw" /> </LinearLayout>
Java code
/******************************************************************************* * Copyright (c) 2012 Manning/*from w w w. jav a 2 s .co m*/ * See the file license.txt for copying permission. ******************************************************************************/ import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.widget.TextView; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } class ThreadUtils { private static final long DEFAULT_SLEEP_DURATION = 2500L; private ThreadUtils() { } public static void sleep() { sleep(DEFAULT_SLEEP_DURATION); } public static void sleep(long duration) { try { Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } } } class SlowDrawView extends TextView { public SlowDrawView(Context context) { super(context); } public SlowDrawView(Context context, AttributeSet attrs) { super(context, attrs); } public SlowDrawView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { /* * Please remember this is just a hack for the purpose of the demonstration. * Do not block the main thread in production code as it may fire an * "Application Not Responding" dialog. */ ThreadUtils.sleep(); super.onDraw(canvas); } } class SlowLayoutView extends TextView { public SlowLayoutView(Context context) { super(context); } public SlowLayoutView(Context context, AttributeSet attrs) { super(context, attrs); } public SlowLayoutView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { /* * Please remember this is just a hack for the purpose of the demonstration. * Do not block the main thread in production code as it may fire an * "Application Not Responding" dialog. */ ThreadUtils.sleep(); super.onLayout(changed, left, top, right, bottom); } } class SlowMeasureView extends TextView { public SlowMeasureView(Context context) { super(context); } public SlowMeasureView(Context context, AttributeSet attrs) { super(context, attrs); } public SlowMeasureView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /* * Please remember this is just a hack for the purpose of the demonstration. * Do not block the main thread in production code as it may fire an * "Application Not Responding" dialog. */ ThreadUtils.sleep(); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } /******************************************************************************* * Copyright (c) 2012 Manning * See the file license.txt for copying permission. ******************************************************************************/