Skip to content

Instantly share code, notes, and snippets.

@marinat
Created July 22, 2014 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinat/29ce930934f9e9ca8794 to your computer and use it in GitHub Desktop.
Save marinat/29ce930934f9e9ca8794 to your computer and use it in GitHub Desktop.
package com.example.wwwithscrolltest;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
public class TitleBarWebView extends WebView {
private View mTitleBar;
public EditText title;
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) {
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());
mTitleBarLayoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
40, 0, 0);
addView(v, mTitleBarLayoutParams);
}
mTitleBar = v;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
if(mTitleBar != null) {
final int sy = getScrollY();
int titleBarOffs = mTitleBar.getHeight() - sy;
if(titleBarOffs < 0) titleBarOffs = 0;
canvas.translate(0, titleBarOffs);
}
super.onDraw(canvas);
canvas.restore();
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
if(child == mTitleBar) {
mTitleBar.offsetLeftAndRight((int) (getScrollX() - mTitleBar.getLeft()));
}
return super.drawChild(canvas, child, drawingTime);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Log.d("SCROLL", "t = " + t + " oldT = " + oldt);
super.onScrollChanged(l, t, oldl, oldt);
if (t < 0)
return;
if (t > oldt) {
if (title.getY() - (t - oldt) / 2 > -title.getHeight())
title.setY(title.getY() - (t - oldt) / 2);
else
title.setY(-title.getHeight());
} else if (t < oldt) {
if (title.getY() + (oldt - t) / 2 < 0)
title.setY(title.getY() + (oldt - t) / 2);
else
title.setY(0);
}
if (t == 0 && oldt == 0)
title.setY(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment