我正在尝试创建一个动画,其中位图应该在围绕自身旋转的同时从屏幕顶部落到底部。
for(int i=0;i<MAX;i++){
posY[i]+=10;
matrix[i].setTranslate(posX[i],posY[i]);
matrix[i].postRotate(10);
c.drawBitmap(lethal[i],matrix[i],null);
if(posY[i]>screenY){
posY[i] =(float) (Math.random() * screenY *-3);
posX[i] = (float) (Math.random() * screenX);
}
}
但是,当我这样做时,矩阵开始向其他方向移动。我希望从上到下的运动是直线的,但图像本身会旋转。
最佳答案
这已经很晚了,但对于尝试相同操作的其他人来说,您可以尝试在 View 绘制函数的每次迭代中创建一个新矩阵。在循环内创建新矩阵后,您可以按正确的顺序应用转换。
Matrix myMatrix;
int translateX = 0;
int translateY = 0;
int rotation = 0;
// Below are variables for the rotation's pivot point.
// The values I gave can pivot around the center of a 200x200 bitmap.
int rotationCenterX = 100;
int rotationCenterY = 100;
@Override
public void onDraw(Canvas c)
{
myMatrix = new Matrix();
myMatrix.postRotate(rotation, rotationCenterX, rotationCenterY);
myMatrix.postTranslate(translateX, translateY);
// apply this matrix to the bitmap
c.drawBitmap(<your bitmap here>, myMatrix, new Paint());
// put this on a loop (just for animation)
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
invalidate();
}
这有点低效,但我很难找到一种更简单的方法来做到这一点。
关于java - 如何在 android/java 中使用矩阵移动和旋转 Canvas 中的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325079/