所以,我实现了一个类似Android 3D Carousel的旋转木马。
现在,只要我的 getChildStaticTransformation(查看子,转换转换)方法如下所示:
@Override
protected boolean getChildStaticTransformation(View child, Transformation transformation) {
transformation.clear();
transformation.setTransformationType(Transformation.TYPE_MATRIX);
// Center of the view
float centerX = (float)getWidth()/2, centerY = (float)getHeight()/2;
// Save camera
mCamera.save();
// Translate the item to it's coordinates
final Matrix matrix = transformation.getMatrix();
matrix.reset();
mCamera.translate(((CarouselItem)child).getItemX(), ((CarouselItem)child).getItemY(),
((CarouselItem)child).getItemZ());
// Align the item
mCamera.getMatrix(matrix);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
float[] values = new float[9];
matrix.getValues(values);
// Restore camera
mCamera.restore();
Matrix mm = new Matrix();
mm.setValues(values);
((CarouselItem)child).setCIMatrix(mm);
//http://code.google.com/p/android/issues/detail?id=35178
child.invalidate();
return true;
}
一切都很好看:
但是只要我添加y旋转:
@Override
protected boolean getChildStaticTransformation(View child, Transformation transformation) {
transformation.clear();
transformation.setTransformationType(Transformation.TYPE_MATRIX);
// Center of the view
float centerX = (float)getWidth()/2, centerY = (float)getHeight()/2;
// Save camera
mCamera.save();
// Translate the item to it's coordinates
final Matrix matrix = transformation.getMatrix();
matrix.reset();
mCamera.translate(((CarouselItem)child).getItemX(), ((CarouselItem)child).getItemY(),
((CarouselItem)child).getItemZ());
// rotation start
float rotationAngle = ((CarouselItem)child).getCurrentAngle();
if(rotationAngle > 90){
rotationAngle = 180 - rotationAngle;
}
if(rotationAngle < -90){
rotationAngle = 180 - rotationAngle;
rotationAngle = -1 * (360 - rotationAngle);
}
mCamera.rotateY(rotationAngle);
// rotation end
// Align the item
mCamera.getMatrix(matrix);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
float[] values = new float[9];
matrix.getValues(values);
// Restore camera
mCamera.restore();
Matrix mm = new Matrix();
mm.setValues(values);
((CarouselItem)child).setCIMatrix(mm);
//http://code.google.com/p/android/issues/detail?id=35178
child.invalidate();
return true;
}
项目的位置被拧紧,查看项目与中心项目的相对距离:
我没有想法。知道这是从哪里来的吗?
答案 0 :(得分:0)
你必须改变基质的乘法方向。 我的意思是,你必须先应用旋转。只有在那之后你才能翻译对象。
使用方向:缩放,旋转,翻译。