0

我正在开发一个从 RelativeLayout 扩展的子类,并且有一个OnScaleGestureListener. 我希望当您在视图上缩放时,这种变化的高度(就像缩放一样,但是调整对象的大小)。但它给我一个例外,我不知道我该如何解释它。

我的代码是:

public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        tmpScale = detector.getCurrentSpan() / detector.getPreviousSpan();
        Log.d("DEBUG", ".getCurrentSpan() is "+detector.getCurrentSpan());
        Log.d("DEBUG", ".getPreviousSpan() is "+detector.getPreviousSpan());
        Log.d("DEBUG", ".getScaleFactor() is "+detector.getScaleFactor());
        scaling = true;
        RelativeLayout.LayoutParams params = new LayoutParams(kTicketWidth, (int) (getMeasuredHeight()*mScaleFactor));
        setLayoutParams(params);
        //invalidate();
        requestLayout();
    return true;
    }
    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        mScaleFactor = detector.getCurrentSpan() / detector.getPreviousSpan();
        adjustTickets();
        //Expand Animation
        /*if (mScaleFactor >= kMinScale){
            ScaleAnimation anim = new ScaleAnimation(1, 1, mScaleFactor, 1);
            startAnimation(anim);
            tmpScale=1;
            adjustTickets();
        } else if (mScaleFactor < kMinScale){
            ScaleAnimation anim = new ScaleAnimation(1, 1, mScaleFactor, 1);
            startAnimation(anim);
            tmpScale = kMinScale;
            adjustTickets();
        }
        scaling = false;*/
        Log.d("SCALE", "Scale have ended");
    }
}   

另外,我需要的例外是:

05-19 13:10:28.943: E/AndroidRuntime(15701): FATAL EXCEPTION: main
05-19 13:10:28.943: E/AndroidRuntime(15701): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:655)
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-19 13:10:28.943: E/AndroidRuntime(15701):    at android.view.View.measure(View.java:15190)

我不知道为什么会发生的另一件事是 mScaleFactor 不断更改为 1.0,当它更改为 0.81464 或 1.28774 时,它又更改为 1.0。有谁知道为什么会这样?

4

1 回答 1

2

异常显示出了什么问题

 android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

您正在尝试将 LinearLayout 转换为 RelativeLayout ,请检查您的 XML 文件

于 2013-05-19T11:20:02.503 回答