【问题标题】:Android: Rotate image in ImageView by 90degrees but without delayAndroid:将ImageView中的图像旋转90度但没有延迟
【发布时间】:2013-09-15 03:05:36
【问题描述】:

我正在开发一个游戏,用户需要点击 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);
         }
});

我想知道是否有任何其他旋转方式图像 不会造成任何延迟

【问题讨论】:

标签: android matrix bitmap rotation imageview


【解决方案1】:

我也尝试过一次,除了使用动画之外找不到任何其他解决方案。我会怎么做。

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

【讨论】:

  • 不,你可以直接把这个放在onclick方法里面。你不需要做任何其他事情。 imgview.startAnimation(rotateAnim);直接旋转图片
  • 好的,现在我使用 degree = degree + 90 将图像旋转 90、180、270 和 360 度。但是图像首先回到原来的位置,而不是旋转到给定的位置
  • 为此(我不确定)您可以设置动画的起始度。 new RotateAnimation(0.0f, degree, ... 前两个参数是度数“from”和“to”。如果您知道要旋转的度数,您可以使用该值设置第一个参数。这可能会解决这个问题。
  • 谢谢,它成功了,我使用了 rotate(float fromDegree,float toDegree){...} 并存储了 fromDegree = toDegree 和 toDegree = toDegree + 90;
【解决方案2】:

您不需要旋转对象,旋转视图就足够了。 如果你的目标是 API>=11,你总是可以这样做。

mImageView.setRotation(angle);

干杯。

【讨论】:

  • 如果再次单击旋转按钮,它不起作用。也就是说,两次单击不会将视图旋转 180。而是保持在 90。有没有办法在按钮单击时完成多次旋转?
  • @KritikaJalan 为此,您只需要一个计数器来计算点击次数。 onClick(){ counter++; imageView.setRotation(counter*90)};
  • 是的,我使用的是:img.setRotation(img.getRotation()- 90f);谢谢,@BarthyB。
【解决方案3】:

短方法

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

【讨论】:

    【解决方案4】:

    您是否尝试使用自定义视图扩展 imageview 并在后台线程中旋转图像?

    【讨论】:

    • 请举个例子,图片应该只在用户点击时旋转
    • 因此,这与其说是答案,不如说是评论。我知道你还没有评论的代表。
    • @laalto 是的,我知道我为此道歉。无论如何,您应该使用度参数在自定义视图中创建一个旋转方法,然后从这种方法中启动一个异步任务,执行如下操作: 1- 将位图旋转 1 度或更多度 2- 将位图设置为图像视图资源 3-使视图无效。做这一切,直到你达到在这个方法中传递的度数。这样你就应该有一个没有延迟的动画,你甚至可以决定动画的持续时间和平滑度
    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多