2014-03-31 92 views
0

我實現使用給定的代碼封面流在http://www.inter-fuser.com/2010/01/android-coverflow-widget.html矩陣不能被修改

的問題是,在該代碼的ImageView用作封面流項。我需要一個自定義的UI(裏面的LinearLayout ImageView的和TextView中)在我的應用程序,所以我創建了一個新的適配器和CoverFlow.java我changd所有的ImageView查看,但這種崩潰的廣告應用程式,並顯示以下錯誤

03-31 18:32:36.486: E/AndroidRuntime(14839): FATAL EXCEPTION: main 
03-31 18:32:36.486: E/AndroidRuntime(14839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cisco.rnd.ytdeom/com.cisco.rnd.carousal.absspinner.CoverFlowAbsActivity}: java.lang.IllegalStateException: Matrix can not be modified 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread.access$700(ActivityThread.java:140) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.os.Looper.loop(Looper.java:137) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread.main(ActivityThread.java:4921) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at java.lang.reflect.Method.invokeNative(Native Method) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at java.lang.reflect.Method.invoke(Method.java:511) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at dalvik.system.NativeStart.main(Native Method) 
03-31 18:32:36.486: E/AndroidRuntime(14839): Caused by: java.lang.IllegalStateException: Matrix can not be modified 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.graphics.Matrix$1.oops(Matrix.java:43) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.graphics.Matrix$1.preTranslate(Matrix.java:109) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverFlow.transformImageBitmap(CoverFlow.java:806) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverFlow.makeAndAddView(CoverFlow.java:861) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverFlow.layout(CoverFlow.java:668) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverAbsSpinner.setSelectionInt(CoverAbsSpinner.java:315) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverAbsSpinner.setSelection(CoverAbsSpinner.java:291) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at com.cisco.rnd.carousal.absspinner.CoverFlowAbsActivity.onCreate(CoverFlowAbsActivity.java:52) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.Activity.performCreate(Activity.java:5188) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
03-31 18:32:36.486: E/AndroidRuntime(14839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 
03-31 18:32:36.486: E/AndroidRuntime(14839): ... 11 more 

我的代碼是

static private void transformImageBitmap(View imageView,int offset, 
     boolean initialLayout, int rotationAngle) { 
    Camera camera = new Camera(); 
    Matrix imageMatrix; 
    int imageHeight; 
    int imageWidth; 
    int bitMapHeight; 
    int bitMapWidth; 
    float scaleWidth; 
    float scaleHeight; 

    imageMatrix = imageView.getMatrix(); 

    camera.translate(0.0f, 0.0f, 100.0f); 

    if (initialLayout) { 
     if(offset < 0) {    
      camera.rotateY(rotationAngle); 
     } else if (offset > 0) { 
      camera.rotateY(-rotationAngle); 
     } else { 
      //Just zoom in a little for the central View 
      camera.translate(0.0f, 0.0f, mMaxZoom); 

     } 
    } else { 
     if (offset == 0) { 
      //As the angle of the view gets less, zoom in 
      int rotation = Math.abs(rotationAngle); 
      if (rotation < 30) { 
       float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); 
       camera.translate(0.0f, 0.0f, zoomAmount);     
      } 
      camera.rotateY(rotationAngle); 
     } 
    } 

    camera.getMatrix(imageMatrix);    

    imageHeight = imageView.getLayoutParams().height; 
    imageWidth = imageView.getLayoutParams().width; 
    bitMapHeight = imageView.getBackground().getIntrinsicHeight(); 
    bitMapWidth = imageView.getBackground().getIntrinsicWidth(); 
    scaleHeight = ((float) imageHeight)/bitMapHeight; 
    scaleWidth = ((float) imageWidth)/bitMapWidth; 

    imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2)); 
    imageMatrix.preScale(scaleWidth, scaleHeight); 
    imageMatrix.postTranslate((imageWidth/2), (imageHeight/2)); 

} 

請幫助...

回答

3

提供的堆棧跟蹤說,你不能碰imageMatrix,所以你應該創建現有矩陣的深層副本和做大規模&翻譯業務該新代碼 用以下代碼替換您的代碼的最後三行:

Matrix newMatrix = new Matrix(); 
newMatrix.set(imageMatrix); 

newMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2)); 
newMatrix.preScale(scaleWidth, scaleHeight); 
newMatrix.postTranslate((imageWidth/2), (imageHeight/2));