RotateAnimation旋转两次而不是一次

时间:2014-06-09 15:19:22

标签: 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(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( previewImage )显示的90度

如果我能告诉你们任何进一步的细节,请告诉我。

1 个答案:

答案 0 :(得分:0)

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

首先,您在程序开始时调用资源(如果您有要调用的图像)。

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 );
}

之后,如果您想旋转,只需执行:

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();

我不知道这对你有用,但我希望我能以某种方式帮助你。