Etch A Sketch

Swipe your finger to draw a picture. Change the orientation of the device (porttrait to landscape) to erase the picture and start again.

Source code in Etch.zip

  1. MainActivity.java: creates an EtchView object.
  2. EtchView.java: build up a longer and longer Path. We saw a Path in the triangle example in Japan.
  3. activity_main.xml: unused and ignored.
  4. AndroidManifest.xml: untouched.
  5. build.gradle (Module: app)

Examples in ApiDemos

  1. Graphics/FingerPaint (tap the Android menu button and see the options menu)
    1. app/java/com.example.android.apis/graphics/FingerPaint.java: class FingerPaint is a subclass of class GraphicsActivity.
    2. app/java/com.example.android.apis/graphics/GraphicsActivity.java: class GraphicsActivity is a subclass of class Activity. It adds very little to class Activity.

  2. Graphics/Touch Paint (press the menu button for clear/fade)
    1. app/java/com.example.android.apis/graphics/TouchPaint.java: class TouchPaint is a subclass of class GraphicsActivity.
    2. app/java/com.example.android.apis/graphics/GraphicsActivity.java: class GraphicsActivity is a subclass of class Activity. It adds very little to class Activity.

Things to try

  1. Since onDraw is called so often, we should make it as fast as possible. Remove its call to drawColor and replace it by the following statement in the constructor of EtchView immediately after super(context);.
            setBackgroundColor(Color.WHITE);
    

  2. Does the sketch get erased when you change the orientation of the device from portrait to landscape? Give class MainActivity all three pairs of lifecyle methods. Put a call to Log.d into onCreate, too, and import android.util.Log.
    	@Override
    	protected void onStart() {
    		super.onStart();
    		Log.d("myTag", "onStart");
    	}
    
    	@Override
    	protected void onResume() {
    		super.onResume();
    		Log.d("myTag", "onResume");
    	}
    
    	@Override
    	protected void onPause() {
    		super.onPause();
    		Log.d("myTag", "onPause");
    	}
    
    	@Override
    	protected void onStop() {
    		super.onStop();
    		Log.d("myTag", "onStop");
    	}
    
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		Log.d("myTag", "onDestroy");
    	}
    
    Watch the output in your Mac Terminal or PC Command Prompt window as we did in Text.
    adb logcat myTag:D '*:S'
    
    Launch the app with the emulator in portrait orientation and then switch to landscape.
    D/myTag   ( 2018): onPause      (launched in portrait orientation)
    D/myTag   ( 2018): onStart
    D/myTag   ( 2018): onResume
    
    D/myTag   ( 2018): onPause      (switch to landscape)
    D/myTag   ( 2018): onStop
    D/myTag   ( 2018): onDestroy
    D/myTag   ( 2018): onPause
    D/myTag   ( 2018): onStart
    D/myTag   ( 2018): onResume
    
    D/myTag   ( 2018): onPause      (switch back to portrait)
    D/myTag   ( 2018): onStop
    D/myTag   ( 2018): onDestroy
    D/myTag   ( 2018): onPause
    D/myTag   ( 2018): onStart
    D/myTag   ( 2018): onResume
    

    To prevent the Activity object from being destroyed and recreated, see Configuration Changes. In the AndroidManifest.xml file,

            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:configChanges="orientation|screenSize" >
    

    In MainActivity.java,

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_LONG).show();
            //etc.
        }
    

    To test it, draw a line in the upper left corner of the EtchView.


  3. Add an erase button, either in a layout .xml file or programmatically. In the latter case, don’t forget the addView we saw in Programmatically.