动画后如何保存图像?

How to save image after animation?

本题已根据进度修改,以备后用

我在 ImageView 中有一张图片。它附近有一个 rotate 按钮。这是它的作用:

imageView.animate().rotation(imageView.getRotation()+45);

然后我保存修改后的图片:

Bitmap bm = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

但是当我提取更改后的图像时,旧图像出现(未旋转):

rotatedImageView.setImageBitmap(bm);

如何在动画后获取新图像作为 Bitmap

编辑:

根据@Droid Chris 的回答,这就是我得到的:

我将背景设置为黑色,以便您更好地理解。最终它会变得透明

之前:

之后:

观察:

当我将图像旋转 90 的倍数时,@Droid Chris 的回答有效。但是例如 45 的倍数是有问题的...

虽然我确实需要它是 45

解决方案:

所以我使用了@Droid Chris 的功能,但不知何故它正在对我的图像进行下采样。所以我需要在 xml 中为 ImageView 设置 android:scaleType="center"。这解决了下采样问题。

此外,为了保持过渡动画,我必须这样做:

imageView.animate().rotation(imageView.getRotation()+45);

在每次旋转操作时,我想保存时只执行一次:

Bitmap oldBitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap().copy(((BitmapDrawable)imageView.getDrawable()).getBitmap().getConfig(), true);

Bitmap bitmapAfterRotation = getRotatedBitmap(oldBitmap,imageView.getRotation());

此外,在 getRotatedBitmap() 中,我写了 mat.setRotate(angle); 而不是 mat.postRotate(angle);

现在它按预期工作了。

像这样对图像应用矩阵,考虑到您在以这种方式获取宽度和高度时旋转的角度:

((BitmapDrawable)mUserProfileImageView.getDrawable()).getBounds().height()
or
((BitmapDrawable)mUserProfileImageView.getDrawable()).getBounds().width()

private Bitmap getRotatedBitmap(Bitmap bm, int angle, int width, int height) {
    Matrix mat = new Matrix();
    mat.postRotate(angle);

    Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, mat, true);

    bm.recycle();

    return newBitmap;
}