简体   繁体   English

RotateAnimation旋转两次,而不是旋转一次

[英]RotateAnimation rotates two times instead of just once

I was trying to implement a feature where a user can rotate a picture. 我正在尝试实现一个用户可以旋转图片的功能。 I thought of using the RotateAnimation for it like so: 我想像这样使用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


    }

});

However, when I do this the picture rotates two times the first time the button is clicked. 但是,当我这样做时,第一次单击该按钮,图片旋转了两次。 Every time after that the picture rotates properly. 每次之后,图片都会正确旋转。 Another thing to note is that the picture saved in bmp has the right rotation is 90deg off what the ImageView ( previewImage ) is showing 还要注意的另一件事是,保存在bmp中的图片具有正确的旋转角度,与ImageView( PreviewImage )所显示的内容相差90deg

Let me know if I can give you guys any further details. 让我知道是否可以给你们进一步的细节。

I'm sorry, but I don't know very well what you are looking for, but if you want to rotate something an image, I suggest that you use a BufferedImage, together with AffineTransform to rotate it. 抱歉,但我不太了解您要寻找的内容,但是如果您要旋转图像,建议您使用BufferedImage以及AffineTransform来旋转它。

First you call the resource at the beggining of the program (if you have an image to call). 首先,您在程序开始时调用资源(如果有要调用的图像)。

BufferedImage image = null;
try {
    image = ImageIO.read( MyClass.class.getResourceAsStream( "scr/someimage.png" ) );
}
catch ( java.io.IOException iOException ) { 
    JOptionPane.showMessageDialog( null, "It was not possible to load the dot cursor." );
    System.exit( 0 );
}

After that, if you wish to rotate, just do: 之后,如果您想旋转,请执行以下操作:

BufferedImage rotatedImage = new BufferedImage( image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB );

AffineTransform rotate = AffineTransform.getRotateInstance( Math.PI/2 );
//Must be in radian. Math.PI/2 = 90º
BufferedImageOp buffIOP = new AffineTransformOp( rotate, null );

Graphics2D g2 = rotatedImage.createGraphics();
g2.drawImage( image, buffIOP, 0, 0 );
g2.dispose();

I don't know if this is usefull for you, but I hope I helped in some way. 我不知道这对您是否有用,但我希望我能以某种方式提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM