下面列出了android.graphics.Camera#getMatrix ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Camera camera = new Camera();
camera.save();
// 设置camera动作为绕Y轴旋转
// 总共旋转180度,因此计算在每个补间时间点interpolatedTime的角度即为两着相乘
// camera.rotateX(deg * interpolatedTime);
camera.rotateY(180 * interpolatedTime);
// camera.rotateZ(180 * interpolatedTime);
//
// 根据camera动作产生一个matrix,赋给Transformation的matrix,以用来设置动画效果
Matrix matrix = t.getMatrix();
camera.getMatrix(matrix);
camera.restore();
//经过以下平移,才能以view的中心点进行翻转
matrix.preTranslate(-view.getWidth() / 2, -view.getHeight() / 2);
matrix.postTranslate(view.getWidth() / 2, view.getHeight() / 2);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
protected final void applyTransformation(float paramFloat, Transformation transformation) {
float f1 = 0.0F - 360.0F * paramFloat;
float f2 = mWidth;
float f3 = mHeight;
final Camera camera = mCamera;
Matrix matrix = transformation.getMatrix();
camera.save();
if ((0x1 & mRotationFlags) == 1)
camera.rotateX(f1);
if ((0x2 & mRotationFlags) == 2)
camera.rotateY(f1);
if ((0x4 & mRotationFlags) == 4)
camera.rotateZ(f1);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-f2, -f3);
matrix.postTranslate(f2, f3);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float fromZ = mFromZ;
float z = fromZ + ((mToZ - fromZ)*interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.translate(0, 0, z);
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
if (mRotateType == TYPEX) {
camera.rotateX(degrees);
} else if (mRotateType == TYPEY) {
camera.rotateY(degrees);
} else if (mRotateType == TYPEZ) {
camera.rotateZ(degrees);
}
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
switch (rotationAxis) {
case X:
camera.rotateX(degrees);
break;
case Y:
camera.rotateY(degrees);
break;
case Z:
camera.rotateZ(degrees);
break;
}
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// 根据时间和起始终止的角度值,插值算出当前的角度:degrees
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
}
else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
//pre:
matrix.preTranslate(-centerX, -centerY);
//post:
matrix.postTranslate(centerX, centerY);
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
Matrix matrix=new Matrix();
matrix.postTranslate(100, 200);
matrix.mapPoints(dst, src);
System.out.println("mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);//dst是 src通过矩阵变换的点 [0,0]点对应到 [100,200]
canvas.drawBitmap(bt, matrix, paint);
//通过下边的三个方法 可以通过 dst矩阵变化后的点 通过矩阵 逆向方法 转换到 invertDst 即是src原点位置 所以dst中[100,200]的点 即是inverDst[0,0]这个点即 src[0,0]点
Matrix invertMatrix = new Matrix();
matrix.invert(invertMatrix);
invertMatrix.mapPoints(invertDst, dst);
System.out.println("invertMatrix_____>>>mapPoints invertDst first Point X:" + invertDst[0] + "\t Y:" + invertDst[1]);
//测试postConcat
Matrix matrixCon =new Matrix();
matrixCon.postTranslate(100, 200);
matrix.postConcat(matrixCon);
matrix.mapPoints(dst, src);
System.out.println("matrixCon: mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("matrixCon: mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);
//测试Camera getMatrix
Camera camera=new Camera();
camera.save();
camera.translate(100,200,0);
camera.getMatrix(matrix);
camera.restore();
matrix.mapPoints(dst, src);
System.out.println("Camera: mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("Camera: mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
Matrix matrix = t.getMatrix();
Camera camera = new Camera();
camera.save();
camera.rotateY(-180 * interpolatedTime);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-mcenterX,-mcenterY);
matrix.postTranslate(mcenterX,mcenterY);
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
protected boolean getChildStaticTransformation(View child, Transformation t) {
Camera camera = new Camera();
int leftCenterView = this.mWidthCenter - this.mChildrenWidthMiddle;
float offset = (-child.getLeft() + leftCenterView) /
this.mSpaceBetweenViews;
if (offset != 0.0F) {
float absOffset = Math.abs(offset);
float scale = (float) Math.pow(0.8999999761581421D, absOffset);
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
t.setAlpha(this.mSetInactiveViewTransparency);
Matrix m = t.getMatrix();
m.setScale(scale, scale);
if (this.mTranslatateEnbabled) {
m.setTranslate(0.0F, this.mTranslate * absOffset);
}
if (offset > 0.0F) {
camera.save();
camera.translate(0.0F, 0.0F, this.mViewZoomOutFactor * offset);
camera.rotateY(this.mCoverflowRotation);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-this.mChildrenWidthMiddle, -this.mChildrenHeight);
m.postTranslate(this.mChildrenWidthMiddle, this.mChildrenHeight);
} else {
camera.save();
camera.translate(0.0F, 0.0F, -(this.mViewZoomOutFactor * offset));
camera.rotateY(-this.mCoverflowRotation);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-this.mChildrenWidthMiddle, -this.mChildrenHeight);
m.postTranslate(this.mChildrenWidthMiddle, this.mChildrenHeight);
}
this.mMatrix.reset();
if (this.mRotationEnabled) {
this.mMatrix.setRotate(this.mRotation * offset);
}
this.mMatrix.preTranslate(-this.mChildrenWidthMiddle, -this.mChildrenHeightMiddle);
this.mMatrix.postTranslate(this.mChildrenWidthMiddle, this.mChildrenHeightMiddle);
m.setConcat(m, this.mMatrix);
}
return true;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
// 生成中间角度
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
// 取得变换后的矩阵
camera.getMatrix(matrix);
camera.restore();
//----------------------------------------------------------------------------
/**
* 修复打脸问题 ( ̄ε(# ̄)☆╰╮( ̄▽ ̄///)
* 简要介绍:
* 原来的3D翻转会由于屏幕像素密度问题而出现效果相差很大
* 例如在屏幕像素比为1,5的手机上显示效果基本正常,
* 而在像素比3,0的手机上面感觉翻转感觉要超出屏幕边缘,
* 有种迎面打脸的感觉、
*
* 解决方案
* 利用屏幕像素密度对变换矩阵进行校正,
* 保证了在所有清晰度的手机上显示的效果基本相同。
*
*/
float[] mValues = {0,0,0,0,0,0,0,0,0};
matrix.getValues(mValues); //获取数值
mValues[6] = mValues[6]/scale; //数值修正
matrix.setValues(mValues); //重新赋值
// Log.e("TAG", "mValues["+0+"]="+mValues[0]+"------------\t"+"mValues["+6+"]="+mValues[6]);
//----------------------------------------------------------------------------
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (degrees>90 || degrees<-90){
if (!isContentChange){
if(listener!=null){
listener.contentChange();
}
isContentChange = true;
}
if (degrees>0) {
degrees = 270 + degrees - 90;
}else if (degrees<0){
degrees = -270+(degrees+90);
}
}
camera.rotateX(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}