应用黑色&;Android WebView画布上的白色过滤器会降低其滚动速度

应用黑色&;Android WebView画布上的白色过滤器会降低其滚动速度,android,canvas,webview,android-custom-view,colorfilter,Android,Canvas,Webview,Android Custom View,Colorfilter,我在我的应用程序中使用android WebView来加载我的网页,我想帮助我的用户查看黑白色的WebView内容。 我在WebView的onDraw方法上应用了颜色过滤器: public class WebViewInverterDummy extends WebView { private Paint paint; private ColorFilter cf; private Rect inversionRect; private Canvas cc; private int NO

我在我的应用程序中使用android WebView来加载我的网页,我想帮助我的用户查看黑白色的WebView内容。 我在WebView的onDraw方法上应用了颜色过滤器:

public class WebViewInverterDummy extends WebView {


private Paint paint;
private ColorFilter cf;
private Rect inversionRect;
private Canvas cc;


private int NORMAL_MODE = 0;
private int BLACK_WHITE_MODE = 1;
public int WEB_VIEW_MODE = 0;
private int LAST_SELECTED_MODE = 0;


//    private Bitmap bitmap;

private ColorMatrix normalViewMatrix;
private ColorMatrix blackViewMatrix;

public WebViewInverterDummy(Context context) {
    super(context);
    intiFields();
    init();
    paint = new Paint();
    intBlackViewMatrix();
}
private void intiFields() {
    inversionRect = new Rect();
    cc = new Canvas();
}


@Override
protected void onDraw(Canvas c) {

     if (WEB_VIEW_MODE == NORMAL_MODE) {
        super.onDraw(c);
    } else {
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            cc = new Canvas(bitmap);
        }
        super.onDraw(cc);
        init();
        c.drawColor(Color.WHITE);
        c.drawBitmap(bitmap, 0, 0, paint);
    }
}

private void init() {

    if (cf == null || WEB_VIEW_MODE != LAST_SELECTED_MODE) {
        if (WEB_VIEW_MODE == BLACK_WHITE_MODE) {
            if (blackViewMatrix == null)
                intBlackViewMatrix();
            cf = new ColorMatrixColorFilter(blackViewMatrix);
        }
    }
}


public void setWEB_VIEW_MODE(int mode) {
    LAST_SELECTED_MODE = WEB_VIEW_MODE;
    WEB_VIEW_MODE = mode;
}

public int getWebViewMode() {
    return WEB_VIEW_MODE;
}


private ColorMatrix intBlackViewMatrix() {
    // Black & white Mode
    float[] mx = new float[]{
            -1.0f, 0, 0, 0, 255, //red
            0, -1.0f, 0, 0, 255, //green
            0, 0, -1.0f, 0, 255, //blue
            0, 0, 0, 1.0f, 0 //alpha
    };

    blackViewMatrix = new ColorMatrix(mx);
    blackViewMatrix.setSaturation(0);
    return blackViewMatrix;
}

}
我成功地在WebView上应用了黑白颜色,但问题是它减慢了WebView的滚动速度。我如何优化它,是否有任何方法可以直接在画布上应用颜色过滤器,因为在画布上绘制位图会降低WebView速度。是否有任何优化它的建议。
谢谢

您可以将您的
ColorMatrixColorFilter
直接应用于您主要活动中的WebView

// Initialize `paint`.
Paint paint = new Paint();

// Setup the float array.
float[] mx = {
    -1.0f, 0, 0, 0, 255,  // red
    0, -1.0f, 0, 0, 255,  // green
    0, 0, -1.0f, 0, 255,  // blue
    0, 0, 0, -1.0f, 0     // alpha
};

// Set `paint` to use `mx` as the `ColorMatrixColorFilter`.
paint.setColorFilter(new ColorMatrixColorFilter(mx));

// Apply `paint` to `webView`.
webView.setLayerType(View.LAYER_TYPE_HARDWARE, paint);

这比创建位图并对其应用
ColorMatrixColorFilter
要高效得多
onDraw()

您可以在主要活动中将
ColorMatrixColorFilter
直接应用到WebView

// Initialize `paint`.
Paint paint = new Paint();

// Setup the float array.
float[] mx = {
    -1.0f, 0, 0, 0, 255,  // red
    0, -1.0f, 0, 0, 255,  // green
    0, 0, -1.0f, 0, 255,  // blue
    0, 0, 0, -1.0f, 0     // alpha
};

// Set `paint` to use `mx` as the `ColorMatrixColorFilter`.
paint.setColorFilter(new ColorMatrixColorFilter(mx));

// Apply `paint` to `webView`.
webView.setLayerType(View.LAYER_TYPE_HARDWARE, paint);
这比创建位图并将
ColorMatrixColorFilter
应用于位图
onDraw()
要高效得多