Code example for FrameLayout

Methods: addViewremoveAllViews, getMeasuredHeight

0
            .setMessage("Do you want to clear this " + mDrawMode + "?");
             
            builder.setPositiveButton(getString(R.string.tf_yes), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    mDrawPreview = new DrawView(DrawActivity.this);                    
                    mDrawFrame.removeAllViews(); 
                    mDrawFrame.addView(mDrawPreview);
                } 
            }); 
             
            builder.setNeutralButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                } 
            }); 
             
            mDialog = builder.create();
            break; 
             
        case DIALOG_SAVE:
            builder
            .setMessage("Do you want to save this " + mDrawMode + "?");
             
            builder.setPositiveButton(getString(R.string.tf_yes), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {                    
                    OutputStream outputStream = null;
                     
                    try { 
                        // Replace alpha with white 
                        Bitmap noAlpha = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
                        Canvas canvas = new Canvas(noAlpha);
                        canvas.drawColor(Color.WHITE);
                        canvas.drawBitmap(mBitmap, 0, 0, null);
                         
                        // Write non-alpha image 
                        outputStream = getContentResolver().openOutputStream(mSaveUri);
                        noAlpha.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
                        outputStream.close();
                         
                        setResult(RESULT_OK);
                        finish();
                    } catch (IOException e) {
                        // Ignore exception 
                    } 
                     
                    setResult(RESULT_OK);
                    finish();
                } 
            }); 
             
            builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                } 
            }); 
             
            mDialog = builder.create();
            break; 
        } 
         
        return mDialog;
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu)
    { 
        super.onCreateOptionsMenu(menu);
         
        menu.add(0, COLOR_MENU_ID, 0, "Pick Color").setShortcut('3', 'c');
        menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
        menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
        menu.add(0, ERASE_MENU_ID, 0, "Eraser").setShortcut('5', 'z');
//        menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z'); 
         
        // Only enable menu in annotate or sketch modes (signature uses reasonable presets) 
        return (mDrawMode.equals(KEY_ANNOTATE_MODE) || mDrawMode.equals(KEY_SKETCH_MODE));
    } 
     
    @Override 
    protected void onDestroy()  
    { 
        if (mBitmap != null)
            mBitmap.recycle();
         
        // Logic above 
        super.onDestroy(); 
    } 
     
    /* 
     * (non-Javadoc) 
     * 
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent) 
     */ 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                return true; 
        } 
 
        return super.onKeyDown(keyCode, event);
    } 
     
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    {         
        mPaint.setAlpha(0xFF);
     
        switch (item.getItemId()) {
            case COLOR_MENU_ID:
                new ColorPickerDialog(this, this, mPaint.getColor()).show();
                return true; 
            case EMBOSS_MENU_ID:
                if (mPaint.getMaskFilter() != mEmboss) {
                    mPaint.setMaskFilter(mEmboss);
                } else { 
                    mPaint.setMaskFilter(null);
                } 
                return true; 
            case BLUR_MENU_ID:
                if (mPaint.getMaskFilter() != mBlur) {
                    mPaint.setMaskFilter(mBlur);
                } else { 
                    mPaint.setMaskFilter(null);
                } 
                return true; 
            case ERASE_MENU_ID:
                if (mPaint.getXfermode() == null) {
                    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                } else { 
                    mPaint.setXfermode(null);
                } 
                return true; 
            case SRCATOP_MENU_ID:
                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                mPaint.setAlpha(0x80);
                return true; 
        } 
         
        return super.onOptionsItemSelected(item);
    } 
 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) 
    { 
        super.onPrepareOptionsMenu(menu);
         
        if (mPaint.getMaskFilter() == mEmboss) {
            menu.getItem(1).setTitle("Emboss ON");
        } else { 
            menu.getItem(1).setTitle("Emboss");
        } 
         
        if (mPaint.getMaskFilter() == mBlur) {
            menu.getItem(2).setTitle("Blur ON");
        } else { 
            menu.getItem(2).setTitle("Blur");
        } 
         
        if (mPaint.getXfermode() == null) {
            menu.getItem(3).setTitle("Erase");
        } else { 
            menu.getItem(3).setTitle("Erase ON");
        } 
         
        return true; 
    } 
     
    @Override 
    protected void onResume() 
    { 
        // Logic below 
        super.onResume(); 
         
    } 
     
    @Override 
    protected void onSaveInstanceState(Bundle outState)
    { 
        super.onSaveInstanceState(outState);
    } 
     
    /* 
     * (non-Javadoc) 
     * @see android.app.Activity#onWindowFocusChanged(boolean) 
     *  
     * DrawView depends on the dimensions of mDrawFrame being known 
     * so this must happen here vs. onCreate()  
     */ 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) 
    { 
        if (hasFocus && mDrawFrame == null) {
            mDrawFrame = (FrameLayout) findViewById(R.id.drawPreview); 
            mDrawPreview = new DrawView(this);                       
            mDrawFrame.addView(mDrawPreview);    
        } 
    } 
 
    public class DrawView extends View 
    { 
        private Display mDisplay;
         
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
         
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 3;
         
        public DrawView(Context c) 
        { 
            super(c);
             
            mDisplay = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
             
            if (mBitmap == null) {
                // Load from existing file if one exists 
                mBitmap = BitmapFactory.decodeFile(mSaveUri.getPath());
                 
                // Fall back to empty image 
                if (mBitmap == null) {
                    mBitmap = Bitmap.createBitmap(mDisplay.getWidth(), mDrawFrame.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                } else { 
                    // Make the loaded bitmap non-immutable 
                    // FIXME: known problem - ERASE function will not work with this image 
                    mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
                } 
            } else { 
                // If called a second time this will clear the screen 
                mBitmap = Bitmap.createBitmap(mDisplay.getWidth(), mDrawFrame.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            } 
                         
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);