2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package com.android.browser;
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Picture;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.webkit.WebView;
27 import android.widget.ImageView;
29 import android.util.Log;
32 * This class is used by ImageAdapter to draw a representation of each tab. It
33 * overrides ImageView so it can be used for the new tab image as well.
35 public class FakeWebView extends ImageView {
36 private TabControl.Tab mTab;
37 private boolean mUsesResource;
39 private class Listener implements WebView.PictureListener {
40 public void onNewPicture(WebView view, Picture p) {
41 FakeWebView.this.invalidate();
45 public FakeWebView(Context context) {
49 public FakeWebView(Context context, AttributeSet attrs) {
50 this(context, attrs, 0);
53 public FakeWebView(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
58 protected void onDraw(Canvas canvas) {
62 // Always draw white behind the picture just in case the picture
64 // FIXME: We used to draw white only when the WebView was null but
65 // sometimes the picture was empty. So now we always draw white. It
66 // would be nice to know if the picture is empty so we can avoid
68 canvas.drawColor(Color.WHITE);
70 final WebView w = mTab.getTopWindow();
72 Picture p = w.capturePicture();
75 float scale = getWidth() * w.getScale() / w.getWidth();
76 canvas.scale(scale, scale);
77 canvas.translate(-w.getScrollX(), -w.getScrollY());
78 canvas.drawPicture(p);
87 public void setImageResource(int resId) {
90 super.setImageResource(resId);
94 * Set a WebView for this FakeWebView to represent.
95 * @param v WebView whose picture and other data will be used in onDraw.
97 public void setTab(TabControl.Tab t) {
98 mUsesResource = false;
100 if (t != null && t.getWebView() != null) {
101 Listener l = new Listener();
102 t.getWebView().setPictureListener(l);
103 if (t.getSubWebView() != null) {
104 t.getSubWebView().setPictureListener(l);