web-dev-qa-db-ja.com

Android->新しい位置にアニメーションする方法

ここに単純なxmlがありますAndroidアニメーション:

<translate xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:fromXDelta="0" Android:fromYDelta="0" Android:toXDelta="-110"
    Android:toYDelta="-110" Android:duration="1000" Android:fillAfter="true" />

アニメーションオブジェクトを画面の中央から0、0の位置に移動したい。猫の作り方(すべての画面解像度で機能するはずです)


私の答え:

ご協力ありがとうございます。しかし、私は別の方法で動的に、xmlなしで問題を修正しました。このメソッドの完全なコードは次のとおりです。

public void replace(int xTo, int yTo, float xScale, float yScale) {
        // create set of animations
        replaceAnimation = new AnimationSet(false);
        // animations should be applied on the finish line
        replaceAnimation.setFillAfter(true);

        // create scale animation
        ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
        scale.setDuration(1000);

        // create translation animation
        TranslateAnimation trans = new TranslateAnimation(0, 0,
                TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
                TranslateAnimation.ABSOLUTE, yTo - getTop());
        trans.setDuration(1000);

        // add new animations to the set
        replaceAnimation.addAnimation(scale);
        replaceAnimation.addAnimation(trans);

        // start our animation
        startAnimation(replaceAnimation);
    }
15
pleerock

私の解決策:

ご協力ありがとうございます。しかし、私は別の方法で動的に、xmlなしで問題を修正しました。このメソッドの完全なコードは次のとおりです。

public void replace(int xTo, int yTo, float xScale, float yScale) {
        // create set of animations
        replaceAnimation = new AnimationSet(false);
        // animations should be applied on the finish line
        replaceAnimation.setFillAfter(true);

        // create scale animation
        ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
        scale.setDuration(1000);

        // create translation animation
        TranslateAnimation trans = new TranslateAnimation(0, 0,
                TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
                TranslateAnimation.ABSOLUTE, yTo - getTop());
        trans.setDuration(1000);

        // add new animations to the set
        replaceAnimation.addAnimation(scale);
        replaceAnimation.addAnimation(trans);

        // start our animation
        startAnimation(replaceAnimation);
    }
15
pleerock

まず、画面全体を占めるオブジェクトをコンテナに挿入します。次に、アニメーションxmlを次のように変更します

<translate xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:fromXDelta="50%p" Android:fromYDelta="50%p" 
    Android:toXDelta="0%p" Android:toYDelta="0%p" 
    Android:duration="1000" 
    Android:fillAfter="true" />

これも確認できますAndroid APIリファレンス http://developer.Android.com/guide/topics/resources/animation-resource.html#translate-element

%pサフィックスは、要素を「親サイズに対するパーセンテージで」変換します。

6

すべての画面で機能させる場合は、x、yの値をハードコードすることはできません。アニメーションではパーセントを使用できます。このアニメーションは、ビューを0%xとyから移動します(それ自体に対して相対的です。つまり、アニメーションの前であればどこからでも開始されます)0%pxとyに移動します(つまり、親)これで左上に移動します。必要なコーナーまでの距離に応じて、5%pまたは10%pを試し、余白を増やすことができます。

<translate xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:fromXDelta="0%" Android:fromYDelta="0%" Android:toXDelta="0%p"
    Android:toYDelta="0%p" Android:duration="1000" Android:fillAfter="true" />
0
FoamyGuy