Java 旋转动画旋转两次,而不是仅旋转一次

Java 旋转动画旋转两次,而不是仅旋转一次,java,android,rotation,android-imageview,android-bitmap,Java,Android,Rotation,Android Imageview,Android Bitmap,我试图实现一个用户可以旋转图片的功能。我考虑过使用RotateAnimation,如下所示: rotateRight.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //create a new bitmap that is rotated 90 deg Matrix mat = new Matrix(); mat.postRotate(

我试图实现一个用户可以旋转图片的功能。我考虑过使用RotateAnimation,如下所示:

rotateRight.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {

    //create a new bitmap that is rotated 90 deg
    Matrix mat = new Matrix();
    mat.postRotate(90);//90 degree rotation right
    Bitmap bMapRotate = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);//create a new bitmap with the rotated angle
    bmp = bMapRotate; //save the rotated bitmap as the bitmap that will be uploaded

    RotateAnimation rotateAnim = new RotateAnimation(0f, 90f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnim.setDuration((long)100);
    rotateAnim.setRepeatCount(0);
    rotateAnim.setFillAfter(true);//maintains the rotation
    previewImage.startAnimation(rotateAnim);

    previewImage.setImageBitmap(bmp);//set the bitmap to the rotated one (if I don't do this, the animation will always start from the initial position


    }

});
但是,当我这样做时,第一次单击按钮时,图片会旋转两次。每次之后,图片都会正确旋转。另一件需要注意的是,保存在bmp中的图片具有正确的旋转角度,与ImageView(预览图像)显示的角度相差90度


如果我能给你们提供更多的细节,请告诉我。

很抱歉,我不太清楚你们在找什么,但如果你们想旋转图像,我建议你们使用BuffereImage和AffineTransform来旋转它

首先,在程序运行时调用资源(如果要调用映像)

buffereImage image=null;
试一试{
image=ImageIO.read(MyClass.class.getResourceAsStream(“scr/someimage.png”);
}
catch(java.io.IOException-IOException){
showMessageDialog(null,“无法加载点光标”);
系统出口(0);
}
之后,如果您希望旋转,只需执行以下操作:

BufferedImage rotatedImage=新的BufferedImage(image.getWidth()、image.getHeight()、BufferedImage.TYPE\u INT\u ARGB);
AffineTransform rotate=AffineTransform.getRotateInstance(Math.PI/2);
//一定是弧度。Math.PI/2=90º
BuffereImage Op buffIOP=新仿射变换器(旋转,空);
Graphics2D g2=rotateImage.createGraphics();
g2.drawImage(图像,棕黄色,0,0);
g2.dispose();

我不知道这是否对您有用,但我希望我在某种程度上有所帮助。

只是添加了一些内容,当您调用资源时,请确保调用正确。我给你的例子是cals图像“someimage.png”,如果这是直接在src文件上的话。我认为在Android上我不能使用AffineTrasform