java - 如何在 android/java 中使用矩阵移动和旋转 Canvas 中的位图?

标签 java android bitmap rotation android-canvas

我正在尝试创建一个动画,其中位图应该在围绕自身旋转的同时从屏幕顶部落到底部。

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/

相关文章:

android - 获取 JSON 并对其进行操作

c# - 从常见图像格式中删除所有元数据?

java - Android:无法获取要在 ListView 中显示的数据

java - 当 HTTP 状态为 202 时,Axis 1.4 不解码 SOAP 信封 - 错误还是功能?

java - Spring Cloud - Zuul 代理正在生成 No 'Access-Control-Allow-Origin' ajax 响应

java - 无法运行 ant clean jar ide-dependencies 命令

java - 实现和运行Here API的问题

Android 应用程序在第二次或第三次启动后崩溃(显示内存错误)

android - 从字节数组 (byte[]) 转换位图 - 位图仍然为空

java - VBA 连接正常,但 java 不行