I was toying around with the implementation of the embedded title bar view. 
I attached the source code to this post. The "TitleBarWebView" is tested on 
Android 2.3 and 4.0.1. I couldn't get my hands on 4.1 yet. The emulator is 
simply too slow on my development machine and I have yet to find a 
VirtualBox image for it.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
package com.nobu_games.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class TitleBarWebView extends WebView {
    private View mTitleBar;
    private LayoutParams mTitleBarLayoutParams;
    private Matrix mMatrix = new Matrix();
    private Rect mClipBounds = new Rect();

    public TitleBarWebView(Context context) {
        super(context);

    }

    public TitleBarWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TitleBarWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setEmbeddedTitleBar(View v) {
        if(mTitleBar == v) return;
        if(mTitleBar != null) {
            removeView(mTitleBar);
        }
        if(null != v) {
            mTitleBarLayoutParams = new LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0);
            addView(v, mTitleBarLayoutParams);
            setInitialScale(100);
        }
        mTitleBar = v;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        if(mTitleBar != null) {
            final int sy = getScrollY();
            final int sx = getScrollX();
            mClipBounds.top = sy;
            mClipBounds.left = sx;
            mClipBounds.right = mClipBounds.left + getWidth();
            mClipBounds.bottom = mClipBounds.top + getHeight();
            canvas.clipRect(mClipBounds);
            mMatrix.set(canvas.getMatrix());
            int titleBarOffs = mTitleBar.getHeight() - sy;
            if(titleBarOffs < 0) titleBarOffs = 0;
            mMatrix.postTranslate(0, titleBarOffs);
            canvas.setMatrix(mMatrix);
        }

        super.onDraw(canvas);
        canvas.restore();
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        if(child == mTitleBar) {
            mClipBounds.top = 0;
            mClipBounds.left = 0;
            mClipBounds.right = mClipBounds.left + child.getWidth();
            mClipBounds.bottom = child.getHeight();
            canvas.save();
            child.setDrawingCacheEnabled(true);
            mMatrix.set(canvas.getMatrix());
            mMatrix.postTranslate(getScrollX(), -getScrollY());
            canvas.setMatrix(mMatrix);
            canvas.clipRect(mClipBounds);
            child.draw(canvas);
            canvas.restore();

            return false;
        }

        return super.drawChild(canvas, child, drawingTime);
    }
}

Reply via email to