I am implementing cover flow using the code given at http://www.inter-fuser.com/2010/01/android-coverflow-widget.html
The problem is, in this code ImageView is used as cover flow item. I need a custom UI (ImageView and TextView inside LinearLayout) in my application so I created a new adapter and in CoverFlow.java I changd all ImageView to View, but this is crashing the applicaion and showing below error
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
my code is
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));
}
Please help...
The stack trace provided says that you cannot touch the imageMatrix, So you should create a deep copy of the existing matrix and do the scale & translate operations on the new one replace last three lines of your code with the code below:
Matrix newMatrix = new Matrix();
newMatrix.set(imageMatrix);
newMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2));
newMatrix.preScale(scaleWidth, scaleHeight);
newMatrix.postTranslate((imageWidth/2), (imageHeight/2));