2011-12-07 19 views
64

私はImageViewを持っており、それにシンプルなスケールのアニメーションを行います。非常に標準的なコード。Androidの画像スケールのアニメーション中心点を基準にして

マイscale_up.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale android:fromXScale="1" 
      android:fromYScale="1" 
      android:toXScale="1.2" 
      android:toYScale="1.2" 
      android:duration="175"/> 
</set> 

私のアニメーションコード:

Animation a = AnimationUtils.loadAnimation(this, R.anim.scale_up); 
((ImageView) findViewById(R.id.circle_image)).startAnimation(a); 

問題:

画像は、それは中心からスケールしないスケール、しかしから左上隅。換言すれば、画像の縮尺変更されたバージョンは中心と同じポイントを持たないが、同じ左上のポイントを有する。 Here's a link that explains what I mean.最初のイメージはアニメーションのスケールです.2番目のイメージはスケールをどのようにしたいのです。中心点を同じに保つ必要があります。私はコンテナ上でイメージ上に重力を設定しようとしましたが、左または右に揃えて、常に同じ縮尺になりました。 私はメイン画面にRelativeLayoutを使用していますが、ImageViewは別のRelativeLayoutにありますが、他のレイアウトを試しましたが、変更はありませんでした。

答えて

63

追加の翻訳を忘れては、半分の幅と高さにandroid:pivotXandroid:pivotYを設定し、それが画像の中心からスケールします。

5

セット内のtranslateアニメーションを使用して、それをオフセットすることができます。あなたはおそらくtoXDeltaとtoDeltaの値を微調整する必要がありますので、イメージを中央に保持するようにしてください。

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale android:fromXScale="1" 
     android:fromYScale="1" 
     android:toXScale="1.2" 
     android:toYScale="1.2" 
     android:duration="175"/> 
    <translate 
     android:fromXDelta="0" 
     android:fromYDelta="0" 
     android:toXDelta="-20%" 
     android:toYDelta="-20%" 
     android:duration="175"/> 
</set> 
123

50%はアニメーションビューの中心です。

50%pは親

<scale 
    android:fromXScale="1.0" 
    android:toXScale="1.2" 
    android:fromYScale="1.0" 
    android:toYScale="1.2" 
    android:pivotX="50%p" 
    android:pivotY="50%p" 
    android:duration="175"/> 

+0

50%pが仕事をしました。 –

+21

私の場合50%が仕事をしました(pなし) – agamov

+2

アニメーションを適用するコンポーネントの幅または高さに相対的であればpなしでなければなりません。 pは、アニメーションを適用するurのコンポーネントの親を指します。 –

70

の中心地である@stevanveltemaと@JiangQiが提供する答えが最適ですが、あなたは、コードを使用してスケーリングしたい場合、あなたは私の答えを使用することができます。

// first 0f, 1f mean scaling from X-axis to X-axis, meaning scaling from 0-100% 
// first 0f, 1f mean scaling from Y-axis to Y-axis, meaning scaling from 0-100% 
// The two 0.5f mean animation will start from 50% of X-axis & 50% of Y-axis, i.e. from center 

ScaleAnimation fade_in = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
fade_in.setDuration(1000);  // animation duration in milliseconds 
fade_in.setFillAfter(true); // If fillAfter is true, the transformation that this animation performed will persist when it is finished. 
view.startAnimation(fade_in); 
+1

@ T.Toduaどのようなエラーがありますか?新しい質問をして、この回答に戻るようにしてください。 –

+0

問題は私の側にあった...ありがとう。 –

関連する問題