【问题标题】:rotate image around a center of another image围绕另一个图像的中心旋转图像
【发布时间】:2012-06-19 11:16:29
【问题描述】:

我已经用下面链接中的直升机绕着它的中心旋转了一个刻度盘:

http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-a-rotating-dialer/

现在我在拨号器旁边有一个图标,我需要围绕拨号器旋转它,同时让拨号器沿圆形路径旋转。

    private void rotateLogo(float degrees){
                 Matrix nMatrix = new Matrix();
                 Bitmap peopleOrg = BitmapFactory.decodeResource(getResources(), R.drawable.peoplelogo);
                 float translateX = dialerWidth / 2 - dialerWidth / 2;
                 float translateY = dialerHeight / 2 - dialerWidth / 2;
                 nMatrix.preTranslate(-turntable.getWidth()/2, -turntable.getHeight()/2);
                 nMatrix.postRotate(degrees, translateX, translateY);
                 nMatrix.postTranslate(turntable.getWidth()/2, turntable.getHeight()/2); 
                 Bitmap peopleScale = Bitmap.createBitmap(peopleOrg, 0, 0, peopleOrg.getWidth(), peopleOrg.getHeight(), nMatrix, true);
                 peopleLogo.setImageBitmap(peopleScale);        
                 peopleLogo.setImageMatrix(nMatrix);                  
    }

这只会导致图像围绕自己的中心旋转,而不是围绕拨号器的中心点旋转。 我不知道我错在哪里:(

更新

  1. 我基本上需要徽标在圆形路径中移动并成为可点击的视图。
  2. 尝试使用 rotateAnim,但视图没有动画,我无法获取 onclick 事件。
  3. 需要任何可以使用矩阵旋转相同的帮助

【问题讨论】:

  • 这应该会有所帮助:stackoverflow.com/questions/10664006/…
  • @userSeven7s 我很抱歉,但我似乎无法弄清楚它会有什么帮助 :( 抱歉
  • 旋转后添加图片快照..
  • @userSeven7s : 好吧,标志只是围绕自己的中心旋转,而不是背景图像的中心..
  • 您确定徽标在您从 res 加载的图像中完全居中?

标签: android matrix rotation


【解决方案1】:

尝试仅使用peopleOrg 的宽度和高度进行旋转。

nMatrix.postRotate(degrees, peopleOrg.getWidth()/2, peopleOrg.getHeight()/2);

更新:

既然您让我知道您的徽标应该是可点击的视图,那么将徽标图像与您的拨号器合并是不适用的。要围绕拨号器的中心旋转徽标视图,您实际上应该计算徽标视图的(顶部,左侧)点并移动它,而不仅仅是旋转它。

使用sinecosine 函数获取假想圆圆周上的点以绘制徽标视图。

这篇文章将帮助您计算:How do I calculate a point on a circle’s circumference?

【讨论】:

  • 我最初使用 translateX,后来更改为 dialerWidth,根据我对矩阵运算的理解,您首先转换需要旋转的点,然后将其设置回来。我最初只做了预翻译,但在groups.google.com/group/android-developers/browse_thread/thread/… 中读到两者都必须完成。
  • 即使只是调用setIMageMatrix 也应该可以。无需创建位图。只需将位图设置为图像视图并设置矩阵..
  • 抱歉回复晚了。我尝试了提到的更新,但图像仍然围绕它自己的中心点旋转,而不是围绕一个点
  • 您实际上应该计算徽标视图的左上角并移动它,然后只需旋转徽标。使用 sin/cos 函数获取圆周上给定度数的点以绘制视图。
  • 使用 sin 函数并没有那么难,但要弄清楚要做什么参数和计算。所以这需要一些时间。抱歉,我看不到任何其他方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多