Android ImageView PostInvalidate始终无法正常工作

时间:2017-08-22 07:03:21

标签: android android-layout imageview okhttp3

我在Android中扩展了一个ImageView,我下载了一个图像,当图像到达时,我使视图无效以重新绘制。如果我使用缓存它不会一直工作,所以我怀疑如果系统刚刚重新绘制,android会忽略一些无效命令。如果没有缓存,它的速度会慢一点,而且我的绘图工作正常,缓存会更快,并且不时会重新绘制所有的imageview。 (在流程中,我得到框架的第一次onDraw调用,当我的图像准备就绪时,我调用postInvalidate,然后我得到第二个onDraw,我在画布上绘制位图)

@Override
    public void onLoadImage(Bitmap image, State state) {

        _image = image;
        postInvalidate();


    }

 @Override
    protected void onDraw(Canvas canvas) {


        if (_image == null) {
            super.onDraw(canvas);
        } else {

            Rect destination = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());

            canvas.drawBitmap(_image, null, destination, _paint);
        }

    }

okHttpClient配置如下(下图),onLoadImage发生在来自网络库的onResponse回调中,因此不在UI线程中,这就是为什么我使用postInvalidate而不是Invalidate

private static volatile OkHttpClient _httpClient = null;

    public static OkHttpClient getHttpClient() {

        if(_httpClient == null)
        {
            synchronized (Factory.class) {
                if (_httpClient == null) _httpClient = new OkHttpClient.Builder().retryOnConnectionFailure(true).cache(getHttpCache()).build();
            }
        }
        return _httpClient;
    }

在某些情况下,android会忽略Invalidate吗?欢迎任何帮助! THX

0 个答案:

没有答案