缩放/旋转时ImageView会移动

时间:2013-03-17 12:02:38

标签: android

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:background="@android:color/transparent"
              android:id="@+id/globeViewStreamInfoPanel"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <ImageView android:id="@+id/streamIndicator"
               android:src="@drawable/nv_tidestreamindicator"
               android:scaleType="matrix"
               android:layout_alignParentTop="true"
               android:layout_alignParentLeft="true"
               android:layout_margin="10dp"
               android:maxHeight="40dp"
               android:maxWidth="40dp"
               android:layout_width="40dp"
               android:layout_height="40dp"/>

代码(streamIndicatorImageView):

streamIndicatorMatrix.reset();
streamIndicatorMatrix.setScale(size,size);

// rotate indicator in the direction of the flow
streamIndicatorMatrix.setRotate((float)(stream.currentStream.direction));

streamIndicator.setImageMatrix(streamIndicatorMatrix);

当我旋转或缩放或同时旋转时,ImageView会在布局中移动。

奇怪的是,当我在setImagematrix后突破并检查streamIndicatormTopmLeftmWidthmHeight时一切看起来都正确size和我的旋转角度总是合法的,明智的价值。

我只知道这是愚蠢的,我错过了什么?

谢谢!

[编辑]

这是一张照片,我添加了红色箭头指向错误的ImageView:

enter image description here

1 个答案:

答案 0 :(得分:1)

默认情况下,

Matrix.setRotate使用(0, 0)轴心点。这就是图像在布局中移动的原因。有一个Matrix.setRotate方法的重载版本,允许您设置轴心点。

final float density = getResources().getDisplayMetrics().density;
final int center = Math.round(20 * density);

streamIndicatorMatrix.setScale(size,size, center, center);
streamIndicatorMatrix.setRotate((float)(stream.currentStream.direction), center, center);
相关问题