2012-04-01 77 views
2

我想使用一系列RotateDrawable對象來爲復擺制作動畫(因此第二個RotateDrawable的中心點必須「附加」到第一個RotateDrawable上的點)。這意味着我需要計算drawable中的一個點在旋轉發生後的位置。似乎沒有獲取或設置RotateDrawable的主要方法;當事情膨脹時,他們不能直接從XML中檢索它們,我想不出一種方法來做到這一點。如何以編程方式獲得RotateDrawable的樞軸?

回答

0

獲得膨脹期間從XML樞軸可以以類似於的方式來完成:

//r is a Resources object containing the layout 
//id is an integer from R.drawable 
XmlPullParser parser = r.getXml(id); 
AttributeSet attrs = Xml.asAttributeSet(parser); 
float pivotX = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/android", "pivotX", 0.5f); 
float pivotY = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/android", "pivotY", 0.5f); 

//d is a RotateDrawable 
d.inflate(r, parser, attrs); 

屬性應該是零和一個(如果存在)和0.5F之間是由Android如果設置的默認值該屬性不存在。

http://idunnolol.com/android/drawables.html#rotate

重要提示

我已經意識到這一點也很重要,以獲得android:fromDegreesandroid:toDegrees那樣的話,像我一樣,你用onLevelChanged(int)獲得旋轉量,則需要知道你實際獲得了多少旋轉。同樣的方法適用於這些屬性。默認值是0.0f和360.0f(以度爲單位)。級別介於0和10,000之間,其中0對應於fromDegrees和10,000對應於toDegrees

+0

無論如何,我決定使用一個標準的'Drawable'在'Canvas'上顯示一個'Bitmap',因爲'Matrix'類使編程翻譯變得更加容易(並且復擺的相關元素將不得不翻譯)。 – 2012-04-01 16:50:10

相關問題