错误与Android 4.3 ImageView的方法getImageMatrix() [英] Bug with Android 4.3 ImageView method getImageMatrix()

查看:362
本文介绍了错误与Android 4.3 ImageView的方法getImageMatrix()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到Android 4.4,有人对我的应用程序的功能都出奇的停止工作。

I recently upgraded to Android 4.4 and someone of the features of my app have surprisingly stopped working.

我有这个code初始化,然后画我的自定义视图。其基本思路是它调节缩放级别,以便整个视图适合在屏幕上。

I have this code for initializing and then drawing my custom view. The basic idea is it adjusts the zoom level so the entire view fits on the screen.

private void initAtZoomLevel(float zoomLevel){
    ....
    Matrix transformMatrix = new Matrix();
    transformMatrix.setScale(initialZoomLevel, initialZoomLevel);
    float yTransCenter = (screenHeight - mapHeight)/2.0f;
    setImageMatrix(transformMatrix);
}

protected void onDraw(Canvas canvas){

    super.onDraw(canvas);
    float[] values = new float[9];
    getImageMatrix().getValues(values);
    scaleFactor = values[0];
    ....
}

这部作品在Android 4.1.2,和4.2.2设备的I HAVE

但是在Android 4.4 / 4.3 getImageMatrix()的GetValues​​(值)停止工作! 它返回一个单位矩阵,而不是变换矩阵我希望在应用程序启动!

But on Android 4.4/4.3 getImageMatrix().getValues(values) stopped working! It returns an identity matrix instead of the transform matrix I expect on app start up!

DEBUG打印输出:

4.1.2 的:@ setImageMatrix(transformMatrix) transformMatrix =矩阵{[0.025122833,0.0,0.0] [0.0,0.025122833,566.5] [0.0,0.0,1.0]}

@ getImageMatrix()的GetValues​​(值) transformMatrix =矩阵{[0.025122833,0.0,0.0] [0.0,0.025122833,566.5] [0.0,0.0,1.0]}

4.4 的:@ setImageMatrix(transformMatrix) transformMatrix =矩阵{[0.025122833,0.0,0.0] [0.0 ,0.025122833,553.0] [0.0,0.0,1.0]}

@ getImageMatrix()的GetValues​​(值) transformMatrix =矩阵{[1.0,0.0,0.0] [0.0,1.0,0.0] [0.0,0.0,1.0]}

我环顾四周,我似乎无法找到关于此的任何文件。不知不觉中,这些图像矩阵为我的观点是被重置;有Android 4.4系统的改变,我们都应该做到这一点的方法吗?有没有其他人在跑这个问题?

I've looked around and I can't seem to find any documentation on this. Somehow the image matrix for my view is being reset; has Android 4.4 changed the way we are supposed to do this? Has anyone else run in to this problem?

注:这个问题似乎已起源于Android的4.3 - 同样的问题发生在一个模拟器

更新:我已经检查从更改日志4.2〜4.3 ,但没有任何有关在那里我可以看到上面的矩阵类,或任何View类。

UPDATE: I have checked the change log from 4.2 to 4.3, but there is nothing on there I can see above the Matrix Class, or anything relevant to the View class.

更新2:我捏到变焦也不能正常工作,使用相同的 setImageMatrix()方法 - 这显然不是坚持,因为没有发生在 getImageMatrix()的GetValues​​()

UPDATE 2: My pinch-to-zoom is also not working, which uses the same setImageMatrix() method - and it's clearly not sticking because nothing happens in getImageMatrix().getValues()

推荐答案

虽然我的previous的回答也勾勒出整体的问题,它实际上可能不是一个错误。正如由通用假日名称, mDrawMatrix 的设置为 mMatrix configureBounds()法 - 这将解决这个问题/错误/不管

While my previous answer does outline the overall problem, it in fact might not be a bug. As pointed out by Generic Holiday Name, mDrawMatrix should be set to mMatrix in the configureBounds() method - which would eliminate this problem/bug/whatever.

然而的,我不得不添加几行code获得 configureBounds()实际工作:

HOWEVER, I had to add several lines of code to get configureBounds() to actually work:

private void configureBounds() {
    if (mDrawable == null || !mHaveFrame) {
        return;
    }

    int dwidth = mDrawableWidth;
    int dheight = mDrawableHeight;

    ...

    if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
        /* If the drawable has no intrinsic size, or we're told to
            scaletofit, then we just fill our entire view.
        */
        mDrawable.setBounds(0, 0, vwidth, vheight);
        mDrawMatrix = null;
    } else {
             //here's the where mDrawMatrix == mMatrix IF using scaleType.MATRIX
             ...
    }
}


因此,为了真正得到这个工作,你需要确保的方式,我期待着与4.2及以下:

  1. mDrawable!= NULL 。这不是一个问题之前,但对于我来说,我没有使用可拉伸所以一切都没有(在返回语句被击中的时候了)
  2. DWIDTH&GT; 0安培;&安培; dheight&GT; 0 。这不是一个问题,如果你有一个真正的绘制,但就像我说的,我不知道。
  3. mHaveFrame = TRUE 。我不知道这是什么了 - 从来没有使用过。设置为true的唯一方法是通过调用的setFrame(INT,INT,INT,INT)
  1. mDrawable != null. This wasn't a problem before, but for my case I wasn't using a drawable so everything was failing (the return statement was hit right away)
  2. 'dwidth >0 && dheight >0. This isn't a problem if you have a real drawable, but like I said, I didn't.
  3. mHaveFrame = true. I had no idea what this was - never used it. The only way to set this to true is by calling setFrame(int, int, int, int).

为了让我的尺度code重新工作,我不得不添加以下内容:

 //potentially a fix for the "bug" that appears in Android 4.3+
 //mDrawable cannot be null anymore for getImageMatrix to work---stupid
 //therefore the imageview class MUST set a drawable for matrix scaling to work
 //so here I am using a "empty" drawable to get around this
 ShapeDrawable fakeDrawable = new ShapeDrawable(); //so mDrawable != null
 fakeDrawable.setIntrinsicHeight(1); //so dwidth and dheight are > 0
 fakeDrawable.setIntrinsicWidth(1);

 setImageDrawable(fakeDrawable);

 setFrame(0, 0, viewWidth, viewHeight); //setting a frame so mHaveFrame = true

YIKES

这篇关于错误与Android 4.3 ImageView的方法getImageMatrix()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆