在ImageView上调用setImageMatrix后是否曾调用onSizeChanged()或onDraw()?

时间:2015-04-10 20:54:34

标签: android android-imageview

我在调用view.setImageMatrix(matrix)的ImageView上使用pinch and zoom function。问题是我还想为此onSizeChanged调用ImageView,并且在图像缩放或缩小图像后似乎永远不会调用此函数。是否设置了setImageMatrix之后调用的onDraw()和onSizeChanged()?我希望能够访问此方法:

class MyImageView extends ImageView { 

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
    super.onSizeChanged(xNew, yNew, xOld, yOld);
    parent = (View) this.getParent();
    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) parent.getLayoutParams();
    params.height = yNew;
    params.width = xNew;
    parent.setLayoutParams(params);
    if (parent.getParent() != null) {
        if (parent.getParent() instanceof ScrollView) {
            ((ScrollView) parent.getParent()).smoothScrollBy(xNew,yNew);
            ((ScrollView) parent.getParent()).setSmoothScrollingEnabled(true);
            ((ScrollView) parent.getParent()).fullScroll(View.FOCUS_DOWN);

        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

在您的情况下不会调用

onSizeChanged(),因为更改ImageView的矩阵不会更改View的大小。

但您可以通过调用setMatrix(matrix)内的语句来解决您的问题:

@Override
protected void setMatrix(Matrix matrix)
{
    super.setMatrix(Matrix matrix);
    parent = (View) this.getParent();
    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) parent.getLayoutParams();
    params.height = yNew;
    params.width = xNew;
    parent.setLayoutParams(params);
    if (parent.getParent() != null) {
        if (parent.getParent() instanceof ScrollView) {
            ((ScrollView) parent.getParent()).smoothScrollBy(xNew,yNew);
            ((ScrollView) parent.getParent()).setSmoothScrollingEnabled(true);
            ((ScrollView) parent.getParent()).fullScroll(View.FOCUS_DOWN);
}

或者您可以使用ImageView对象调用所有这些而不扩展类。无需挂钩onSizeChanged(...)