【问题标题】:Apply BLACK & WHITE color filters to Android WebView canvas Slow down its Scrolling Speed将黑白颜色过滤器应用于 Android WebView 画布减慢其滚动速度
【发布时间】:2016-12-09 10:13:00
【问题描述】:

我在我的应用程序中使用 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 速度。任何优化它的建议。 谢谢

【问题讨论】:

    标签: android canvas webview android-custom-view colorfilter


    【解决方案1】:

    您可以将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() 更有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      相关资源
      最近更新 更多