FileDocCategorySizeDatePackage
TouchPaint.javaAPI DocAndroid 1.5 API10540Wed May 06 22:41:08 BST 2009com.example.android.apis.graphics

TouchPaint

public class TouchPaint extends GraphicsActivity
Demonstrates the handling of touch screen and trackball events to implement a simple painting app.

Fields Summary
private static final int
FADE_MSG
Used as a pulse to gradually fade the contents of the window.
private static final int
CLEAR_ID
Menu ID for the command to clear the window.
private static final int
FADE_ID
Menu ID for the command to toggle fading.
private static final int
FADE_DELAY
How often to fade the contents of the window (in ms).
MyView
mView
The view responsible for drawing the window.
boolean
mFading
Is fading mode enabled?
private android.os.Handler
mHandler
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle savedInstanceState)

    
         
        super.onCreate(savedInstanceState);
        
        // Create and attach the view that is responsible for painting.
        mView = new MyView(this);
        setContentView(mView);
        mView.requestFocus();
        
        // Restore the fading option if we are being thawed from a
        // previously saved state.  Note that we are not currently remembering
        // the contents of the bitmap.
        mFading = savedInstanceState != null ? savedInstanceState.getBoolean("fading", true) : true;
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        menu.add(0, CLEAR_ID, 0, "Clear");
        menu.add(0, FADE_ID, 0, "Fade").setCheckable(true);
        return super.onCreateOptionsMenu(menu);
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case CLEAR_ID:
                mView.clear();
                return true;
            case FADE_ID:
                mFading = !mFading;
                if (mFading) {
                    startFading();
                } else {
                    stopFading();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    
protected voidonPause()

        super.onPause();
        // Make sure to never run the fading pulse while we are paused or
        // stopped.
        stopFading();
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        menu.findItem(FADE_ID).setChecked(mFading);
        return super.onPrepareOptionsMenu(menu);
    
protected voidonResume()

        super.onResume();
        // If fading mode is enabled, then as long as we are resumed we want
        // to run pulse to fade the contents.
        if (mFading) {
            startFading();
        }
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        // Save away the fading state to restore if needed later.  Note that
        // we do not currently save the contents of the display.
        outState.putBoolean("fading", mFading);
    
voidstartFading()
Start up the pulse to fade the screen, clearing any existing pulse to ensure that we don't have multiple pulses running at a time.

        mHandler.removeMessages(FADE_MSG);
        mHandler.sendMessageDelayed(
                mHandler.obtainMessage(FADE_MSG), FADE_DELAY);
    
voidstopFading()
Stop the pulse to fade the screen.

        mHandler.removeMessages(FADE_MSG);