Matrix无法修改

时间:2014-03-31 17:47:38

标签: android matrix coverflow

我正在使用http://www.inter-fuser.com/2010/01/android-coverflow-widget.html

中提供的代码实现封面流程

问题是,在此代码中,ImageView用作封面流程项。我需要在我的应用程序中使用自定义UI(在LinearLayout中使用ImageView和TextView),所以我创建了一个新的适配器,在CoverFlow.java中我将所有ImageView变为View,但这会使应用程序崩溃并显示以下错误

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));

}

请帮忙......

1 个答案:

答案 0 :(得分: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));