2013-03-17 96 views
0

布局:ImageView的移动缩放时/旋转

<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

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); 
+0

Doh !.你知道更令人讨厌的是,如果有人问我这个问题,我会给他们这个答案!我想我最近已经投入太多时间了:)谢谢! – Simon 2013-03-17 12:22:19

+0

你能帮助我吗? @Vladimir Mironov,.... http://stackoverflow.com/questions/32023108/android-how-to-scaley-to-based-on-other-objects-position – gumuruh 2015-08-17 00:15:38