Android:在ImageView中旋转图像90度但没有延迟

use*_*084 8 android bitmap rotation matrix imageview

我正在开发一款游戏,用户需要点击ImageView中的图像来旋转它.在每个拍子图像上顺时针旋转90度.但是图像需要时间从旧位置旋转到新位置.这妨碍了游戏体验.我使用了以下内容:

protected void onCreate(Bundle savedInstanceState)
{  
... 
...  
    imgview = (ImageView)findViewById(R.id.imageView1);  
    imgview.setOnClickListener(new OnClickListener() {
         @Override 
         public void onClick(View arg0) {
              Matrix matrix = new Matrix();
              matrix.postRotate(90); 
              Bitmap myImg = getBitmapFromDrawable(imgview.getDrawable());
              Bitmap rotated = Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),matrix,true);
              imgview.setImageBitmap(rotated);
         }
});
Run Code Online (Sandbox Code Playgroud)

我想知道有没有其他方法旋转图像 而不会产生任何旋转延迟.

elu*_*eci 34

我也试过做一次,找不到任何其他解决方案,但使用动画.我在这里怎么做.

private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    imgview.startAnimation(rotateAnim);
}
Run Code Online (Sandbox Code Playgroud)

  • 为此(我不确定)你可以设置动画的from-degree.new RotateAnimation(0.0f,degree,...前两个参数是"from"和"to"的度数.如果你知道你从哪个程度旋转你可以设置第一个参数的那个值.这可能会解决这个问题. (2认同)

San*_*ojo 15

您不需要旋转对象,旋转视图应该足够了.如果您的目标API> = 11,您可以随时执行此操作.

mImageView.setRotation(angle);
Run Code Online (Sandbox Code Playgroud)

干杯.


Web*_*eis 5

短方法

View.animate().rotation(90).setDuration(0);