【问题标题】:Android Matrix operation throws IllegalStateExceptionAndroid Matrix 操作抛出 IllegalStateException
【发布时间】:2014-03-19 16:05:19
【问题描述】:

我定义了一个扩展类 View 的自定义视图。

public class MyView extends View{
   public MyView(Context context, AttributeSet attrs) {
    super(context, attrs); 
       Matrix m= new Matrix();
       //this line is ok
       m.setScale(...);
   }   

   public boolean onTouchEvent(MotionEvent event){
       //this same operation throws "IllegalStateException: Matrix can not be modified" 
       m.setScale(...);
   }
}

我想知道为什么我会遇到这样的异常。我在网上搜索但根本找不到任何线索。

【问题讨论】:

    标签: android matrix illegalstateexception


    【解决方案1】:

    在类级别声明Matrix 对象m,如下所示...

    public class MyView extends View{
    
       Matrix m;
    
       public MyView(Context context, AttributeSet attrs) {
           super(context, attrs); 
    
           m = new Matrix();
           m.setScale(...);
       }   
    
       public boolean onTouchEvent(MotionEvent event){
    
           m.setScale(...);
    
           return true;
       }
    }
    

    【讨论】:

    • 我找到了原因。这是我的错。我以为我使用 m = new Matrix(); 初始化了我的矩阵但实际上是代码中的“m = getMatrix()”。对于那个很抱歉。看起来视图的矩阵是不可变的。这就是我得到异常的原因?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多