Android -> how to animate to the new position

后端 未结 3 1108
粉色の甜心
粉色の甜心 2021-02-02 15:58

Here is simple xml android animation:



        
相关标签:
3条回答
  • 2021-02-02 16:22

    If you want it to work on all screens you aren't going to be able to hardcode x,y values. Animations allow you to use percents. This animation would move your view from 0% x and y(relative to itself. In other words where ever it is before the animation it will start from there) It will move to 0%p x and y (which means 0% relative to the parent) This should take you to the top left. Depending on how far into the corner you want it you may try 5%p or 10%p to get some extra margin.

    <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 讨论(0)
  • 2021-02-02 16:24

    My solution:

    Thank you guys for your help. But I have fix my problem by other way, dynamically, without xml. Here's full code of this method:

    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);
        }
    
    0 讨论(0)
  • 2021-02-02 16:34

    First, insert object in container that occupies entire screen. Then modify you animation xml like this

    <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" />
    

    You can also check this android API reference http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

    %p suffix translates element "in percentage relative to the parent size".

    0 讨论(0)
提交回复
热议问题